ggplot2 核密度图和直方图

2020-09-15 15:42:09 浏览数 (2)

密度图和直方图

sunqi
2020/8/3

Density Plot

Density Plot:也称作核密度图

函数和参数

geom_density()

color, size, linetype: 颜色、大小和线的类型

fill:填充

alpha:透明度

绘图

代码语言:javascript复制
# 需要的包
library(ggplot2)
theme_set(
  theme_classic()  
    theme(legend.position = "top")
  )
library(patchwork)
library(tidyverse)
rm(list = ls())
get_data <- function(){
  set.seed(1234)
  df = data.frame(
        sex = factor(rep(c("F", "M"), each=200)),
        weight = c(rnorm(200, 55), rnorm(200, 58))
        )
  return(df)
}

# 调用函数
dataset <- get_data()

# get the mean by sex
# me为性别分组的均值
me <- dataset %>%
  group_by(sex) %>%
  summarise(grp.mean = mean(weight))

# 基础绘图单元
p <- ggplot(dataset, aes(x = weight))

# 简单的绘图
# 添加密度图默认绘图
p1 <- p   geom_density()  
  # 添加垂直线
  geom_vline(aes(xintercept = mean(weight)), linetype = "dashed")
  
# y轴为计数
p2 <- p   geom_density(aes(y = stat(count)), fill = "lightgray")  
  # 添加垂直均值线
  geom_vline(aes(xintercept = mean(weight)), linetype = "dashed")
p1 p2
代码语言:javascript复制
# 根据颜色区分组

# 改变线的颜色
p3 <- p   geom_density(aes(color = sex))  
  # 这个函数很熟悉吧
  scale_color_manual(values = c("#868686FF", "#EFC000FF"))

# 更改线的颜色和填充颜色和垂直线
p4 <- p   geom_density(aes(fill = sex), alpha = 0.4)  
  # 添加垂直线,me为性别均值
  geom_vline(aes(xintercept = grp.mean, color = sex), data = me, linetype = "dashed")  
  # 以下为配色方案
  scale_color_manual(values = c("#868686FF", "#EFC000FF")) 
  scale_fill_manual(values = c("#868686FF", "#EFC000FF"))
p3 p4

Histogram

直方图为令一种展示分布的方式

主要的函数和参数

geom_histgram

color, size, linetype: 同上 fill: 填充 alpha: 透明度

代码

代码语言:javascript复制
rm(list=ls())
# 需要的包
library(ggplot2)
theme_set(
  theme_classic()  
    theme(legend.position = "top")
  )
library(patchwork)
library(tidyverse)
get_data <- function(){
  set.seed(1234)
  df = data.frame(
        sex = factor(rep(c("F", "M"), each=200)),
        weight = c(rnorm(200, 55), rnorm(200, 58))
        )
  return(df)
}
# 调用函数
dataset <- get_data()

# 计算均值
junzhi <- dataset %>%
  group_by(sex) %>%
  summarise(grp.mean = mean(weight))
# 绘图基本单元
# 后续添加图就在此基础上
p <- dataset %>% ggplot( aes(x = weight))

# 简单的直方图
# bins为一个柱子里放的数目
p   geom_histogram(bins = 30, color = "black", fill = "gray")  
  # 垂直线
  geom_vline(aes(xintercept = mean(weight)),
             linetype = "dashed", size = 0.6)
代码语言:javascript复制
# 用颜色表示分组
# 更改线段的颜色
p1 <- p   geom_histogram(aes(color = sex), fill = "white",
                   position = "identity")  
  # 添加颜色方案
  scale_color_manual(values = c("#00AFBB", "#E7B800"))

# 更改填充
p2 <- p   geom_histogram(aes(color = sex, fill = sex),
                         alpha = 0.4, position = "identity")  
  # 添加配色方案
  scale_fill_manual(values = c("#00AFBB", "#E7B800"))  
  scale_color_manual(values = c("#00AFBB", "#E7B800"))
p1 p2
代码语言:javascript复制
# 和和密度图组合

# 添加核密度图
p3 <- p   geom_histogram(aes(y = stat(density)),
                   colour="black", fill="white")  
  # 密度图部分
  geom_density(alpha = 0.2, fill = "#FF6666")
     

# 分组
p4 <- p   geom_histogram(aes(y = stat(density), color = sex),
                   fill = "white",position = "identity") 
  # 密度图部分
  geom_density(aes(color = sex), size = 1)  
  # 配色方案
  scale_color_manual(values = c("#868686FF", "#EFC000FF"))
p3 p4

结束语

核密度图和直方图一般在论文中使用的很少,这也就注定是一个数据探索阶段的绘图,所以修的再漂亮也没什么用

love&peace

0 人点赞