为具有多个 geom 的 facetwrap 创建图例和定义颜色

Creating legend and defining colours for facetwrap with multiple geoms

我正在为每个采样点创建一个多面图,并尝试显示四个变量 - 一个条形图(每个采样点的物种丰度)、一条表示采样点水深的线、另一条表示河流高度的线(在其他地方测量),以及治疗干预的符号。

我的问题是我似乎无法让图例显示所有变量。我已经在 geom_point 的美学中包含了颜色,因此显示出来了,但我无法将 geom_lines 放入图例中。我怎样才能得到图例中代表的所有四个几何体?

另外,我想要第三个 y 轴,因为目前灰线(河流高度)根本没有在 y 轴上表示。我对此进行了研究,不认为可以添加第三个 y 轴,但如果我遗漏了什么,也将不胜感激。

这是代码(现在根据评论更新)

ggplot() +
  geom_bar(data = dat2_site, aes(y=count_l_site, x=date, fill=f_grp), position="stack",stat="identity") +
  geom_line(data = dat2_site, aes(y=2*water_depth, x=date)) +
  geom_point(data = subset(dat2_site, control_previous_week=='yes'), aes(y=-5, x=date), size = 3, shape = 17, colour = 'black', ) +
  geom_line(data = dat_twyford, aes(x = date, y=50*value), linetype = 1) +
  facet_wrap(~site,ncol=3) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), strip.background = element_blank(), axis.line = element_line(colour = "black"), panel.border = element_rect(colour = 'black', fill = NA)) +
  scale_y_continuous(expand = c(0,0), limits=c(-5, 100), sec.axis = sec_axis(~./2, name = 'Water depth (cm)')) +
  scale_x_date(date_breaks = "1 month", date_labels =  "%b") +
  theme(axis.text=element_text(size=14),axis.title=element_text(size=12),legend.text = element_text(size=8))+
  labs(x="Date",y="Number of larvae per litre", colour = 'Control')+
  scale_fill_manual(values = cols_f_grps)+
  theme(legend.position = 'bottom')+
  theme(legend.text.align = 0)

您需要在美学中包括颜色或形状,但正如您发现的那样,R 并不总是会玩球。

此处有更多相关信息https://aosmith.rbind.io/2018/07/19/manual-legends-ggplot2/

我试过编辑你的代码。我建议您在构建 ggplot 之前对数据进行子集化,例如:

dat2_site_y <-
  dat2_site %>%
  filter(control_previous_week == 'yes')   

完成后,您可以正常构建 ggplot,并使用 scale_colour_identity 定义线条颜色、名称和标签。也可以添加scale_shape来添加控制符号。

ggplot() +
  geom_bar(data = dat2_site, aes(y=count_l_site, x=date, fill=f_grp), position="stack",stat="identity") +.    geom_line(data = dat2_site, aes(y=2*water_depth, x=date, colour = 'blue')) +
  geom_point(data = dat2_site_y, aes(y=-5, x=date, shape = control_previous_week), size = 3) +
  geom_line(data = dat_twyford2, aes(x = date, y=100*value, colour = 'grey')) +
  facet_wrap(~site,ncol=3, scales = 'free_y') +
  scale_fill_manual(values = cols_f_grps, name = 'Functional group') +
  scale_colour_identity(name = '', breaks = c('blue', 'grey'), labels = c('Water depth', 'River height'), guide = 'legend') +
  scale_shape(name = 'line', breaks = c('yes'), labels = c('Control'))

我没能运行,正如菲尔在下面所说的,我们需要一个可重现的例子。

如果您计划使用不同的数据框来填充绘图,则不应在 ggplot 函数中使用数据参数。而是将 data 参数移动到它依赖的每个 geom,它应该可以工作。我无法提供输出示例,因为未提供您的数据,因此您的代码不可重现,但它看起来像这样:

ggplot(aes(x=date)) +
  geom_bar(data = dat2_site, aes(y=count_l_site, fill=f_grp), position="stack",stat="identity")+
  geom_line(data = dat2_site, aes(y=2*water_depth), colour = 'blue')  +
  geom_point(data = subset(dat2_site, control_previous_week=='yes'), aes(y=-5, shape = control_previous_week, colour = 'yes'), size = 3, shape = 17, colour = 'black') +
  geom_line(data = dat_twyford, aes(x = date, y=50*value, colour = value), linetype = 1)

请注意,最后一行您使用了两次颜色参数,并且在 aes() 函数中,您必须在不带引号的情况下命名要映射的变量。