Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scatterplot with no x variable

Tags:

r

ggplot2

My data set has a response variable and a 2-level factor explanatory variable. Is there a function for creating a scatter plot with no x axis variable? I'd like the variables to be randomly spread out along the x axis to make them easier to see and differentiate the 2 groups by color. I'm able to create a plot by creating an "ID" variable, but I'm wondering if it's possible to do it without it? The "ID" variable is causing problems when I try to add + facet_grid(. ~ other.var) to view the same plot broken out by another factor variable.

#Create dummy data set
response <- runif(500)
group <- c(rep('group1',250), rep('group2',250))
ID <- c(seq(from=1, to=499, by=2), seq(from=2, to=500, by=2))
data <- data.frame(ID, group, response)

#plot results
ggplot() +
    geom_point(data=data, aes(x=ID, y=response, color=group)) 

desired plot

like image 978
heyydrien Avatar asked Mar 10 '23 15:03

heyydrien


2 Answers

How about using geom_jitter, setting the x axis to some fixed value?

ggplot() +
    geom_jitter(data=data, aes(x=1, y=response, color=group)) 

enter image description here

like image 182
aosmith Avatar answered Mar 20 '23 15:03

aosmith


You could plot x as the row number?

ggplot() +
    geom_point(data=data, aes(x=1:nrow(data), y=response, color=group)) 

enter image description here

Or randomly order it first?

RandomOrder <- sample(1:nrow(data), nrow(data))
ggplot() +
    geom_point(data=data, aes(x= RandomOrder, y=response, color=group)) 

enter image description here

like image 24
William Avatar answered Mar 20 '23 14:03

William