0

Let's say I have data looking like this:

type    value
A        1
A        1
A        2
A        2
A        3
B        2
B        2
B        2
B        3
C        2
C        3
C        4
C        5

How can I plot this in one graph, so I have the A, B, and C types on the x-axis, and then the corresponding y-values for each type plotted as dots? So kind of a scatter plot, but with fixed x-values.

Denver Dang
  • 2,433
  • 3
  • 38
  • 68

1 Answers1

1

Try using ggplot2. It automatically identifies categorical variables and treats them accordingly.

library(ggplot)
#say your dataframe is stored as data
ggplot(aes(x=data$type,y=data$value))+geom_point()

As Ian points out, this will indeed over plot. You can read about it here. So if you are ok with a 'small amount of random variation to the location of each point', then +geom_jitter is a useful way of handling overplotting.

honeybadger
  • 1,465
  • 1
  • 19
  • 32
  • While this will work it will over plot the data and some of the points will show as a single value. Better would be `+geom_jitter()`, `+geom_boxplot()` or `+geom_count()` – Ian Wesley Sep 29 '17 at 22:28