0

I want to plot a very simple line plot (timeseries chart, actually). The only thing I want is the line to appear not in black but in the first color of the default color wheel that ggplot2 uses (i.e. replace the default "black" --- which might be specified somewhere --- with the first color of the default ggplot2 color palette).

I have only come up with a non-elegant version where I manually specify the HCL value of the first color (hcl(h=15, l=65, c=100)) - thanks to info in this answer: https://stackoverflow.com/a/8197703/1477035

Isn't there a more elegant way? I've also thought about including a factor variable which only has one value and then specifying colour=as.factor(MyFactorVariable), but this messes up the na.rm = T somehow (because I need to have gaps in the line where NAs appear).

Data sample:

structure(list(Date = structure(c(15701, 15702, 15703, 15704, 
15705, 15706, 15707, 15708, 15709, 15710, 15711, 15712, 15713, 
15714, 15715, 15716, 15717, 15718, 15719, 15720, 15721, 15722, 
15723, 15724, 15725, 15726, 15727, 15728, 15729, 15730, 15731, 
15732, 15733, 15734, 15735, 15736, 15737, 15738, 15739, 15740
), class = "Date"), Additions = c(398L, 212L, 171L, 133L, 124L, 
99L, 105L, 103L, 99L, 101L, 104L, 102L, 99L, 70L, 76L, 95L, 51L, 
76L, 76L, 65L, 73L, 84L, 71L, 82L, 47L, 78L, 65L, 73L, 79L, 64L, 
45L, NA, NA, NA, NA, NA, NA, NA, 149L, 198L)), .Names = c("Date", 
"Additions"), row.names = c(NA, 40L), class = "data.frame")

Code:

p <- ggplot(data, aes(Date, Additions), na.rm = T) 
p <- p + geom_line(colour=hcl(h=15, l=65, c=100)) + scale_x_date(labels = date_format("%B")) 
p <- p + xlab("Date of addition") + ylab("Daily additions to user database") 
p <- p + theme_minimal() + theme(plot.margin = unit(c(1,1,1,1), "cm"), 
                        axis.title.x = element_text(vjust=-1),
                        axis.title.y = element_text(angle=90, vjust=0),
                        panel.grid.minor = element_blank(),
                        plot.background = element_rect(fill = rgb(0.99,0.99,0.99))
                        ) 

p

This gives:

output

Community
  • 1
  • 1
grssnbchr
  • 2,877
  • 7
  • 37
  • 71
  • have you tried `geom_line(colour="red")` or any value other than black..? PS: It'd be nice if you provide `dput(your_data)` output – Arun Jul 29 '13 at 08:40
  • I have tried this, but it's certainly not an improvement over `colour=hcl(h=15, l=65, c=100)` , it just changes the color to the eye-stinging red I don't want. I added the data, thanks for the hint. – grssnbchr Jul 29 '13 at 08:56
  • What you're asking is for it to choose a colour by itself which is to your liking... How's it possible? If you want a colour of your choice, you can use [IWantHue](http://tools.medialab.sciences-po.fr/iwanthue/) to get the colour you want in hex format and just add it to colour. If you want to generate multiple colours based on levels of a factor column, then you can do: `geom_line(aes(colour=my_factor_column))`. – Arun Jul 29 '13 at 09:02
  • No, that is not my question. My question is how to generically use the first color of the ggplot2 default colors instead of the default black, without having to specify it manually. If this is not possible, I'll have to stick with `colour=hcl(h=15, l=65, c=100)`. – grssnbchr Jul 29 '13 at 09:07
  • the default colour if no colour aesthetic is provided is "black". If you want another colour, then specify it. But the way you're generating it is unnecessary as you're using only 1 colour. Have a look at [scale_colour_brewer()](http://docs.ggplot2.org/0.9.3.1/scale_brewer.html), probably that's what you're looking for... – Arun Jul 29 '13 at 09:10
  • Alright, then I have to specify it, even though it seems weird that there is no way of changing the default color. But why should this be unnecessary? Why should I have to manually convert `hcl(h=15, l=65, c=100)` to a hex value using some tool? And why are you suggesting looking into brewer colors, that's just another palette I am not interested in but has nothing to do with the problem at hand?! – grssnbchr Jul 29 '13 at 09:17
  • I think I just don't understand your question. I'll leave it to someone who does, as your Q doesn't make sense to me. – Arun Jul 29 '13 at 09:25
  • You could specify the color as depending on a factor such as this `p + geom_line(aes(colour=factor(1))) + scale_color_discrete(guide="none")`, but I don't know why that should be any better than specifying the color directly. – shadow Jul 29 '13 at 09:50

1 Answers1

2

Is this elegant enough?

ggdefault_cols <- function(n) hcl(h=seq(15, 375-360/n, length=n)%%360, 
                                  c=100, l=65)

#First 4 ggplot default colours:
ggdefault_cols(4)
#[1] "#F8766D" "#7CAE00" "#00BFC4" "#C77CFF"

ggplot(data, aes(Date, Additions), na.rm = T) + 
        geom_line(colour=ggdefault_cols(1))
Roland
  • 127,288
  • 10
  • 191
  • 288