加载包
library(ggplot2) library(tidyverse)
导入数据集
mpg
创建ggplot图形
ggplot(data = mpg) ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = “blue”))
图形属性映射
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = “blue”))
分面
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(drv ~cyl)
ggplot(data = mpg) + geom_point(mapping = aes(x = drv, y = cyl))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(drv ~ .)
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(~class, nrow = 2)
几何对象
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,linetype=drv))
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy,group=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() + geom_smooth() # 与上图相同
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 = filter(mpg, class == “subcompact”),se = FALSE)
ggplot(data = mpg,mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(se = FALSE)
ggplot(data = mpg, mapping = aes(x = displ, y =hwy)) + geom_point() + geom_smooth()
ggplot() + geom_point(data = mpg,mapping = aes(x = displ, y = hwy)) + geom_smooth(data = mpg,mapping = aes(x = displ, y = hwy))
A <- diamonds
统计变换
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) + stat_count(mapping = aes(x = cut))
demo <- tribble( ~a, ~b, “bar_1”, 20, “bar_2”, 30, “bar_3”, 40 )
计数 频数
ggplot(data = demo) + geom_bar(mapping = aes(x =a, y=b), stat = “identity”)
比例 频率
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = …prop…, group = 1))
摘要
ggplot(data = diamonds) + stat_summary(mapping = aes(x = cut, y = depth), fun.ymin = min, fun.ymax = max, fun.y = median)
ggplot(data = diamonds) + stat_summary(mapping = aes(x = cut, y = depth)) # MSE 均方误差
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = …prop…)) # 全堆叠
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut,fill = color, y = …prop…)) # 等层堆叠
位置调整
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, color = cut)) # 图形上色
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut)) # 图形上色
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity)) # 映射到另一个变量
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + geom_bar(alpha = 1/5, position = “identity”) # 位置调整
ggplot(data = diamonds, mapping = aes(x = cut, color = clarity)) + geom_bar(fill = NA, position = “identity”) # 位置调整
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 = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), position = “jitter”) # 将同一点的数据追加随机抖动使其分开
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + geom_point(position = “jitter”)
坐标系
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip() # 交换x轴和y轴
coord_quickmap 绘制适合的纵横比
nz <- map_data(“nz”)
ggplot(nz, aes(long, lat, group = group)) + geom_polygon(fill = “white”, color = “black”)
ggplot(nz, aes(long, lat, group = group)) + geom_polygon(fill = “white”, color = “black”) + coord_quickmap()
coord_polar 使用极坐标
bar <- ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut), show.legend = FALSE, width = 1) + labs(x = NULL, y =NULL) bar bar + coord_flip() bar + coord_polar()
ggplot(data = mpg, mapping = aes(x = cty, y =hwy)) + geom_point() + geom_abline() + coord_fixed()