Taras K - 1 month ago 9

R Question

I am trying to display some data, where I don't only need to display a point using

`geom_point`

`geom_segment`

Say I have a data like this:

`df2 <- data_frame(x = c("a", "b", "c" ,"d"), y = c(3:6))`

# A tibble: 4 × 2

x y

<chr> <int>

1 a 3

2 b 4

3 c 5

4 d 6

What I want to get is like the graph below, only having a dot in each of 4 variables between 0 and their value (with the desired points marked manually in red):

`ggplot(df2, aes(x=x)) + geom_point(aes(y=y)) + geom_point(aes(y=0))`

Answer

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 `x`

and `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.