Cecile
Cecile

Reputation: 535

Change lines colour in a plot

I am trying to change the colours of the lines in a plot including two curves and 2 vertical lines, but it has been unsuccessful. Would you mind telling me how I can change my script below to assign a specifi colour to each of the 4 lines? Thanks a lot for your help!

data <- data.frame(day_of_year=rep(1:5, 4), variable=c('variable1', 'variable1', 'variable1', 'variable1', 'variable1', 'variable2', 'variable2', 'variable2', 'variable2', 'variable2', 'variable3', 'variable3', 'variable3', 'variable3', 'variable3', 'variable4', 'variable4', 'variable4', 'variable4', 'variable4'), value=c(12, 21, 35, 42, 56, 1, 4, 3, 4, 5, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4))

p1 <- ggplot(data=data)
p1 <- p1 + geom_line(data=data[data$variable=='variable1',], aes(x=day_of_year, y=value, colour='variable1'), size=0.5)
p1 <- p1 + geom_line(data=data[data$variable=='variable2',], aes(x=day_of_year, y=value*10, colour='variable2'), size=0.5)
p1 <- p1 + geom_vline(data=data[data$variable=='variable3',], aes(xintercept=value, colour='variable3'), linetype='dashed', size=0.5)
p1 <- p1 + geom_vline(data=data[data$variable=='variable4',], aes(xintercept=value, colour='variable4'), linetype='dashed', size=0.5)
p1 <- p1 + scale_colour_discrete(breaks=c("variable1","variable2","variable3", 'variable4'))
p1 <- p1 + labs(list(colour = "", title = "(A)", x = "Day of year", y =""))
p1 <- p1 + theme_bw()
p1 <- p1 + theme(title=element_text(size=7, face='bold', vjust=0.8, hjust = 0.5), axis.text.x=element_text(size=7), axis.text.y =element_text(size=8),legend.text=element_text(size=8),legend.title=element_text(size=8), axis.title=element_text(size=8, face='bold'))
p1 <- p1 + geom_text(x=1.5, y=50, label='fallow', size=3)
p1 <- p1 + geom_text(x=3, y=50, label='crop', size=3)
p1 <- p1 + geom_text(x=4.5, y=50, label='fallow',size=3)
p1

This is the graph I have for the moment with the default colours: graph

I have tried to add the scripts below, but without success:

# p1 <- p1 + scale_colour_manual(values=c('red','black','dark grey','blue'))
# p1 <- p1 + scale_colour_discrete(value=c("blue", "red", "grey", 'black'))
# p1 <- p1 + scale_color_manual(value=c("#CC6666", "#9999CC", "#CC6666", "#9999CC"), breaks=c("variable1","variable2","variable3", 'variable4'))

Upvotes: 0

Views: 227

Answers (2)

eipi10
eipi10

Reputation: 93881

Just as for @MrFlick your code worked for me with the sample data you provided (with the exception of the value vs. values typo in two of the three attempts to change the colors).

Normally, you would just want one call to geom_line and then colour=variable would create separate lines for each level of variable. But here it looks like you want to do something different with each level of variable. In any case, here is a modification of your code to make it a bit more concise, and with scale_colour_manual included.

p1 <- ggplot() +
  geom_line(data=d[d$variable=='variable1',], 
            aes(x=day_of_year, y=value, colour=variable), size=0.5) +
  geom_line(data=d[d$variable=='variable2',], 
            aes(x=day_of_year, y=value*10, colour=variable), size=0.5) +
  geom_vline(data=d[d$variable %in% c('variable3','variable4'),], 
             aes(xintercept=value, colour=variable), linetype='dashed', 
             size=0.5, show.legend=FALSE) +
  scale_colour_manual(values=c('red','black','dark grey','blue')) +
  labs(list(colour = "", title = "(A)", x = "Day of year", y ="")) +
  theme_bw(base_size=8) +
  theme(title=element_text(size=7, face='bold', vjust=0.8, hjust = 0.5), 
        axis.text.x=element_text(size=7), 
        axis.title=element_text(face='bold')) +
  geom_text(data=data.frame(x=c(1.5,3,4.5), y=50), 
            aes(x,y), label=c('Fallow','Crop','Fallow'), size=3) 

Upvotes: 2

nick
nick

Reputation: 1160

try this formatting..you can change any parameter to see how the plot changes

 geom_line(mapping=aes(y = y, color = y), size = 1.11) +  theme(panel.grid.major = element_line(colour = "blue", size = 1.25)) +
    theme(axis.ticks = element_line(size = 5, colour = "black")) +
    scale_fill_discrete() + scale_x_date(breaks = "1 month", labels = date_format("%d-%b-%Y")) +
    scale_color_manual(values = c(brewer.pal(9, "Set1"), brewer.pal(9, "Set1"))) + 
    labs(colour = legend.title) + theme(plot.title = element_text(size = rel(1.76))) + 
    guides(colour = guide_legend(override.aes = list(size=3))) +
    scale_y_continuous(breaks=number_ticks(10)) +
    theme(text = element_text(size=20), axis.title=element_text(size=34,face="bold"),
          axis.text.x = element_text(face="bold", color="black", size=24, angle=25),
          axis.text.y = element_text(face="bold", color="black", size=24, angle=0))

Upvotes: 0

Related Questions