我对ggplot
与scale_fill_manual
的结果相矛盾,感到困惑。
使用下面的代码,我打算在变量heat
为-1时绘制红色图块,在为0时绘制灰色图块,在为1时绘制绿色图块。此代码嵌套在循环中并且压倒性地生成预期的图表。由于某些未知的原因(至少对我来说),有一些迭代会产生颜色与数据相矛盾的图形。
下面的数据1应该会产生一个带有两个绿色瓷砖的图形(其余为灰色) - 但是我会得到压倒性的红色瓷砖(应该是灰色)和两个灰色瓷砖(应该是绿色)。热值为-1的数据没有观察到,但几乎所有的瓷砖都是红色的(值为0的那些)
为了对比起见,我还提供了一个产生正确结果的例子(数据2和最底层的图2)。
这是与此https://github.com/hadley/ggplot2/issues/384相关的错误吗?代码有问题吗?或者我错过了什么?
代码
comp.plot <- ggplot(df, aes(y=variable, x=as.factor(as.character(year)), fill=as.factor(heat)))+
geom_tile()+
ggtitle(paste("Difference"))+
theme(plot.title=element_text(face="bold"),
legend.position="bottom",
legend.title=element_text(size=7),
legend.text=element_text(size=5),
legend.box="vertical",
axis.title.x = element_blank(),
axis.text.x = element_text(angle=90, size=6),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
guides(fill=guide_legend(title.position="top",
keywidth=0.5, keyheight=0.5))+
scale_fill_manual(name="Promise vs Practice",
breaks=c(-1,0,1),
labels=c("No Practice","No Promise","Practice"),
drop=FALSE,
values=c("darkred","lightgrey","darkgreen"))
数据1
df <– as.data.frame(structure(list(variable = structure(c(11L, 14L, 12L, 13L, 4L,
3L, 2L, 1L, 16L, 15L, 8L, 6L, 7L, 9L, 5L, 10L), .Label = c("eps_commission",
"eps_company", "mps_armyint", "mps_milcmd", "other_constitution",
"other_parlelect", "other_preselect", "other_proprep", "other_referendum",
"other_unresolved", "pps_cabinet", "pps_nsencabinet", "pps_parlquota",
"pps_sencabinet", "tps_autonomy", "tps_devolution"), class = "factor"),
year = c(2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006,
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006), heat = c(0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0)), .Names = c("variable",
"year", "heat"), class = "data.frame", row.names = c(7L, 86L,
165L, 244L, 323L, 402L, 481L, 560L, 639L, 718L, 797L, 876L, 955L,
1034L, 1113L, 1192L)))
图1
数据2
df2 <– as.data.frame(structure(list(variable = structure(c(11L, 14L, 12L, 13L, 4L,
3L, 2L, 1L, 16L, 15L, 8L, 6L, 7L, 9L, 5L, 10L), .Label = c("eps_commission",
"eps_company", "mps_armyint", "mps_milcmd", "other_constitution",
"other_parlelect", "other_preselect", "other_proprep", "other_referendum",
"other_unresolved", "pps_cabinet", "pps_nsencabinet", "pps_parlquota",
"pps_sencabinet", "tps_autonomy", "tps_devolution"), class = "factor"),
year = c(1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999,
1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999), heat = c(1,
1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 1)), .Names = c("variable",
"year", "heat"), class = "data.frame", row.names = c(11L, 90L,
169L, 248L, 327L, 406L, 485L, 564L, 643L, 722L, 801L, 880L, 959L,
1038L, 1117L, 1196L)))
图2
答案 0 :(得分:1)
您制作data.frame的代码对我不起作用,但我想我可能会看到问题所在。当你在data.frame df中检查变量“heat”的类时,它是数字还是因子?我打赌它是数字的。当我创建一个类似于您列出的示例data.frame并将“heat”变量保留为数字数据时,该图形看起来就像您发布的那个。但是,当我这样做时:
df$heat <- factor(df$heat, levels = c(-1, 0, 1))
然后运行用于创建图形的片段,值似乎正确映射,即“0”值为灰色,“1”值为绿色。我认为ggplot2将遇到的最低值映射到scale_fill_manual值中列出的第一个颜色。