首页 > 解决方案 > 为图例填充符号,ggplot

问题描述

大家好,提前感谢,

我试图让我的图例显示填充形状,而不是简单地用颜色勾勒(如下所示):

在此处输入图像描述

我的代码如下:

    p.ch4 <- ggplot(ch4.data, aes(x=date, y=value, group = type, 
                            fill=type, color=type)) +
  geom_line() +
  geom_point(aes(shape=type), color = "black", size =4) +
  geom_point(aes(shape=type, color=type), size=3) +
  scale_shape_manual(values = c(21:24)) +
  scale_fill_manual(values = c("darkorchid3","indianred3","dodgerblue3")) +
  scale_color_manual(values = c("darkorchid3","indianred3","dodgerblue3")) +
  annotate("rect", xmin=as.POSIXct("2017-11-09"), xmax=as.POSIXct("2018-04-09"),ymin=0,ymax=75,alpha=.25) +
  scale_y_continuous(sec.axis = sec_axis(~., name = expression(paste('Methane (', mu, 'M)')))) +
  labs(y = expression(paste('Methane (', mu, 'M)')), x = "", color = "", shape = "") +
  theme_linedraw(base_size = 18) +
  #theme(legend.position = "none") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        strip.text = element_text(face = "bold")) +
  theme(axis.text = element_text(angle = 45, hjust = 1))

print(p.ch4)

我意识到我在数据点颜色、轮廓和形状方面做了很多工作,我想知道这是否是我的问题所在?

为了重现性,这是我的数据集“ch4.data”:

> dput(ch4.data)
structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("SWI", 
"4cm", "12cm"), class = "factor"), date = structure(c(1537167600, 
1535266800, 1533279600, 1531292400, 1529305200, 1527318000, 1526713200, 
1527231600, 1525762800, 1524294000, 1522825200, 1521356400, 1516089600, 
1511596800, 1509606000, 1537340400, 1534575600, 1531119600, 1530255600, 
1527663600, 1525071600, 1517212800, 1513756800, 1510300800, 1537167600, 
1535094000, 1533020400, 1530946800, 1528873200, 1528441200, 1526713200, 
1524898800, 1522393200, 1519545600, 1516435200, 1514361600, 1510992000
), class = c("POSIXct", "POSIXt"), tzone = ""), value = c(52.43, 
40.37, 12.92, 5.12, 4.93, 2.4, 1.97, 3.07, 5.7, 3.56, 2.74, 0.67, 
0.78, 0.48, 0.7, 0.3, 0.21, 4.11, 1.54, 4.86, 7.42, 3.62, 0.81, 
0.37, 6.2, 9.14, 34.11, 57.83, 63.02, 63.56, 54.13, 42.92, 32.59, 
23.23, 9.97, 5.62, 0.52), geochem = c("ch4", "ch4", "ch4", "ch4", 
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", 
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", 
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", "ch4", 
"ch4", "ch4", "ch4", "ch4", "ch4", "ch4")), row.names = c(NA, 
-37L), class = "data.frame")

谢谢!

标签: rggplot2legend

解决方案


这可以像这样实现。重要的一步是为所有三个比例赋予相同的名称,以便将图例合并为一个:

顺便说一句:我还删除了第二个 geom_point 图层,只是添加了黑色作为参数。

编辑:正如@chemdork123 在他的评论中正确指出的那样,通过为labs()语句中的所有三个比例设置相同的标签或删除空的,即和比例""的标签,可以更容易地解决这个问题。colorshape

library(ggplot2)

ggplot(ch4.data, aes(x=date, y=value)) +
  geom_line(aes(color=type)) +
  geom_point(aes(shape=type, fill=type), color = "black", size=4) +
  scale_shape_manual(name = "type", values = c(21:23)) +
  scale_fill_manual(name = "type", values = c("darkorchid3","indianred3","dodgerblue3")) +
  scale_color_manual(name = "type", values = c("darkorchid3","indianred3","dodgerblue3")) +
  annotate("rect", xmin=as.POSIXct("2017-11-09"), xmax=as.POSIXct("2018-04-09"),ymin=0,ymax=75,alpha=.25) +
  scale_y_continuous(sec.axis = sec_axis(~., name = expression(paste('Methane (', mu, 'M)')))) +
  labs(y = expression(paste('Methane (', mu, 'M)')), x = "", color = "", shape = "") +
  theme_linedraw(base_size = 18) +
  #theme(legend.position = "none") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        strip.text = element_text(face = "bold")) +
  theme(axis.text = element_text(angle = 45, hjust = 1))

reprex 包(v0.3.0)于 2020 年 8 月 24 日创建


推荐阅读