R - How to assign color for values with color2d.matplot
My question deals with the color2d.matplot
function from the plotrix
package. The function is documented here.
I have this output:
Produced by this code:
library(plotrix)
# model parameters
spaces <- 400
agents<- 300
prop_black = 0.5
prop_white = 1 - prop_black
tolerance <- 0.6
# creating matrix of types
group<-c(rep(0,spaces-agents),rep(1,prop_black*agents),rep(2,prop_white*agents))
grid<-matrix(sample(group,400,replace=F), ncol=20)
# plotting
color2D.matplot(grid, ylab="", xlab = "", axes=F)
plot(runif(100,0,1),ylab="Happy",xlab="Time",col="white",ylim=c(0,1))
Notice that my grid
contains values of 0,1,2 only.
How do I make it so that:
- All values of 0 map to white squares.
- All values of 1 map to red squares.
- All values of 2 map to blue squares.
I tried to figure it out by looking at these examples but didn't have much luck.
r plot plotrix
add a comment |
My question deals with the color2d.matplot
function from the plotrix
package. The function is documented here.
I have this output:
Produced by this code:
library(plotrix)
# model parameters
spaces <- 400
agents<- 300
prop_black = 0.5
prop_white = 1 - prop_black
tolerance <- 0.6
# creating matrix of types
group<-c(rep(0,spaces-agents),rep(1,prop_black*agents),rep(2,prop_white*agents))
grid<-matrix(sample(group,400,replace=F), ncol=20)
# plotting
color2D.matplot(grid, ylab="", xlab = "", axes=F)
plot(runif(100,0,1),ylab="Happy",xlab="Time",col="white",ylim=c(0,1))
Notice that my grid
contains values of 0,1,2 only.
How do I make it so that:
- All values of 0 map to white squares.
- All values of 1 map to red squares.
- All values of 2 map to blue squares.
I tried to figure it out by looking at these examples but didn't have much luck.
r plot plotrix
1
What does the secondplot
thing has to do with your question? ;)
– Henrik
Nov 16 '18 at 11:35
add a comment |
My question deals with the color2d.matplot
function from the plotrix
package. The function is documented here.
I have this output:
Produced by this code:
library(plotrix)
# model parameters
spaces <- 400
agents<- 300
prop_black = 0.5
prop_white = 1 - prop_black
tolerance <- 0.6
# creating matrix of types
group<-c(rep(0,spaces-agents),rep(1,prop_black*agents),rep(2,prop_white*agents))
grid<-matrix(sample(group,400,replace=F), ncol=20)
# plotting
color2D.matplot(grid, ylab="", xlab = "", axes=F)
plot(runif(100,0,1),ylab="Happy",xlab="Time",col="white",ylim=c(0,1))
Notice that my grid
contains values of 0,1,2 only.
How do I make it so that:
- All values of 0 map to white squares.
- All values of 1 map to red squares.
- All values of 2 map to blue squares.
I tried to figure it out by looking at these examples but didn't have much luck.
r plot plotrix
My question deals with the color2d.matplot
function from the plotrix
package. The function is documented here.
I have this output:
Produced by this code:
library(plotrix)
# model parameters
spaces <- 400
agents<- 300
prop_black = 0.5
prop_white = 1 - prop_black
tolerance <- 0.6
# creating matrix of types
group<-c(rep(0,spaces-agents),rep(1,prop_black*agents),rep(2,prop_white*agents))
grid<-matrix(sample(group,400,replace=F), ncol=20)
# plotting
color2D.matplot(grid, ylab="", xlab = "", axes=F)
plot(runif(100,0,1),ylab="Happy",xlab="Time",col="white",ylim=c(0,1))
Notice that my grid
contains values of 0,1,2 only.
How do I make it so that:
- All values of 0 map to white squares.
- All values of 1 map to red squares.
- All values of 2 map to blue squares.
I tried to figure it out by looking at these examples but didn't have much luck.
r plot plotrix
r plot plotrix
edited Nov 16 '18 at 12:16
Henrik
42.3k994110
42.3k994110
asked Nov 16 '18 at 10:50
Thevesh ThevaThevesh Theva
4321616
4321616
1
What does the secondplot
thing has to do with your question? ;)
– Henrik
Nov 16 '18 at 11:35
add a comment |
1
What does the secondplot
thing has to do with your question? ;)
– Henrik
Nov 16 '18 at 11:35
1
1
What does the second
plot
thing has to do with your question? ;)– Henrik
Nov 16 '18 at 11:35
What does the second
plot
thing has to do with your question? ;)– Henrik
Nov 16 '18 at 11:35
add a comment |
2 Answers
2
active
oldest
votes
You may index a colour vector with the values in your matrix. A smaller example:
color2D.matplot(m, cellcolors = c("white", "red", "blue")[m + 1])
Data:
set.seed(7)
m <- matrix(sample(0:2, 9, replace = TRUE), nrow = 3, ncol = 3)
m
# [,1] [,2] [,3]
# [1,] 2 0 1
# [2,] 1 0 2
# [3,] 0 2 0
add a comment |
One thing I could think of is to specify the color for each cell.
cellcolors <- unlist(grid)
cellcolors <- ifelse(cellcolors == 0, "white", ifelse(cellcolors == 1, "red", "blue"))
color2D.matplot(grid, ylab="", xlab = "", axes=F, cellcolors = cellcolors)
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336348%2fr-how-to-assign-color-for-values-with-color2d-matplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may index a colour vector with the values in your matrix. A smaller example:
color2D.matplot(m, cellcolors = c("white", "red", "blue")[m + 1])
Data:
set.seed(7)
m <- matrix(sample(0:2, 9, replace = TRUE), nrow = 3, ncol = 3)
m
# [,1] [,2] [,3]
# [1,] 2 0 1
# [2,] 1 0 2
# [3,] 0 2 0
add a comment |
You may index a colour vector with the values in your matrix. A smaller example:
color2D.matplot(m, cellcolors = c("white", "red", "blue")[m + 1])
Data:
set.seed(7)
m <- matrix(sample(0:2, 9, replace = TRUE), nrow = 3, ncol = 3)
m
# [,1] [,2] [,3]
# [1,] 2 0 1
# [2,] 1 0 2
# [3,] 0 2 0
add a comment |
You may index a colour vector with the values in your matrix. A smaller example:
color2D.matplot(m, cellcolors = c("white", "red", "blue")[m + 1])
Data:
set.seed(7)
m <- matrix(sample(0:2, 9, replace = TRUE), nrow = 3, ncol = 3)
m
# [,1] [,2] [,3]
# [1,] 2 0 1
# [2,] 1 0 2
# [3,] 0 2 0
You may index a colour vector with the values in your matrix. A smaller example:
color2D.matplot(m, cellcolors = c("white", "red", "blue")[m + 1])
Data:
set.seed(7)
m <- matrix(sample(0:2, 9, replace = TRUE), nrow = 3, ncol = 3)
m
# [,1] [,2] [,3]
# [1,] 2 0 1
# [2,] 1 0 2
# [3,] 0 2 0
answered Nov 16 '18 at 12:11
HenrikHenrik
42.3k994110
42.3k994110
add a comment |
add a comment |
One thing I could think of is to specify the color for each cell.
cellcolors <- unlist(grid)
cellcolors <- ifelse(cellcolors == 0, "white", ifelse(cellcolors == 1, "red", "blue"))
color2D.matplot(grid, ylab="", xlab = "", axes=F, cellcolors = cellcolors)
add a comment |
One thing I could think of is to specify the color for each cell.
cellcolors <- unlist(grid)
cellcolors <- ifelse(cellcolors == 0, "white", ifelse(cellcolors == 1, "red", "blue"))
color2D.matplot(grid, ylab="", xlab = "", axes=F, cellcolors = cellcolors)
add a comment |
One thing I could think of is to specify the color for each cell.
cellcolors <- unlist(grid)
cellcolors <- ifelse(cellcolors == 0, "white", ifelse(cellcolors == 1, "red", "blue"))
color2D.matplot(grid, ylab="", xlab = "", axes=F, cellcolors = cellcolors)
One thing I could think of is to specify the color for each cell.
cellcolors <- unlist(grid)
cellcolors <- ifelse(cellcolors == 0, "white", ifelse(cellcolors == 1, "red", "blue"))
color2D.matplot(grid, ylab="", xlab = "", axes=F, cellcolors = cellcolors)
answered Nov 16 '18 at 11:48
erocoarerocoar
3,79811333
3,79811333
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336348%2fr-how-to-assign-color-for-values-with-color2d-matplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What does the second
plot
thing has to do with your question? ;)– Henrik
Nov 16 '18 at 11:35