面向 SAS 用户的 R
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University

ggplot2 是 R 的强大绘图包ggplot 中的 "GG" 指的是 "grammar of graphics"ggplot2 采用分层绘图方式# Create plot for x=sex and y=diameter
ggplot(data = abalone, aes(sex, diameter))
ggplot() 定义基础图层data = abalone将 aes 设为 sex 和 diameter
目前还没有添加几何对象
sexdiameter
# Add boxplot geometric object or geom
ggplot(data = abalone,
aes(sex, diameter)) +
geom_boxplot()
+ 添加图层添加箱线图 geom_boxplot()
得到一组箱线图
F 雌性,I 幼体,M 雄性
# Add black white theme
ggplot(data = abalone,
aes(sex, diameter)) +
geom_boxplot() +
theme_bw()
theme_bw() 添加主题图层
# Change to geom_violin()
ggplot(data = abalone,
aes(sex, diameter)) +
geom_violin() +
theme_bw()
geom_violin 替换 geom_boxplot
# Make histogram of shuckedWeight
ggplot(abalone, aes(shuckedWeight)) +
geom_histogram()
geom_histogram()aes() 设为 shuckedWeight
# Make lines black and fill light blue
ggplot(abalone, aes(shuckedWeight)) +
geom_histogram(color = "black",
fill = "lightblue")
colorfill 颜色() 内
# Add x, y axis labels and title
ggplot(abalone, aes(shuckedWeight)) +
geom_histogram(color = "black",
fill = "lightblue") +
xlab("Shucked Weight") +
ylab("Frequency Counts") +
ggtitle("Shucked Weights Histogram")
xlab() 和 ylab()ggtitle()
# Make scatterplot with geom_point()
ggplot(abalone,
aes(rings, shellWeight)) +
geom_point()
aes 需要两个变量geom_point() 添加点
# Add smoothed fit line
ggplot(abalone,
aes(rings, shellWeight)) +
geom_point() +
geom_smooth()
geom_smooth() 拟合线
# Add panels using facet_wrap()
ggplot(abalone,
aes(rings, shellWeight)) +
geom_point() +
geom_smooth() +
facet_wrap(vars(sex))
facet_wrap() 图层vars(sex) 指定分面变量
ggplot2 绘图基础面向 SAS 用户的 R