扩展笔记 Top50 ggplot2 图库

Marginal Histogram - Boxplot

围绕图形示例、绘制逻辑与适用场景,便于快速迁移到自己的分析任务。

R训练营主题笔记

围绕图形类型整理适用场景、绘图思路与示例代码,便于快速迁移到自己的数据。

Marginal Histogram / Boxplot

适用于展示两个变量间相关性,同时展现了相关性中每一个点上两个变量的具体情况

# load package and data
library(ggplot2)
library(ggExtra)
data(mpg, package="ggplot2")
# mpg <- read.csv("<http://goo.gl/uEeRGu>")

# Scatterplot
theme_set(theme_bw())  # pre-set the bw theme.
mpg_select <- mpg[mpg$hwy >= 35 & mpg$cty > 27, ]
g <- ggplot(mpg, aes(cty, hwy)) +
  geom_count() + #防止重叠点的函数
  geom_smooth(method="lm", se=F)#拟合曲线

ggMarginal(g, type = "histogram", fill="transparent")
ggMarginal(g, type = "boxplot", fill="transparent")
# ggMarginal(g, type = "density", fill="transparent")
#第一图层(设置变量,同时如果有一些图外元素,也可以在这里一起设置,也就是全局映射)
ggplot(mpg, aes(cty, hwy))

#第二图层(点图)(这里设置的为防止重叠的点图)
geom_count()

#第三图层(添加拟合曲线)
geom_smooth(method="lm", se=F)

#第四图层(这里我们添加另一个可视化来帮助我们更好的展示两个变量)
ggMarginal(g, type = "histogram", fill="transparent")#直线图
ggMarginal(g, type = "boxplot", fill="transparent")#箱线图
ggMarginal(g, type = "density", fill="transparent")#密度图
ggMarginal(g, type = "violin", fill="transparent")#小提琴图
#直线图
# load package and data
library(ggplot2)
library(ggExtra)
data(mpg, package="ggplot2")
# mpg <- read.csv("<http://goo.gl/uEeRGu>")

# Scatterplot
theme_set(theme_bw())  # pre-set the bw theme.
mpg_select <- mpg[mpg$hwy >= 35 & mpg$cty > 27, ]
mpg_select
g <- ggplot(mpg, aes(cty, hwy)) +
  geom_count() +
  geom_smooth(method="lm", se=F)

ggMarginal(g, type = "histogram", fill="transparent")
#箱线图
# load package and data
library(ggplot2)
library(ggExtra)
data(mpg, package="ggplot2")
# mpg <- read.csv("<http://goo.gl/uEeRGu>")

# Scatterplot
theme_set(theme_bw())  # pre-set the bw theme.
mpg_select <- mpg[mpg$hwy >= 35 & mpg$cty > 27, ]
mpg_select
g <- ggplot(mpg, aes(cty, hwy)) +
  geom_count() +
  geom_smooth(method="lm", se=F)

ggMarginal(g, type = "boxplot", fill="transparent")
← 返回训练营笔记库 去看单细胞模块 →