ggplot2 - Faulty legend in R with "color_scale_manual" -


can please tell me why legend not displaying correctly (the point in legend hypericin filled green , not blue).

here 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

it definately see data reproducability.

guessing structure of data results in this.

# create 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 plotted this:

# plot 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 plot this:

plot1

explanation

first of all, don't need add data = df in ggplot-functions if defined in first ggplot-call.

furthermore, ggplot likes tidy data best (aka. long-format. read more here http://vita.had.co.nz/papers/tidy-data.pdf). adding 2 datasets (df, , df2) possible merging them , creating every variable in dataset has advantage easier understand data.

your error (a green point instead of blue one) came confusion. in line 6 stated fill = "blue", don't change later (i.e., don't specify this: scale_fill_color(...).

does give want?

lastly, future questions, please make sure follow mwe-principle (minimal-working-example) make life of trying answer question bit easier: how make great r reproducible example?


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -