0

Can please someone tell me why my legend is not displaying correctly (The point in the legend for Hypericin is filled green and not blue).

Here is my code:

ggplot(df,aes(x=x, y=y))+     
    labs(list(title='MTT_darktox',x=expression('Concentration['*mu*'M]'),y='Survival[%]'))+ 
    scale_x_continuous(breaks=seq(0,50,2.5))+ 
    scale_y_continuous(breaks=seq(0,120,20))+ 
    expand_limits(y=c(0,120))+ 
    geom_point(data=df,shape = 21, size = 3, aes(colour='Hypericin'), fill='blue')+ 
    geom_errorbar(data=df,aes(ymin=y-sd1, ymax=y+sd1),width = 0.8, colour='blue')+ 
    geom_line(data=df,aes(colour='Hypericin'), size = 0.8)+ 
    geom_point(data=df2,shape = 21, size = 3, aes(colour='#212'), fill='green')+ 
    geom_errorbar(data=df2,aes(ymin=y-sd1, ymax=y+sd1),width = 0.8, colour='green')+
    geom_line(data=df2,aes(colour='#212'), size = 0.8)+ 
    scale_colour_manual(name='Batch_Nr', values=c('Hypericin'='blue','#212' ='green')) 

Thank you!

R Plot

David
  • 9,216
  • 4
  • 45
  • 78

2 Answers2

2

It would definately help to see some data for reproducability.

Guessing the structure of your data results in something like this.

# create some fake data:
df <- data.frame(x = rep(1:10, 2),
                 y = c(1/1:10, 1/1:10 + 1),
                 error = c(rnorm(10, sd = 0.05), rnorm(10, sd = 0.1)),
                 group = rep(c("Hypericin", "#212"), each = 10))

Which can be plotted like this:

# plot the data:
library(ggplot2)
ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line() + 
  geom_point() +
  geom_errorbar(aes(ymin = y - error, ymax = y + error)) + 
  scale_colour_manual(name='Batch_Nr', 
                      values = c("Hypericin" = "blue", "#212" = "green")) 

Which results in a plot like this:

Plot1

Explanation

First of all, you don't need to add the data = df in the ggplot-functions if you already defined that in the first ggplot-call.

Furthermore, ggplot likes tidy data best (aka. the long-format. Read more about that here http://vita.had.co.nz/papers/tidy-data.pdf). Thus adding two datasets (df, and df2) is possible but merging them and creating every variable in the dataset has the advantage that its also easier for you to understand your data.

Your error (a green point instead of a blue one) came from this confusion. In line 6 you stated that fill = "blue", which you don't change later (i.e., you don't specify something like this: scale_fill_color(...).

Does that give you what you want?

Lastly, for future questions, please make sure that you follow the MWE-principle (Minimal-Working-Example) to make the life of everyone trying to answer the question a bit easier: How to make a great R reproducible example?

Community
  • 1
  • 1
David
  • 9,216
  • 4
  • 45
  • 78
0

Thank you very much for your help! I will consider the merging for future code. Meanwhile I found another solution to get what I wanted without changing everything (although probably not the cleanest way). I just added another line to override the legend appearance :

guides(colour= guide_legend(override.aes=list(linetype=c(1,1)
                                                       ,shape=c(16,16))))

resulting in :

R plot new