可视化:RStudio ggplot2绘图系统讲解(二)

355 阅读3分钟

本文已参加「新人创作礼」活动,一起开启掘金创作之路。

这次带来的是 RStudio 的 ggplot2 绘图系统的讲解,超详细!!!

文中提到的代码在导入 ggplot2tidyverse 包后,均可直接运行,所有数据集均为 R 自带数据集。

文末还为大家准备了习题和讲解哦,感兴趣的大伙们可以做一做呀!

ggplot2 绘图系统:分面

有时我们需要基于数据的各个子集上画出同类图形, 以便于观察和展示各个子集的异同点, 分面 facet 将这种工作变得很简单.

  • facet_grid 网格化的分面
  • facet_wrap 按行排列的分面
library(ggplot2)
str(diamonds)
dsmall = diamonds[sample(nrow(diamonds),1000),]
p = ggplot(dsmall, aes(x = carat, y = price)) +
  geom_point()
p
p + facet_grid(cut ~ .)
p + facet_grid(.~ cut)
p + facet_grid(color ~ cut)


p + facet_wrap(cut ~.)
p + facet_wrap(.~ cut )

p + facet_wrap(.~ cut, ncol=2)
p + facet_wrap(.~ cut, ncol=3)

ggplot2 绘图系统:标注

我们经常需要在图像上标注一些信息, 这些信息经常可以通过一些字符或者额外的几何图形来展示。annotate 和 geom_text 可用于完成这项任务。

  • text
  • math formula
  • region
  • line
  • arrows, segment
p + annotate("text", x = 0.5, y = 5000, label = "small diamonds")
p + annotate("text", x = 0.5, y = 5000, label = "small diamonds", color = "blue")
p + 
  annotate("text", x = 0.5, y = 5000, label = "small \n diamonds", color = "blue", size = 5) +
  annotate("text", x = 2, y = 16000, label = "large \n diamonds", color = "red", size = 5)

p + annotate("rect", xmin = 1, xmax = 1.5, ymin = 0, ymax = 19000,
             fill = "blue", alpha = 0.1)

p + annotate("rect", xmin = 1, xmax = 1.5, ymin = -Inf, ymax = Inf,
             fill = "purple", alpha = 0.1)

p + geom_abline(intercept = 0, slope = 5000)
p + geom_vline(xintercept = 1, color = "blue")
p + geom_hline(yintercept = 10000, color = "orange")

ggplot2 绘图系统:坐标变换

dsmall %>%
  ggplot(aes(x = carat, y = price, group = color, color = color)) +
  geom_point()

dsmall %>%
  ggplot(aes(x = carat, y = price, group = color, color = color)) +
  geom_point() +
  coord_flip()

dsmall %>%
  ggplot(aes(x = carat, y = price, group = color, color = color)) +
  geom_point() +
  coord_polar(theta = "x")

dsmall %>%
  ggplot(aes(x = carat, y = price, group = color, color = color)) +
  geom_point() +
  coord_trans(y = "log")

ggplot(data = mtcars, mapping = aes(x = factor(cyl), fill = factor(cyl))) + 
  geom_bar()

ggplot(data = mtcars, mapping = aes(x = factor(cyl), fill = factor(cyl))) + 
  geom_bar() +
  coord_polar(theta = "x")


ggplot(data = mtcars, mapping = aes(x = 1, fill = factor(cyl))) + 
  geom_bar(position = "fill")

ggplot(data = mtcars, mapping = aes(x = 1, fill = factor(cyl))) + 
  geom_bar(position = "fill") +
  coord_polar(theta = "y")

ggplot2 绘图系统:其它常用图形

ggplot(dsmall, aes(x = carat)) + geom_histogram()
ggplot(dsmall, aes(x = carat)) + geom_density()
ggplot(dsmall, aes(x = carat)) + geom_line(stat = "density")

ggplot(dsmall, aes(x = carat, color = cut)) + geom_line(stat = "density")


ggplot(dsmall, aes(x = carat)) + geom_area(stat = "count")

ggplot(dsmall, aes(x = carat)) + stat_bin(geom = "area", bins = 30)
ggplot(dsmall, aes(x = carat, fill = cut)) + 
  stat_bin(geom = "area", bins = 50, position = "stack") +
  facet_wrap(~cut)

ggplot(dsmall, aes(x = carat, fill = cut)) + 
  stat_bin(geom = "area", bins = 50, position = "stack")

习题

在掌握了以上知识后,并结合 可视化:RStudio ggplot2绘图系统讲解(一) 所学到的,我们可以轻松复现以下图形:

这里应用的是自带的 diamonds 数据集。

image.png

image.png

image.png

image.png

习题答案讲解

下面是这四副图的相关代码,大家可以如果做完了的话可以看一看,有什么问题可以及时提出来哦!

# 导入必要包:
library(tidyverse)
library(ggplot2)

# 数据预处理
dsmall = diamonds[sample(nrow(diamonds), 1000),]

# 图一:
ggplot(dsmall, aes(x = carat, y = price, color = cut)) +
  geom_point() -> p
p+facet_wrap(cut ~ .)

# 图二:
ggplot(dsmall, aes(x = carat, y = price)) +
  geom_point() -> p
p+annotate('text', x = 2.75, y = 7500, label = 'Diamonds \n heavier \n than 3 carat', color = 'red', size = 6) +
  annotate('rect', xmin = 2, xmax = 3.5, ymin = 0, ymax = 20000, fill = 'red', alpha = 0.1)

# 图三:
ggplot(dsmall, aes(x = price / carat, fill = cut)) +
  stat_bin(geom = 'area', bins = 40, position = 'stack')

# 图四:
dsmall %>%
  mutate(density = carat / (x * y * z)) -> dsmall2
ggplot(dsmall2, aes(x = price / carat, fill = cut)) +
  stat_density(geom = 'area', bins = 40, position = 'stack')