3

I have created a multi-panel plot using the lattice package in R with the below code. The plot has four panels each of which contains a histogram. I would like the bars in each panel to be a unique colour, but I am having trouble achieving this.

Para = as.vector(rnorm(100, mean=180, sd=35))
Year = as.vector(c(rep(1,25),rep(2,25),rep(3,25), rep(4,25)))
df = as.data.frame(cbind(Para, Year))

library(lattice)
print(histogram(~ Para | Year, data = df, scales = list(cex = c(1.0, 1.0)), 
                layout = c(4, 1), type = "count",
                xlab = list("Parameter", fontsize = 16), 
                ylab = list("Frequency", fontsize = 16), 
                strip = function(bg = 'white',...) strip.default(bg = 'white', ...),
                        breaks = seq(0, 300, by = 50), ylim = seq(0, 35, by = 10),
                        as.table = TRUE, groups = year, col = c("red", "blue", "green", "purple")))

I tried adding as.table=TRUE, groups=year, col=c("red", "blue", "green", "purple") to the code as was suggested in a related question found here R: specifying color for different facets / panels in lattice, but all this did was to alternate the colours of the bars WITHIN each panel rather than BETWEEN panels.

Any suggestions on how to remedy this would be much appreciated!

If possible, I would also like to know how a similar effect can be achieved on the strip above each panel (i.e. whether the background of each panel strip can be set individually rather than just for all panels in the entire plot?)

Many thanks!

Henrik
  • 65,555
  • 14
  • 143
  • 159
Emily
  • 859
  • 5
  • 14
  • 31

2 Answers2

4

Here is the lattice version. Essentially you define a vector of colors, which I did outside the histogram() function. In the histogram() function you use a panel.function which will allow you to make each panel look different. You call panel.histogram and tell it to pick a color based on packet.number(). That function just returns an integer indicating which panel is being drawn, panel 1 gets red, panel 2 gets blue etc.

Para = as.vector(rnorm(100, mean=180, sd=35))
Year = as.vector(c(rep(1,25),rep(2,25),rep(3,25), rep(4,25)))
df = as.data.frame(cbind(Para,Year))

colors<-c("red","blue","green","purple")  #Define colors for the histograms

print(histogram(~Para | Year, data = df,  scales=list(cex=c(1.0,1.0)), 
    layout=c(4,1), type="count", xlab=list("Parameter", fontsize=16), 
    ylab=list("Frequency", fontsize=16), 
    strip=function(bg='white',...) strip.default(bg='white',...), 
    breaks=seq(0,300, by=50), ylim=seq(0,35,by=10),
    as.table=TRUE, groups=Year,col=colors, #passing your colors to col

    panel=function(x, col=col,...){
    panel.histogram(x,col=col[packet.number()],...) #gets color for each panel
    }
))
John Paul
  • 12,196
  • 6
  • 55
  • 75
3

You may try a ggplot alternative:

library(ggplot2)
ggplot(data = df, aes(x = Para, fill = factor(Year))) +
  geom_histogram() +
  facet_wrap(~ Year) +
  scale_fill_manual(values = c("red", "blue", "green", "purple"))

enter image description here

To colour the strip background by Year, you may have a look here for some ideas.

Community
  • 1
  • 1
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • @ Henrik: Many thanks for the answer! I would however really like to achieve this using the lattice package if there is a way? – Emily May 11 '14 at 16:03
  • @Emily, I am sure you can make the desired plot with `lattice`. However, I don't know `lattice` well enough to help you, sorry... – Henrik May 11 '14 at 16:04