Hi, I don't quite follow what you want to achieve - colored backgrounds for each panel? You could RSeek.org for some ideas on how to fish the panels grobs out... https://rseek.org/?q=ggplot+facet+backgound+color https://rseek.org/?q=ggplot+facet+backgound+color I can get sort of close by skipping the use of table ct and instead filling the panels using your df table. But it doesn't color each panel instead it colors each geo group. ggplot(df,aes(x=date,y=vari))+ geom_rect(aes(fill=geo), xmin=-Inf,xmax=Inf, ymin=-Inf,ymax=Inf,alpha = 0.3)+ scale_fill_manual(values=colr)+ geom_bar(stat="identity")+ facet_grid(est~geo) You could add another column, composed of geo and est, and fill by that... df <- dplyr::tibble(date,vari,geo,est) %>% dplyr::mutate(colr = paste(geo, est)) ggplot(df,aes(x=date,y=vari))+ theme(panel.background = element_rect(fill = colr)) + scale_fill_manual(values=colr)+ geom_bar(stat="identity")+ facet_grid(est~geo) But it makes for a long set of labels on the scale bar thingy. I hope that helps. Ben On Mon, Oct 25, 2021 at 9:08 PM <phil at philipsmith.ca> wrote:> > I am using ggplot2 and I want to use different colours for some facets. > Here is a reprex: > > library(tidyverse) > date <- as.numeric(c(2017,2017,2017,2017,2017,2017,2018,2018, > 2018,2018,2018,2018,2019,2019,2019,2019,2019,2019)) > vari <- as.numeric(c(4.8,3.3,4.2,5.2,4.8,5.7,5.4,3.1,5.7,4.1, > 3.1,1.5,4.5,4.4,2.8,2.0,2.1,2.2)) > geo <- as.factor(c("Canada","Canada","Nova Scotia", > "Nova Scotia","Manitoba","Manitoba","Canada", > "Canada","Nova Scotia","Nova Scotia","Manitoba", > "Manitoba","Canada","Canada","Nova Scotia", > "Nova Scotia","Manitoba","Manitoba")) > est <- as.factor(c("Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income","Wages and salaries", > "Gross mixed income")) > df <- data.frame(date,vari,geo,est) > ct <- unique(df[,c('est','geo')]) > colr=c("blue","forestgreen","red","lightblue", > "brown","gold") > p0 <- ggplot(df,aes(x=date,y=vari))+ > geom_rect(data=ct,aes(fill=geo), > xmin=-Inf,xmax=Inf, > ymin=-Inf,ymax=Inf,alpha = 0.3)+ > scale_fill_manual(values=colr)+ > geom_bar(stat="identity")+ > facet_grid(est~geo) > p0 > > I have tried several approaches and Googled for help, but to no avail. I > am getting the error message: Error: Aesthetics must be either length 1 > or the same as the data (6): x and y > > Thanks for some help. > > Philip > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Ben Tupper (he/him) Bigelow Laboratory for Ocean Science East Boothbay, Maine http://www.bigelow.org/ https://eco.bigelow.org
Whoopsie. Misfired cut and paste - all 13 fingers are thumbs today. That second one should be... df <- dplyr::tibble(date,vari,geo,est) %>% dplyr::mutate(colr = paste(geo, est)) ggplot(df,aes(x=date,y=vari))+ geom_rect(aes(fill=colr), xmin=-Inf,xmax=Inf, ymin=-Inf,ymax=Inf,alpha = 0.3)+ scale_fill_manual(values=colr)+ geom_bar(stat="identity")+ facet_grid(est~geo) On Mon, Oct 25, 2021 at 10:37 PM Ben Tupper <btupper at bigelow.org> wrote:> > Hi, > > I don't quite follow what you want to achieve - colored backgrounds > for each panel? You could RSeek.org for some ideas on how to fish the > panels grobs out... https://rseek.org/?q=ggplot+facet+backgound+color > > https://rseek.org/?q=ggplot+facet+backgound+color > > > I can get sort of close by skipping the use of table ct and instead > filling the panels using your df table. But it doesn't color each > panel instead it colors each geo group. > > ggplot(df,aes(x=date,y=vari))+ > geom_rect(aes(fill=geo), > xmin=-Inf,xmax=Inf, > ymin=-Inf,ymax=Inf,alpha = 0.3)+ > scale_fill_manual(values=colr)+ > geom_bar(stat="identity")+ > facet_grid(est~geo) > > > You could add another column, composed of geo and est, and fill by that... > > df <- dplyr::tibble(date,vari,geo,est) %>% > dplyr::mutate(colr = paste(geo, est)) > > ggplot(df,aes(x=date,y=vari))+ > theme(panel.background = element_rect(fill = colr)) + > scale_fill_manual(values=colr)+ > geom_bar(stat="identity")+ > facet_grid(est~geo) > > But it makes for a long set of labels on the scale bar thingy. > > I hope that helps. > > Ben > > > On Mon, Oct 25, 2021 at 9:08 PM <phil at philipsmith.ca> wrote: > > > > I am using ggplot2 and I want to use different colours for some facets. > > Here is a reprex: > > > > library(tidyverse) > > date <- as.numeric(c(2017,2017,2017,2017,2017,2017,2018,2018, > > 2018,2018,2018,2018,2019,2019,2019,2019,2019,2019)) > > vari <- as.numeric(c(4.8,3.3,4.2,5.2,4.8,5.7,5.4,3.1,5.7,4.1, > > 3.1,1.5,4.5,4.4,2.8,2.0,2.1,2.2)) > > geo <- as.factor(c("Canada","Canada","Nova Scotia", > > "Nova Scotia","Manitoba","Manitoba","Canada", > > "Canada","Nova Scotia","Nova Scotia","Manitoba", > > "Manitoba","Canada","Canada","Nova Scotia", > > "Nova Scotia","Manitoba","Manitoba")) > > est <- as.factor(c("Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income","Wages and salaries", > > "Gross mixed income")) > > df <- data.frame(date,vari,geo,est) > > ct <- unique(df[,c('est','geo')]) > > colr=c("blue","forestgreen","red","lightblue", > > "brown","gold") > > p0 <- ggplot(df,aes(x=date,y=vari))+ > > geom_rect(data=ct,aes(fill=geo), > > xmin=-Inf,xmax=Inf, > > ymin=-Inf,ymax=Inf,alpha = 0.3)+ > > scale_fill_manual(values=colr)+ > > geom_bar(stat="identity")+ > > facet_grid(est~geo) > > p0 > > > > I have tried several approaches and Googled for help, but to no avail. I > > am getting the error message: Error: Aesthetics must be either length 1 > > or the same as the data (6): x and y > > > > Thanks for some help. > > > > Philip > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > -- > Ben Tupper (he/him) > Bigelow Laboratory for Ocean Science > East Boothbay, Maine > http://www.bigelow.org/ > https://eco.bigelow.org-- Ben Tupper (he/him) Bigelow Laboratory for Ocean Science East Boothbay, Maine http://www.bigelow.org/ https://eco.bigelow.org
phii m@iii@g oii phiiipsmith@c@
2021-Oct-26 02:40 UTC
[R] Putting colours in ggplot facets
Thanks for the suggestions. As it happens, I just a moment ago found a solution. By adding the lines: ct$date <- 1 and ct$vari <- 1 everything works as I want it to. Philip On 2021-10-25 22:37, Ben Tupper wrote:> Hi, > > I don't quite follow what you want to achieve - colored backgrounds > for each panel? You could RSeek.org for some ideas on how to fish the > panels grobs out... https://rseek.org/?q=ggplot+facet+backgound+color > > https://rseek.org/?q=ggplot+facet+backgound+color > > > I can get sort of close by skipping the use of table ct and instead > filling the panels using your df table. But it doesn't color each > panel instead it colors each geo group. > > ggplot(df,aes(x=date,y=vari))+ > geom_rect(aes(fill=geo), > xmin=-Inf,xmax=Inf, > ymin=-Inf,ymax=Inf,alpha = 0.3)+ > scale_fill_manual(values=colr)+ > geom_bar(stat="identity")+ > facet_grid(est~geo) > > > You could add another column, composed of geo and est, and fill by > that... > > df <- dplyr::tibble(date,vari,geo,est) %>% > dplyr::mutate(colr = paste(geo, est)) > > ggplot(df,aes(x=date,y=vari))+ > theme(panel.background = element_rect(fill = colr)) + > scale_fill_manual(values=colr)+ > geom_bar(stat="identity")+ > facet_grid(est~geo) > > But it makes for a long set of labels on the scale bar thingy. > > I hope that helps. > > Ben > > > On Mon, Oct 25, 2021 at 9:08 PM <phil at philipsmith.ca> wrote: >> >> I am using ggplot2 and I want to use different colours for some >> facets. >> Here is a reprex: >> >> library(tidyverse) >> date <- as.numeric(c(2017,2017,2017,2017,2017,2017,2018,2018, >> 2018,2018,2018,2018,2019,2019,2019,2019,2019,2019)) >> vari <- as.numeric(c(4.8,3.3,4.2,5.2,4.8,5.7,5.4,3.1,5.7,4.1, >> 3.1,1.5,4.5,4.4,2.8,2.0,2.1,2.2)) >> geo <- as.factor(c("Canada","Canada","Nova Scotia", >> "Nova Scotia","Manitoba","Manitoba","Canada", >> "Canada","Nova Scotia","Nova Scotia","Manitoba", >> "Manitoba","Canada","Canada","Nova Scotia", >> "Nova Scotia","Manitoba","Manitoba")) >> est <- as.factor(c("Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income","Wages and salaries", >> "Gross mixed income")) >> df <- data.frame(date,vari,geo,est) >> ct <- unique(df[,c('est','geo')]) >> colr=c("blue","forestgreen","red","lightblue", >> "brown","gold") >> p0 <- ggplot(df,aes(x=date,y=vari))+ >> geom_rect(data=ct,aes(fill=geo), >> xmin=-Inf,xmax=Inf, >> ymin=-Inf,ymax=Inf,alpha = 0.3)+ >> scale_fill_manual(values=colr)+ >> geom_bar(stat="identity")+ >> facet_grid(est~geo) >> p0 >> >> I have tried several approaches and Googled for help, but to no avail. >> I >> am getting the error message: Error: Aesthetics must be either length >> 1 >> or the same as the data (6): x and y >> >> Thanks for some help. >> >> Philip >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code.