R - How to assign color for values with color2d.matplot












1















My question deals with the color2d.matplot function from the plotrix package. The function is documented here.



I have this output:
enter image description here



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.










share|improve this question




















  • 1





    What does the second plot thing has to do with your question? ;)

    – Henrik
    Nov 16 '18 at 11:35
















1















My question deals with the color2d.matplot function from the plotrix package. The function is documented here.



I have this output:
enter image description here



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.










share|improve this question




















  • 1





    What does the second plot thing has to do with your question? ;)

    – Henrik
    Nov 16 '18 at 11:35














1












1








1








My question deals with the color2d.matplot function from the plotrix package. The function is documented here.



I have this output:
enter image description here



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.










share|improve this question
















My question deals with the color2d.matplot function from the plotrix package. The function is documented here.



I have this output:
enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 second plot thing has to do with your question? ;)

    – Henrik
    Nov 16 '18 at 11:35














  • 1





    What does the second plot 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












2 Answers
2






active

oldest

votes


















1














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])


enter image description here





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





share|improve this answer































    1














    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)


    enter image description here






    share|improve this answer
























      Your Answer






      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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])


      enter image description here





      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





      share|improve this answer




























        1














        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])


        enter image description here





        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





        share|improve this answer


























          1












          1








          1







          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])


          enter image description here





          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





          share|improve this answer













          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])


          enter image description here





          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:11









          HenrikHenrik

          42.3k994110




          42.3k994110

























              1














              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)


              enter image description here






              share|improve this answer




























                1














                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)


                enter image description here






                share|improve this answer


























                  1












                  1








                  1







                  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)


                  enter image description here






                  share|improve this answer













                  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)


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 11:48









                  erocoarerocoar

                  3,79811333




                  3,79811333






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine