[R] Choosing colours for lines in ggplot2
Nick Wray
n|ckmwr@y @end|ng |rom gm@||@com
Wed Aug 2 19:10:41 CEST 2023
Hello - I am trying to plot flows in a number of rivers within the same
plot, and need to colour the lines differently, using a colour-blind
palette.
Code beneath works but has colours assigned by the program I have made some
simple dummy data:
## code 1
cb8<- c("#000000", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
"#D55E00", "#CC79A7") ## this is the colour-blind palette
set.seed(020823)
df<-as.data.frame(cbind(rep(1980:1991,2),c(10*runif(12),10*runif(12)),c(rep(1,12),rep(2,12))))
colnames(df)<-c("Year","Flow","Stat")
df
ggplot(df,aes(Year,Flow,group=Stat,colour=Stat))+
coord_cartesian(ylim = c(0, 10)) +
geom_line()+
geom_point()
## this works
## BUT:
## code 2
col.2<-cb8[4:5]
ggplot(df,aes(Year,Flow,group=Stat,colour=Stat))+
coord_cartesian(ylim = c(0, 10)) +
geom_line()+
geom_point()+
scale_color_manual(values =cb8[4:5])+
theme_bw()
## this gives error message Error: Continuous value supplied to discrete
scale
## However this example using code from the net does work so I don't
understand why my ## second code doesn't work.
## code 3
df.1 <- data.frame(store=c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'),
week=c(1, 2, 3, 1, 2, 3, 1, 2, 3),
sales=c(9, 12, 15, 7, 9, 14, 10, 16, 19))
ggplot(df.1, aes(x=week, y=sales, group=store, color=store)) +
geom_line(size=2) +
#scale_color_manual(values=c('orange', 'pink', 'red'))
scale_color_manual(values=cb8[4:6])
Can anyone help? Thanks Nick Wray
[[alternative HTML version deleted]]
More information about the R-help
mailing list