简体   繁体   中英

Legend for lines but not fill ggplot2

I am creating histograms by month using ggplot2. I am able to easily get the plot structured how I want, but can't seem to get the legend quite right.

Ideally I would be able to suppress the legend associated with the fill of the histograms, as that information is apparent via the facet title. I would like to have a legend for the vertical lines I'm drawing, one at zero and one at the mean for each group.

Here's some code that gets me really close, but I can't seem to get the legend to draw. In the past, I've had success with scale_color_manual() , but it seems to be letting me down here (ie I seem to be letting it down here).

library(ggplot2)
set.seed(232)

## Data that are somewhat similar to mine
df = data.frame(var=c(rnorm(250, 0.5, 1), 
                      rnorm(250, 1.1, 1), 
                      rnorm(250),
                      rnorm(250, -0.8, 1)),
                month=factor(c(rep('Jan', 250),
                               rep('Feb', 250),
                               rep('Mar', 250),
                               rep('Apr', 250)),
                             levels=c('Jan', 'Feb', 
                                      'Mar', 'Apr')))

## I couldn't get ggplot2 to calculate the means within an aes() call,
## so I'm calculating them myself using aggregate
means = aggregate(var ~ month, df, mean)

## Vector of colors with names I want in legend
cols = c('Zero'='black', 'Mean'='#990000')

## Current plot code that doesn't quite give me what I want.
## This is missing the legend from scale_color_manual() for the vlines
hists = ggplot(data=df) +
  geom_histogram(aes(var, fill=month)) +
  facet_wrap( ~ month) + 
  geom_vline(aes(xintercept=0, color='Zero')) +
  geom_vline(aes(xintercept=var, color='Mean'), data=means) +
  scale_color_manual(name='', values=cols) +
  guides(fill=FALSE)
print(hists)

Here's what it currently looks like without the legend. I want it like this, but with a legend for the vertical lines: 我几乎完整的情节缺少垂直线的图例 Any suggestions? Thanks!

Try modifying means to look like:

> means
  month         var   col
1   Jan  0.53487078   red
2   Feb  1.16130990   red
3   Mar  0.06391861   red
4   Apr -0.76902156   red
5   Jan  0.00000000 black
6   Feb  0.00000000 black
7   Mar  0.00000000 black
8   Apr  0.00000000 black

...and then try this:

hists = ggplot(data=df) +
  geom_histogram(aes(var, fill=month)) +
  facet_wrap( ~ month) + 
  geom_vline(aes(xintercept=var, color=col), data=means,show_guide = TRUE) +
  scale_color_manual(name='', values=c('black','red')) +
  guides(fill=FALSE)
print(hists)

...and then adjust the data frame column col to make the legend say what you want.

EDIT FROM OP -- FINAL CODE:

library(ggplot2)
set.seed(232)
df = data.frame(var=c(rnorm(250, 0.5, 1), 
                      rnorm(250, 1.1, 1), 
                      rnorm(250),
                      rnorm(250, -0.8, 1)),
                month=factor(c(rep('Jan', 250),
                               rep('Feb', 250),
                               rep('Mar', 250),
                               rep('Apr', 250)),
                             levels=c('Jan', 'Feb', 
                                      'Mar', 'Apr')))

means = aggregate(var ~ month, df, mean)
means$col = rep('Mean', nrow(means))
means = rbind(means, data.frame(month=means$month,
                                var=rep(0, nrow(means)),
                                col=rep('Zero', nrow(means))))
cols = c('Mean'='#990000', 'Zero'='black')
hists = ggplot(data=df) +
  geom_histogram(aes(var, fill=month)) +
  facet_wrap( ~ month) + 
  geom_vline(aes(xintercept=var, color=col), data=means, show_guide=TRUE) +
  scale_color_manual(name='', values=cols) +
  guides(fill=FALSE)
print(hists)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM