[R] Choosing colours for lines in ggplot2

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Aug 2 20:26:34 CEST 2023


Às 18:10 de 02/08/2023, Nick Wray escreveu:
> 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]]
> 
> ______________________________________________
> R-help using 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.
Hello,

Your Stat column is numeric, therefore, ggplot sees it as continuous.
To make it work, coerce to factor. Here are two ways.


## 1st way, Stat coerce to factor in the ggplot code
## this means you will have to set the legend name
## manually in scale_color_manual
## code 2, now it works
col.2<-cb8[4:5]
ggplot(df, aes(Year,Flow, group = factor(Stat), colour = factor(Stat)))+
   coord_cartesian(ylim = c(0, 10)) +
   geom_line()+
   geom_point()+
   scale_color_manual(name = "Stat", values = cb8[4:5])+
   theme_bw()


## 2nd way, since you are using ggplot2, a tidyverse package,
## coerce to factor in a pipe before the ggplot call
## this is done with dplyr::mutate and R's native pipe operator
## (could also be magritttr's pipe)
## I have left name = "Stat" like above though it's no
## longer needed
df |>
   dplyr::mutate(Stat = factor(Stat)) |>
   ggplot(aes(Year, Flow, group = Stat, colour = Stat))+
   coord_cartesian(ylim = c(0, 10)) +
   geom_line()+
   geom_point()+
   scale_color_manual(name = "Stat", values = cb8[4:5])+
   theme_bw()



Hope this helps,

Rui Barradas



More information about the R-help mailing list