在ggplot2中修改图例

时间:2018-09-06 06:39:04

标签: r 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"))

enter image description here

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

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

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

2 个答案:

答案 0 :(得分:3)

根据我上面的评论,每个“美学”都创建了一个图例-您目前只有填充美学。如果您想使用多个图例,则需要在此处指定多种美感。 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() 

enter image description here

答案 1 :(得分:1)

我想两个传说都拥有一个标题会很好:

ggplot(df, aes(x = Week.Number)) +
  geom_col(aes(y = Dist.18, fill = "2018")) +
  geom_line(aes(y = Dist.17, col = "2017"), size = 0.75) +
  geom_point(aes(y = Dist.17, col = "2017")) +
  scale_colour_manual("Season", values = c("2017" = "black")) +
  scale_fill_manual("", values = c("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")) + 
  theme(legend.margin = margin(-0.8, 0, 0, 0, unit = "cm"))

enter image description here

如果不想在图例中指向,只需从col = "2017"中删除geom_point即可得到

enter image description here

诀窍是使用legend.margin中的theme参数删除两个图例之间的空间。