Home > Blockchain >  R geom_point() number of points reflect value in column
R geom_point() number of points reflect value in column

Time:01-14

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.?

CodePudding user response:

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

  •  Tags:  
  • Related