[R] Choosing colours for lines in ggplot2

Kimmo Elo k|mmo@e|o @end|ng |rom utu@||
Thu Aug 3 08:14:11 CEST 2023


Hi,

when using 'scale_colours_manual' the colours must be defined as value-
color pairs (i.e. "A"="red", "B"="blue" an so on). If you read the
documentation it is recommended to use a named vector.

If I understood it correctly, in your code #2 R understood the variable
"Stat" as a number (i.e. continuous), whereas "store" (in code #3) is a
categorical variable. You cannot use scale_manual_* functions with
continuous variables.

This should work (please note than I use 'as_factor' from 'haven', this
is a more general function compared to 'as.factor'):

--- snip ---
library(tidyverse)
library(haven)
col.2<-cb8[4:5]
 ggplot(df,aes(Year,Flow,group=Stat,colour=as_factor(Stat)))+
   coord_cartesian(ylim = c(0, 10)) +
   geom_line()+
   geom_point()+
 scale_color_manual(values =cb8[4:5])+
   theme_bw()
--- snip ---

Is the result what you expected?

HTH,
Kimmo

ke, 2023-08-02 kello 18:10 +0100, Nick Wray kirjoitti:
> 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(r
> ep(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.



More information about the R-help mailing list