【发布时间】: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)的输出,因为这是我们直接重现问题所需要的。 -
按要求编辑帖子。