Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top