[R] Putting colours in ggplot facets

Paul Murrell p@u| @end|ng |rom @t@t@@uck|@nd@@c@nz
Wed Oct 27 02:28:43 CEST 2021


Sorry, forgot to point out that you would need something like this to 
install ...

remotes::install_github("pmur002/gggrid")

Paul

On 27/10/2021 12:55 pm, phil using philipsmith.ca wrote:
> Thanks for this. I was unable to install the gggrid package apparently
> because it is unavailable for my version of R (4.1.1 (2021-08-10)). But
> I found it here https://github.com/pmur002/gggrid/releases/tag/v0.1-0 
> <https://github.com/pmur002/gggrid/releases/tag/v0.1-0>, 
> 
> copied the code into your code, and it worked very well.
> 
> On 2021-10-26 19:18, Paul Murrell wrote:
>  > Hi
>  >
>  > Another alternative (using 'gggrid' to add a 'grid' rect grob as the
>  > background), just for the record ...
>  >
>  > library(gggrid)
>  >
>  > bgrect <- function(data, coords) {
>  > rectGrob(gp=gpar(col=NA, fill=adjustcolor(data$fill[1], alpha=.3)))
>  > }
>  >
>  > ggplot(df, aes(x=date, y=vari)) +
>  > grid_panel(bgrect, aes(fill=geo)) +
>  > scale_fill_manual(values=colr) +
>  > geom_bar(stat="identity")+
>  > facet_grid(est ~ geo)
>  >
>  > Paul
>  >
>  > On 26/10/2021 3:40 pm, phil using philipsmith.ca wrote:
>  >> 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>
>  >> <https://rseek.org/?q=ggplot+facet+backgound+color 
> <https://rseek.org/?q=ggplot+facet+backgound+color>>
>  >> >
>  >> > https://rseek.org/?q=ggplot+facet+backgound+color 
> <https://rseek.org/?q=ggplot+facet+backgound+color>
>  >> <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 using 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>  >> >> https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help> 
> 
>  >> <https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>>
>  >> >> PLEASE do read the posting guide
>  >> >> http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html> 
> 
>  >> <http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>>
>  >> >> and provide commented, minimal, self-contained, reproducible code.
>  >>
>  >> ______________________________________________
>  >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>  >> https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help> 
> 
>  >> <https://stat.ethz.ch/mailman/listinfo/r-help 
> <https://stat.ethz.ch/mailman/listinfo/r-help>>
>  >> PLEASE do read the posting guide
>  >> http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html> 
> 
>  >> <http://www.R-project.org/posting-guide.html 
> <http://www.R-project.org/posting-guide.html>>
>  >> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul using stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list