r - Scatter plot that shows all points with the same value -
how create scatter plot in r points shown there though have same values in categories. besides data points, have average values in each category.
for example, if have data set of 2 variables 1 of them (cotton weight percentage) factor:
dat <- structure(list(`tensile strength` = c(12l, 19l, 17l, 7l, 25l, 7l, 14l, 12l, 18l, 22l, 18l, 7l, 18l, 18l, 15l, 10l, 11l, 19l, 11l, 19l, 15l, 19l, 11l, 23l, 9l), `cotton weight percent` = c(20l, 30l, 20l, 35l, 30l, 15l, 25l, 20l, 25l, 30l, 20l, 15l, 25l, 20l, 15l, 35l, 35l, 25l, 15l, 25l, 35l, 30l, 35l, 30l, 15l)), .names = c("tensile strength", "cotton weight percent"), class = "data.frame", row.names = c(na, -25l))
how can make scatter plot one:
here, solid dots individual observations , open circles average observed tensile strengths.
this can done in ggplot2 geom_jitter
, stat_summary
. specifically, geom_jitter
give black points on graph:
library(ggplot2) ggplot(mtcars, aes(factor(cyl), mpg)) + geom_jitter(position = position_jitter(width = .1)) p
(the "jitter" add noise in terms of x-axis, occurs in example).
then stat_summary
layer lets add point average of each x value (which i've made large , red):
ggplot(mtcars, aes(factor(cyl), mpg)) + geom_jitter(position = position_jitter(width = .1)) + stat_summary(fun.y = "mean", geom = "point", color = "red", size = 3)
Comments
Post a Comment