首页 > 解决方案 > 修改ggplot2中的图例

问题描述

我正在寻求一些帮助来使用下面的数据修改我的情节中的图例。

dput(df)
structure(list(Week.Number = 1:16, Dist.18 = c(5331.83038, 14084.08602, 
12219.423585, 14406.407445, 5032.74848, 10820.094835, 16935.546075, 
15387.590625, 16195.21247, 20012.09881, 14057.385255, 5127.14891, 
16241.98523, 12793.21837, 10526.785375, 6014.43878), HIR.18 = c(1098.56001, 
4093.010015, 4372.84498, 4074.22002, 709.70499, 2460.04999, 5037.77501, 
5521.029965, 5463.410025, 6761.34502, 3953.20997, 1189.89, 3663.69006, 
2333.005005, 2289.38001, 1069.740005), V6.18 = c(0, 40.77, 63.505, 
112.63, 52.395, 56.795, 211.115, 75.52, 215.059995, 121.725, 
57.64, 15.35, 140.34, 15.615, 85.66, 31.815), Dist.17 = c(11820.06249, 
18123.592835, 14560.30914, 17193.56009, 7733.785765, 15536.659865, 
8694.08218, 19569.060865, 14153.71578, 18498.63446, 16452.63166, 
16820.32351, 9242.407875, 8857.62039, 2371.09375, 10340.258575
), HIR.17 = c(2693.425035, 4971.474985, 4521.895065, 5561.53997, 
1759.31996, 3924.48, 1893.485, 5571.700035, 3239.94503, 4773.02004, 
5927.174995, 4537.58996, 1618.49499, 2771.84002, 284.56, 2181.749995
), V6.17 = c(15.58, 38.355, 240.355, 354.059995, 1.76, 187.575, 
93.495, 184.925, 88.27, 165.08, 231.075, 171.09, 32.55, 93.88, 
0, 56.19)), .Names = c("Week.Number", "Dist.18", "HIR.18", "V6.18", 
"Dist.17", "HIR.17", "V6.17"), row.names = c(NA, -16L), class = "data.frame")

此代码生成绘图。

plot <- ggplot(df, aes(x = Week.Number, y = Dist.18, fill = "2018")) +
  geom_col() +
  geom_line(aes(x = Week.Number, y = Dist.17, fill = "2017"), size = 0.75) +
  geom_point(aes(x = Week.Number, y = Dist.17), size = 0.75) +
  scale_fill_manual("color", values = c("2017" = "black", "2018" = "blue")) +
  scale_x_continuous(breaks = c(1:16)) +
  ylab("Dist") +
  theme_classic() +
  theme(plot.title = element_text(face = "bold"),
        axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face = "bold"))

在此处输入图像描述

我希望将图例的标题更改为“季节”并修改密钥。我想知道是否有可能在密钥中有两个不同的点。例如,标签 2018 的蓝色实心方块和 2017 的黑线,代表图中的每个几何图形。

此外,我fill =aes()参数中使用了第一个实例来生成图例。这似乎可行,但不确定它是否是最佳实践。

希望我提供了足够的信息。任何帮助将不胜感激。谢谢你。

标签: rggplot2

解决方案


根据我上面的评论,为每种“美学”创建了一个图例 - 您目前只有填充美学。如果您想要多个图例,则需要指定几种美学,例如linetypecolor

不过,您的代码存在一些问题。

首先,为了充分利用 ggplot 的美学和分组功能,我建议将您的数据放在长格式 - 目前它是宽格式。例如,按年份分组可能有意义——您可以将属于一个测量项的所有值放在一列中,并有一列指定年份,然后为此“年份列”指定 aes。

此外,请参阅下面的评论

 ggplot(df) + 
# avoid specifying your `aes` in the ggplot main call - 
# specially if you have several plots following. 
# Some people say it's even better to leave it completely empty.
  geom_col(aes(x = Week.Number, y = Dist.18, fill = "2018")) + 
# now here you are currently not really making use of the aes-functionality, 
# because you are only creating an aesthetic for one value, i.e. '2018'
  geom_line(aes(x = Week.Number, y = Dist.17, color = "2017"), size = 0.75) + 
# Here I have changed fill to color
  geom_point(aes(x = Week.Number, y = Dist.17), size = 0.75) +
  scale_fill_manual("your title", values = c("2017" = "black", "2018" = "blue")) + 
# this is to show you that you actually already know 
# how to change your legend title - see the graph :)
  scale_x_continuous(breaks = c(1:16)) +
  ylab ("Dist") +
  theme_classic() 

在此处输入图像描述


推荐阅读