Denver Dang
Denver Dang

Reputation: 2615

Plotting numeric y-value data with non-numeric x-values in R?

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.

Upvotes: 0

Views: 3662

Answers (1)

honeybadger
honeybadger

Reputation: 1575

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.

Upvotes: 1

Related Questions