【问题标题】:GGplot scale_fill_discrete not creating a legendGGplot scale_fill_discrete 不创建图例
【发布时间】:2018-04-12 07:57:46
【问题描述】:

我在之前的 stackoverflow 帖子中使用了该代码,该代码以前使我得到了我想要的带有图例的图形,但是现在我使用的是完全相同的代码,但我的条形图上没有图例。

dput(year.dat2)
structure(list(year = structure(c(1136044800, 1167577200, 1199113200, 
1230735600, 1262275200, 1136044800, 1167577200, 1199113200, 1230735600, 
1262275200, 1136044800, 1167577200, 1199113200, 1230735600, 1262275200
), class = c("POSIXct", "POSIXt"), tzone = ""), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("SM1", 
"SM2", "SM3"), class = "factor"), value = c(1.24863586758821, 
1.23185757914453, 1.10997352162401, 1.13683917747257, 0.987520605867152, 
2.21498726809749, 1.6378992693761, 1.25635623380691, 1.13585705516765, 
1.10169569342842, 7.40955802109858, 5.7940698875978, 6.03438772314438, 
6.82271157830123, 7.24402375195127)), row.names = c(NA, -15L), .Names = 
c("year", 
"variable", "value"), class = "data.frame")

ggplot(year.dat2, aes(x = year, y = value, fill = factor(variable))) +
  geom_bar(stat = "identity", position = "dodge")+
  scale_fill_discrete(name = "variable",
                      breaks = c(1, 2, 3),
                      labels = c("SM1", "SM2", "SM3")) +
  xlab("year") + 
  ylab("yearly Sub Mean")

结果图:

【问题讨论】:

  • 在这种情况下,没有必要在数据清理步骤中混淆您的问题,因为那里不会发生错误。相反,问题出现在数据year.dat2 的绘图上。我认为您可以删除所有cleanData 步骤,而是粘贴dput(year.dat2) 的输出,因为这是我们直接重现问题所需要的。
  • 按要求编辑帖子。

标签: r ggplot2


【解决方案1】:

删除scale_fill_discrete 中的breaks,它们与用作填充aes 的因子variable 数据的值不对应。

这是你想要的代码:

ggplot(year.dat2, aes(x = year, y = value, fill = factor(variable))) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_discrete(name = "variable",
                  labels = c("SM1", "SM2", "SM3")) +
  xlab("year") + 
  ylab("yearly Sub Mean")

注意 1:您甚至不需要 labels 参数,因为您没有重命名 variable 类别。 scale_fill_discrete(name = "variable") 就足够了,或者labs(fill="variable") 是您更改图例标题所需的全部内容。

注意 2:在您的原始帖子中,您链接到了这个 SO 问题:How to get a barplot with several variables side by side grouped by a factor

这里,示例代码scale_fill_discrete(name="Gender", breaks=c(1, 2), labels=c("Male", "Female"))中的breaks实际上引用了原始df中gender的值。同时labels用于将图例中的1重命名为Male,将2重命名为Female

  Group.1      tea     coke     beer    water gender
1       1 87.70171 27.24834 24.27099 37.24007      1
2       2 24.73330 25.27344 25.64657 24.34669      2

另一方面,您的variable 数据中没有与您在原始代码中设置的breaks 匹配的1,2,3 值,这就是未绘制图例的原因。

为了好玩,这里有一个示例,您可以如何在数据集中使用 breakslabels

ggplot(year.dat2, aes(x = year, y = value, fill = factor(variable))) +
  geom_bar(stat = "identity", position = "dodge")+
  scale_fill_discrete(name = "variable",
                  breaks = c("SM2", "SM3", "SM1"),
                  labels = c("SM2 new label", "SM3 new label", "SM1 new label")) +
  xlab("year") + 
  ylab("yearly Sub Mean")

【讨论】:

  • 非常感谢!这让我今天发疯了哈哈。
  • @TimHowat 如果答案解决了您的问题,请接受答案以将其标记为已回答。
猜你喜欢
  • 2015-09-12
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 2014-09-30
  • 2019-03-07
  • 1970-01-01
  • 2021-02-02
相关资源
最近更新 更多