r - Modyfing the Legend in ggplot2 -
i've got problem interacting labels in ggplot2.
i have 2 data sets (temperature vs. time) 2 experiments recorded @ different timesteps. i've managed merge data frames , put them in long fashion plot them in same graph, using melt
function reshape2 library. so, initial data frames this:
> d1 step temp 1 512.5 301.16 2 525.0 299.89 3 537.5 299.39 4 550.0 300.58 5 562.5 300.20 6 575.0 300.17 7 587.5 300.62 8 600.0 300.51 9 612.5 300.96 10 625.0 300.21 > d2 step temp 1 520 299.19 2 540 300.39 3 560 299.67 4 580 299.43 5 600 299.78 6 620 300.74 7 640 301.03 8 660 300.39 9 680 300.54 10 700 300.25
i combine this:
> mrgd <- merge(d1, d2, = "step", = t) step temp.x temp.y 1 512.5 301.16 na 2 520.0 na 299.19 ...
and put long format ggplot2 this:
> melt1 <- melt(mrgd3, id = "step") > melt1 step variable value 1 512.5 temp.x 301.16 2 520.0 temp.x na ...
now, want example histogram of distribution of values. this:
p <- ggplot(data = melt1, aes(x = value, color = variable, fill = variable)) + geom_histogram(alpha = 0.4)
my problem when try modify legend of graph, don't know how to! i've followed suggested in r graphics cookbook book, i've had no luck.
i've tried this, example (to change labels of legend):
> p + scale_fill_discrete(labels = c("d1", "d2"))
but create "new" legend box, so
or removing legend completely
> p + scale_fill_discrete(guide = f)
i
finally, doing doesn't help
> p + scale_fill_discrete("")
again, adds new legend box
does know what's happening here? looks if i'm modyfing label object, if makes sense. i've looked other related questions in site, haven't found having same problem me.
get rid of aes(color = variable...)
remove scale belongs aes(color = ...)
.
ggplot(data = melt1, aes(x = value, fill = variable)) + geom_histogram(alpha = 0.4) + scale_fill_discrete(labels = c("d1", "d1")) # change labels `fill` scale
this second plot contains aes(color = variable...)
. color in case draw colored outlines around histogram bins. can turn off scale have 1 legend, 1 created fill
ggplot(data = melt1, aes(x = value, color = variable, fill = variable)) + geom_histogram(alpha = 0.4) + scale_fill_discrete(labels = c("d1", "d1")) + scale_color_discrete(guide = f) # turn off color (outline) scale
Comments
Post a Comment