I'm trying to plot a subset of my data over the full dataset, like this:
As soon as the subset contains more than one row, the size of the subset tiles doesn't match the size of the full dataset tiles.
df <- data.frame(
position = 1:10,
year = 2000,
id = c("D", "A", "C", "A", "A", "B", "A", "C", "B", "A")
)
ggplot(df, aes(x = factor(year), y = position)) +
geom_tile() +
geom_tile(data = subset(df, id == "B"), fill = "red")
In my actual, much larger dataset, I've used the same code to display other subsets with no issue (i.e., all the tiles are the same size). The resizing problem seems to appear when the subset contains a small number of items.
Is it a scale issue? I've tried manually setting width and height within geom_tile but that hasn't appeared to have any effect.
I'm sure there are more elegant ggplot
answers, but I have done similar things in the past by simply using an ifelse
statement as the fill
and passing along scale_fill_identity()
- so you only need to call geom_tile
once:
ggplot(df, aes(x = factor(year), y = position, fill = ifelse(id == "B", "red", "grey"))) +
geom_tile() +
scale_fill_identity()