2

I'm trying to show a number of dots per id equal to count, split by name in the following dataframe:

df <- data.frame(name = c("name1", "name1", "name1", "name1", "name2", "name2", "name2"),
                 id = c(0, 1, 2, 3, 0, 1, 2),
                 count = c(2, 4, 3, 2, 2, 2, 3))

What I currently have is this.

ggplot(data = df, aes(x = name, y = id)) +
  geom_dotplot(stackdir = "center", binaxis = 'y', dotsize = 0.5, binwidth = 1) +
  scale_y_continuous(breaks = seq(0, 3, 1), minor_breaks = seq(0, 3, 1))

enter image description here

However, this does not seem to show me all the dots per id (it only shows me 1 dot for each id, even though id 0 for name1 has a count of 2).

How would I go about fixing this?

2
  • What is your expected output? Are you expecting to see 2 dots for (name1, id1) and 4 dots for (name1, id2)? or are you expecting to see (name1, id2) dot twice the area of (name1, id1) dot? Do you have to use geom_dotplot? Commented May 9, 2020 at 11:03
  • @Peter I would like to see 1 dot for each count. So for name = name1 and id = 0 I would like to see 2 dots (which is how I expect a dotplot to work. And yes, I need it to be in geom_dotplot() Commented May 9, 2020 at 11:04

1 Answer 1

3

I don't know of a way to pass summarised data to geom_dotplot(). Instead, you can uncount() it first:

library(ggplot2)
library(tidyr)

df <- data.frame(name = c("name1", "name1", "name1", "name1", "name2", "name2", "name2"),
                 id = c(0, 1, 2, 3, 0, 1, 2),
                 count = c(2, 4, 3, 2, 2, 2, 3)) %>%
  uncount(count)

ggplot(data = df, aes(x = name, y = id)) +
  geom_dotplot(stackdir = "center", binaxis = 'y', dotsize = 0.5, binwidth = 1) +
  scale_y_continuous(breaks = seq(0, 3, 1), minor_breaks = NULL)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

The uncount really did the trick. I guess I was wrong to assume I could give a count to create the dots. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.