2

Using base R I want to create a plot like this with two groups, one beside and other on the top.

enter image description here

Here is data and code I played with:

brown <- c(5,4,3)
green <- c(4,7,8)
blue <- c(4,7,2)
dark <- data.frame(brown=brown, green=green, blue=blue)
barplot( as.matrix(t(dark)), col = c("brown", "green", "blue"), beside = TRUE)

brownL <- c(3,1,2)
greenL <- c(2,2,4)
blueL <- c(3,2,1)
light <- data.frame(brownL =brownL, greenL =greenL, blueL =blueL)
barplot( as.matrix(t(light)), col = c("pink", "lightgreen", "lightblue"), add=TRUE)

Is there base R solution for this ?

SHRram
  • 4,127
  • 7
  • 35
  • 53

2 Answers2

1

Try this:

barplot( as.matrix(t(dark)), col = c("brown", "green", "blue"), beside = TRUE)
barplot( as.matrix(t(light)),col = c("pink", "lightgreen", "lightblue"), beside = TRUE, add=TRUE)
Shirin Yavari
  • 626
  • 4
  • 6
  • thank you , yes, this trick works ( but as is not correct as the frequency are not correct) but I have to draw dark + light with light colors first and then dark on the added plot. – SHRram Sep 19 '18 at 13:47
0

You need to make one plot additive and draw the dark color on top of it.

brown <- c(5, 4, 3)
green <- c(4, 7, 8)
blue <- c(4, 7, 2)
dark <- data.frame(brown = brown, green = green, blue = blue)

brownL <- c(3, 1, 2)
greenL <- c(2, 2, 4)
blueL <- c(3, 2, 1)
light <- data.frame(brownL = brownL, greenL = greenL, blueL = blueL)

barplot(as.matrix(t(light + dark)),col = c("pink", "lightgreen", "lightblue"), beside = TRUE)
barplot(as.matrix(t(dark)), col = c("brown", "green", "blue"), beside = TRUE, add = TRUE)

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29