[R] Putting colours in ggplot facets
phii m@iii@g oii phiiipsmith@c@
phii m@iii@g oii phiiipsmith@c@
Tue Oct 26 03:08:21 CEST 2021
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
More information about the R-help
mailing list