2

I have data that have the same x and y values but are separated categorically by another variable. However, since they have the same x and y values, I only see one circle in the plot. I want to see each circle clearly separated and on top of each other for the same x/y coordinate.

I've tried using jitter and position_dodge but it doesn't separate the values clearly vertically.

library(ggplot2)
library(scales)
library("RColorBrewer")

x<- c("1","1","1","1","1","1")

y <- c("1","1","1","2","2","2")

z <- c("Treatment 1","Treatment 2","Treatment 3","Treatment 4","Treatment 5","Treatment 6")

data<- data.frame (x,y,z)

ggplot(data=data,aes (x=y, y=x))+ 
  coord_flip()+
  geom_point(data=data, aes(x=y, y= x, color = z, pch=16,size =3 ))+
  xlab("ID")+
  ylab("Time")+ 
  scale_shape_identity()+

  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))

The various treatments should be vertically separated so that we see a circle for each color rather than just one circle. Any help would be much appreciated!

1
  • This probably won't work for your data if it is more complex but if all your data has this structure, you can just add or subtract a bit from y based on the treatment. Commented Apr 5, 2019 at 19:56

2 Answers 2

2

ggbeeswarm package has some cool functions for plotting overlapped points

Beeswarm plots (aka column scatter plots or violin scatter plots) are a way of plotting points that would ordinarily overlap so that they fall next to each other instead. In addition to reducing overplotting, it helps visualize the density of the data at each point (similar to a violin plot), while still showing each data point individually.

ggbeeswarm provides two different methods to create beeswarm-style plots using ggplot2. It does this by adding two new ggplot geom objects:

  • geom_quasirandom: Uses a van der Corput sequence or Tukey texturing (Tukey and Tukey "Strips displaying empirical distributions: I. textured dot strips") to space the dots to avoid overplotting. This uses sherrillmix/vipor.

  • geom_beeswarm: Uses the beeswarm library to do point-size based offset.

library(ggplot2)
library(scales)

x <- c("1", "1", "1", "1", "1", "1")
y <- c("1", "1", "1", "2", "2", "2")
z <- c("Treatment 1", "Treatment 2", "Treatment 3", "Treatment 4", "Treatment 5", "Treatment 6")
data <- data.frame(x, y, z)

# install.packages('ggbeeswarm', dependencies = TRUE)
library(ggbeeswarm)
ggplot(data = data, aes(x = y, y = x)) +
  geom_quasirandom(aes(col = z), varwidth = TRUE, groupOnX = TRUE, alpha = 3/4, size = 2) +
  coord_flip() +
  xlab("ID") +
  ylab("Time") +
  scale_shape_identity() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

Created on 2019-04-05 by the reprex package (v0.2.1.9000)

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

3 Comments

This might be useful too
Thanks! Do you know if there's a way to increase the distance between the tick marks on the Y axis? I want to make sure that the values at one y point are obvious and clearly separate from the above y point.
@Karuno11: try to play with theme(). There are options for axis ticks and labels ggplot2.tidyverse.org/reference/theme.html
0

position_dodge works for me, but you will have to adjust the dodge width based on the size of your output.

I also simplified the code, you don't have to repeat data = data or the aes() mappings in the geom_point() layer---they will be inherited from the ggplot() call. And you shouldn't put constants like pch = 16 or size = 3 inside aes(). Leave them outside aes() and they will be treated as constants, not producing legends with 1 value and not requiring the scale.

ggplot(data=data,aes (x=y, y=x, color = z)) + 
  coord_flip() +
  geom_point(pch = 16, size = 3, position = position_dodge(width = 0.2)) +
  xlab("ID") +
  ylab("Time") + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))

enter image description here

Comments

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.