一张统计图形就是从数据到几何对象(geometric object,缩写为geom,包括点、线、条形等)的图形属性(aesthetic attributes,缩写为aes,包括颜色、形状、大小等)的一个映射。
此外,图形中还可能包含数据的统计变换(statistical transformation,缩写为stats),最后绘制在某个特定的坐标系(coordinate system,缩写为coord)中,而分面则可以用来生成数据不同子集的图形。总而言之,一张统计图形就是由上述这些独立的图形部件所组成的。
1. 数据映射
library(ggplot2)
library(dplyr)
data(diamonds)
set.seed(1234)
diamond <- diamonds[sample(nrow(diamonds), 2000), ]
head(diamond)
### aes() 数据中的变量映射到几何图形的视觉特性
p <- ggplot(data = diamond, mapping = aes(x = carat, y = price))
p+geom_point() #绘制点图
ggplot(diamond, aes(carat,price))+geom_point()
# 设置点的形状和颜色
p <- ggplot(data=diamonds, mapping=aes(x=carat, y=price,
shape=cut,colour=color))
p+geom_point()
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, colour = displ < 5))
# 设置分组
p <- ggplot(data=diamonds, mapping=aes(x=carat, y=price, group=factor(cut)))
p+geom_boxplot()
# 控制点的透明度, 一般是连续值
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = cty))
2. 几何对象
# geom_point() #绘制点图
# geom_boxplot() #箱式图
# geom_histogram() # 直方图
# geom_bar() # 柱状图
# geom_smooth()
#绘制点图
p <- ggplot(data=diamond, mapping=aes(x=carat, y=price,
shape=cut,colour=color))
p+geom_point()
ggplot(mpg, aes(displ, hwy)) + geom_point()
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
# 趋势图
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy,
linetype = drv))
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE)
# 点图加趋势图
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth()
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = dplyr::filter(mpg, class == "subcompact"), se = FALSE)
# se:Display confidence interval around smooth? TRUE by default.
# 柱状图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
# 重写stat,根据值(如1610)来作图
demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)
ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = "identity")
# 分组统计图
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.min = min,
fun.max = max,
fun = median
)
# position adjustment
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, colour = cut)) # 改变外框颜色
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut)) # 改变填充颜色
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity)) # 按另一变量比例加颜色
# 默认:position = "stack"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) +
geom_bar(alpha = 1/5,position = "identity")
ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = "identity")
# 加少量随机噪声使点分散开来,防止overplotting
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")
3. 坐标轴
#设置横纵坐标的范围
p1 <- ggplot(mpg, aes(displ, hwy)) +
geom_point()
p1 + scale_x_continuous(limits = c(2, 6))+
scale_y_continuous(limits = c(0, 40))
p1 + scale_x_continuous(
breaks = c(2, 4, 6),
label = c("two", "four", "six")
)
# 横纵坐标轴交换
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
# 转化为极坐标
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_polar()
4. 分面
#分面(Facet)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
# ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy)) +
# facet_grid(~ cyl)
#
# ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy)) +
# facet_wrap(.~ cyl,nrow=1)
# facet变量为行排列
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ .)
# facet变量为列排列
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(.~ drv)
5. 主题
### 主题(Theme) theme
# theme_gray() # 默认
# theme_bw()
# theme_linedraw()
# theme_light()
# theme_dark()
# theme_minimal()
# theme_void()
# theme_test() #常用于学术期刊
# theme_classic()#常用于学术期刊
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_classic() +
labs(title = "classic theme")+ theme(legend.position = "none")
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_linedraw() +
labs(title = "theme_linedraw",tag = "A")+ theme(legend.position = "top")
更多作图方法请参考:
https://www.rdocumentation.org/packages/ggplot2/versions/3.3.5
https://r4ds.had.co.nz/data-visualisation.html#the-mpg-data-frame