Laura Jonutytė
Laura Jonutytė

Reputation: 59

How to subset in ggplot?

I want to remove "No Preference" and "Uncommitted" from my ggplot Heatmap on the y axis, but the subset just doesn't work, do you know the possible reasons ?

My code:

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"))

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

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

If I try the subset outside the plot, it also just doesn't work..

Upvotes: 2

Views: 3262

Answers (1)

Mhairi McNeill
Mhairi McNeill

Reputation: 1981

The problem is your subset. You have removed all candidates not equal to the length two character vector c("No Preference", "Uncommitted").

I think you want:

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

Upvotes: 4

Related Questions