I want to make a matrix of plots, with the background color coded to correspond with particular columns (i.e., I want the colors I want, and in a particular sequence).
I used info from here Conditionally change panel background with facet_grid? to come up with a way to color code background colors, by column, in a matrix of plots in ggplot2, running in RStudio.
But, previously I had been working with some colorblind colors (I'm not colorblind though), and I can't for the life of me get those colors to change to a new set of colors, even when I clear objects from the global environment in RStudio, then reopen the R script in a new RStudio session. Somehow, by old colors are still remembered.
What I see as output (as a JPG image file) is https://drive.google.com/file/d/1I_5GJHOW8VPZShJ4C61Uayo8zrEMPCDZ/ - you will certainly get other colors when running my code.
Two Questions:
- How can I control those colors/assign particular colors to climate$elevation levels?
- The approach I've used seems to mask my gridlines within plots - at the bottom of my code you can see I tried to add the gridlines back using 'theme' but it does not work, or is behind the colors of elevation level. Is there a better way to do this?
Here is my code:
# install.packages("ggplot2")
library(ggplot2)
#### create Data ####
#####################
temperature<-c(12,43,15,8,7,9,12,15,14,10,34,27,10,12,33,11,15,7,9,12,25,14,9,10,34,37,10,12,33,15,8,7,9,12,15,14,10,34,37,10,12,33,11,15,7,9,12,15,14,9,10,34,37,10,12,33,15,8,7,9,12,15,14,10,34,37,10,12,33,11,15,7,9,12,15,14,9,10,34,37,10,12,15,14,9,36,10,34,37,10)
hour<-c(2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12,2,4,6,8,10,12)
elevation<-c("Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast","Alpine","Montane","Steppe","Valley","Coast")
region<-c("Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America","Europe","N America","S America")
climate<-data.frame(region,elevation,hour,temperature) # create data frame
# Ensures sequence corresponds to sequence of cave zones
climate$elevation <-factor(climate$elevation,
levels = c('Alpine','Montane','Steppe','Valley','Coast'),order=TRUE)
# create plot matrix, background colored by elevation
base<-ggplot(data = climate, aes(hour, temperature)) +
geom_rect(aes(fill = elevation),xmin = -Inf,xmax = Inf, ymin = -Inf,ymax = Inf,alpha = 0.3) +
geom_line(color = "steelblue", size = 1) +
geom_point(color="steelblue") +
labs(title = "Elevation by Region & Time of Day",
subtitle = "(Temperature)",
y = "Mean Temperature", x = "Hour of Day") +
facet_grid(region ~ elevation)
base
###Plot looks like this to me:
https://drive.google.com/file/d/1I_5GJHOW8VPZShJ4C61Uayo8zrEMPCDZ/
# I'm certain colors will look different for you!
# Trying to add gridlines (does not work)
base + theme(
panel.background = element_rect(fill = "transparent",colour = NA),
plot.background = element_rect(fill = "transparent",colour = NA),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_blank()
)