2

Say I have mydf, a dataframe which is as follows:

Name Value
Mark 101
Joe 121
Bill 131

How would I go about creating a scatterplot in ggplot that takes the data in the value column (e.g., 101) and makes that number of points on a chart? Would this be a stat = that I am unfamiliar with, or would I have to structure the data such that Mark, for example, has 101 unique rows, Joe has 121, etc.?

wcbrown
  • 157
  • 7
  • Not sure I get the problem, but read about `rep` and `geom_jitter`. What is the expected output, what is x and what is y? – zx8754 Jan 13 '22 at 17:06
  • Related (for the data preparation): [Repeat rows in a data frame AND add an increment field](https://stackoverflow.com/questions/52690114/how-to-i-repeat-rows-in-a-data-frame-and-add-an-increment-field) and links therein. – Henrik Jan 13 '22 at 17:45

1 Answers1

3

Update: As suggest by Ben Bolker (many thanks) we could set the width of geom_jitter additionally we could add some colour asthetics:

df %>% 
  group_by(Name) %>% 
  complete(Value = 1:Value) %>% 
  ggplot(aes(x=Name, y=Value, colour=Name))+
  geom_jitter(width = 0.1)

OR more compact as suggested by Henrik (many thanks) using uncount:

ggplot(uncount(df, Value, .id = "y"), aes(x = Name, y = y)) + ...

enter image description here

First answer: Something like this?

library(dplyr)
library(ggplot2)
library(tidyr) # complete
df %>% 
  group_by(Name) %>% 
  complete(Value = 1:Value) %>% 
  ggplot(aes(x=Name, y=Value))+
  geom_jitter()

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66