R

R 数据可视化 —— ggplot 二维直方图和密度图

2021-04-21  本文已影响0人  名本无名

二维直方图

二维直方图用于二维数据的统计分析,X-Y 轴变量均为数值型。首先将坐标平面分割为许多大小相等的区间,并计算落在每个区间中的观察值数目,然后将观察值映射为矩形的填充色。

ggplot2 中,geom_bin2d 函数的区间形状是矩形,而 geom_hex 函数可以绘制六边形区间。

示例

1. geom_bin2d

d <- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)
d + geom_bin2d()

设置分箱数目

d + geom_bin2d(bins = 10)

设置分箱的宽度(水平和竖直)

d + geom_bin2d(binwidth = c(0.1, 0.1))

更改颜色

d + geom_bin2d(aes(fill = after_stat(count))) +
  scale_fill_gradientn(colours = rainbow(10))

设置边缘线条大小与颜色

d + geom_bin2d(size = 1, colour = "green")

2. geom_hex

geom_hexgeom_bin2d 的参数一样

d <- ggplot(diamonds, aes(x, y)) + 
  xlim(4, 10) + ylim(4, 10)
  
d + geom_hex()
d + geom_hex(size = 1, colour = "#FF9900FF")

二维密度图

前面我们提到过使用 geom_density() 函数来绘制一维密度图。

对于二维核密度估计,我们使用 geom_density_2d() 函数,以线条的方式展示,使用 geom_density_2d_filled() 以填充色的方式展示

示例

绘制分布线条

m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
 geom_point() +
 xlim(0.5, 6) +
 ylim(40, 110)
 
m + geom_density_2d()

填充色

m + geom_density_2d_filled(alpha = 0.5)

结合线条与填充色

m + geom_density_2d_filled(alpha = 0.5) +
  geom_density_2d(size = 0.25, colour = "black")

添加分组变量

d <- sample_n(diamonds, 1000) %>%
  ggplot(aes(x, y))
  
d + geom_density_2d(aes(colour = cut))

进行分面

d + geom_density_2d_filled() + 
  facet_wrap(vars(cut))

将观测值的数量映射到颜色强度

d + geom_density_2d_filled(contour_var = "count") + 
  facet_wrap(vars(cut))

我们也可以使用前一节提到的色块图函数,如 raster 对象

d + stat_density_2d(
  geom = "raster",
  aes(fill = after_stat(density)),
  contour = FALSE
) + scale_fill_viridis_c()

或者其他对象,如散点图

d + stat_density_2d(geom = "point", aes(size = after_stat(density)), 
                    n = 20, contour = FALSE)

polygon 对象

d + stat_density_2d(
  geom = "polygon",
  aes(fill = after_stat(level)),
  bins = 30
) + scale_fill_viridis_c()

组合分布图

我们可以将二维直方图和密度图,与一维统计分布图结合起来,更加详细的展示数据的分布情况

首先,构造正态分布数据

library(cowplot)

N <- 500

df <- tibble(
  x1 = rnorm(n = N, mean = 2),
  x2 = rnorm(n = N, mean = 2),
  
  y1 = rnorm(n = N, mean = 2),
  y2 = rnorm(n = N, mean = 2)
)

绘制边缘即组合直方图

top_hist <- ggplot(df, aes(x1)) +
  geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  theme_void()

right_hist <- ggplot(df, aes(x2)) +
  geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  coord_flip() +
  theme_void()

center <- ggplot(df, aes(x1, x2)) +
  geom_hex(colour = "black") +
  scale_fill_gradientn(colours = rainbow(10)) +
  theme(
    panel.background=element_rect(fill="white",colour="black",size=0.25),
    axis.line=element_line(colour="black",size=0.25),
    axis.title=element_text(size=13,face="plain",color="black"),
    axis.text = element_text(size=12,face="plain",color="black"),
    legend.position=c(0.10,0.80),
    legend.background=element_blank()
  )

p1 <- plot_grid(top_hist, center, align = "v", 
          nrow = 2, rel_heights = c(1, 4))

p2 <- plot_grid(NULL, right_hist, align = "v",
          nrow = 2, rel_heights = c(1, 4))

plot_grid(p1, p2, ncol = 2, 
          rel_widths = c(4, 1))

glist <- list(top_hist, center, right_hist)

绘制二维密度图的代码类似

top_hist <- ggplot(df, aes(y1)) +
  # geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  geom_density(fill = "#1f78b4", colour = "black") +
  theme_void()

right_hist <- ggplot(df, aes(y2)) +
  # geom_histogram(bins = 35, fill = "#1f78b4", colour = "black") +
  geom_density(fill = "#1f78b4", colour = "black") +
  coord_flip() +
  theme_void()

center <- ggplot(df, aes(y1, y2)) +
  geom_density2d(colour = "black") +
  geom_density2d_filled() +
  scale_fill_brewer(palette = "Set2") +
  theme(
    panel.background=element_rect(fill="white",colour="black",size=0.25),
    axis.line=element_line(colour="black",size=0.25),
    axis.title=element_text(size=13,face="plain",color="black"),
    axis.text = element_text(size=12,face="plain",color="black"),
    legend.position = "none"
  )

p1 <- plot_grid(top_hist, center, align = "v", 
          nrow = 2, rel_heights = c(1, 4))

p2 <- plot_grid(NULL, right_hist, align = "v",
          nrow = 2, rel_heights = c(1, 4))

plot_grid(p1, p2, ncol = 2, 
          rel_widths = c(4, 1))

glist <- list(top_hist, center, right_hist)

代码:https://github.com/dxsbiocc/learn/blob/main/R/plot/hist_density_2d.R

上一篇 下一篇

猜你喜欢

热点阅读