简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM