如何在ggplot中进行子集化?

时间:2016-05-25 15:29:17

标签: r ggplot2 subset

我想从y轴上的ggplot Heatmap中删除“No Preference”和“Uncommitted”,但是这个子集不起作用,你知道可能的原因吗?

我的代码:

ggplot(subset(primary_results, candidate != c("No Preference", 
"Uncommitted"))) + 
geom_tile(aes(y = candidate, x = state_abbreviation, fill = fraction_votes))  
+ 
scale_fill_viridis(option = "plasma") +
theme_solarized_2() +
xlab("State") +
ylab("Candidate") +
guides(fill = guide_legend(title="Fraction Votes"))

我的Heatmamp(注意,我仍然得到不需要的部分)

My Heatmamp (notice, I still get the unwanted part)

如果我在绘图之外尝试子集,它也只是不起作用..

1 个答案:

答案 0 :(得分:4)

问题是你的子集。您已删除所有不等于长度为两个字符向量c("No Preference", "Uncommitted")的候选项。

我想你想要:

ggplot(subset(primary_results, !(candidate %in% c("No Preference", "Uncommitted")))) +