I am trying to display some data, where I don't only need to display a point using
df2 <- data_frame(x = c("a", "b", "c" ,"d"), y = c(3:6))
# A tibble: 4 × 2
1 a 3
2 b 4
3 c 5
4 d 6
ggplot(df2, aes(x=x)) + geom_point(aes(y=y)) + geom_point(aes(y=0))
This works... you could wrap it up in a function to make it more generalizable if needed.
First we use
expand.grid to create all combinations of
1:(max(y) - 1), join it to the original data, and filter out the unnecessary ones.
library(dplyr) df3 = left_join(expand.grid(x = unique(df2$x), i = 1:max(df2$y - 1)), df2) %>% filter(i < y)
Once the data is constructed, the plotting is easy:
ggplot(df2, aes(x=x)) + geom_point(aes(y=y)) + geom_point(y = 0) + geom_point(data = df3, aes(y = i), color = "red") + expand_limits(y = 0)
I'm not sure if you actually want the dots to be red - if you want them to all look the same then you could use
1:max(df2$y) (omit the
-1) and use
<= in the filter to and then only use the resulting data frame.