r - 填写图例、ggplot 的符号

标签 r ggplot2 legend

大家好,提前致谢,

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

enter image description here

我的代码如下:

    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")

谢谢!

最佳答案

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

顺便说一句:我还删除了第二个 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 package于2020年8月24日创建(v0.3.0)

关于r - 填写图例、ggplot 的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63567278/

相关文章:

r - 计算每秒事务数

r - 在R中找到非线性相关性

r - 轴上所有点之间的距离相等

javascript - 图表js在运行时通过按钮单击显示/隐藏图例

python - 添加覆盖 twinx 轴线条的图例

html - 向 fieldset 元素添加边框时,有没有办法获得完整的边框?

r - R 图中的 mtext()

使用浓缩列从长到宽 reshape 数据框

r - 使用 ggplot2 对齐子集数据点

r - 如何在带有条形图和点图的图中显示图例?