ggplot Stripchart and line

时间:2022-07-25
本文章向大家介绍ggplot Stripchart and line,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

ggplot Stripchart and line

sunqi

2020/8/3

点带图

Stripcharts:一维散点图

主要函数和参数

geom_jitter()

color, fill, size, shape:和之前介绍的一样

代码

rm(list = ls())

#' Title 获得数据
#'
#' @return ToothGrowth
get_data <- function() {
  data("ToothGrowth")
  ToothGrowth$dose <- as.factor(ToothGrowth$dose)
  return(ToothGrowth)
}

ToothGrowth <- get_data()
# 需要的包

library(ggplot2)
library(patchwork)
library(tidyverse)

# 绘图
p <- ToothGrowth %>%  ggplot(aes(x = dose, y = len))

#添加点带图
# 用颜色和形状分组
p + geom_jitter(aes(shape = dose, color = dose),
                position = position_jitter(0.2),
                size = 1.2) +
  # 添加均值和标准差
  stat_summary(
    aes(color = dose),
    size = 0.4,
    # mult=1表示加减一个标准差
    fun.data = "mean_sdl",
    fun.args = list(mult = 1)
  ) +
  # 添加自定义配色方案
  scale_color_manual(values =  c("#00AFBB", "#E7B800", "#FC4E07"))
# 点带统和箱式图、小提琴图的组合
p1 <- p + geom_boxplot() +
  geom_jitter(position = position_jitter(0.2))

p2 <- p + geom_violin(trim = FALSE) +
  geom_jitter(position = position_jitter(0.2)) +
  stat_summary(fun.data = "mean_sdl",
               fun.args = list(mult = 1),
               color = "red")

p1 + p2
# 多组scrip图
# 这里需要用position_jitterdodge替换position_dodge

p + geom_jitter(
  aes(shape = supp, color = supp),
  size = 1.2,
  position = position_jitterdodge(jitter.width = 0.2, dodge.width = 0.8)
) +
  stat_summary(
    aes(color = supp),
    fun.data = "mean_sdl",
    fun.args = list(mult = 1),
    size = 0.4,
    position = position_dodge(0.8)
  ) +
  scale_color_manual(values =  c("#00AFBB", "#E7B800"))

线图

主要函数

geom_path() 连接

geom_line() 绘制线

geom_step() 阶梯图

代码

rm(list = ls())
#
#' Title 建立数据库
#'
#' @return dataset
#' @export
#'
build_data <- function() {
  df <- data.frame(dose = c("D0.5", "D1", "D2"),
                   len = c(4.2, 10, 29.5))
  
  df2 <- data.frame(
    supp = rep(c("VC", "OJ"), each = 3),
    dose = rep(c("D0.5", "D1", "D2"), 2),
    len = c(6.8, 15, 33, 4.2, 10, 29.5)
  )
  
  df3 <- data.frame(
    supp = rep(c("VC", "OJ"), each = 3),
    dose = rep(c("0.5", "1", "2"), 2),
    len = c(6.8, 15, 33, 4.2, 10, 29.5)
  )
  dataset <- list(df = df, df2 = df2, df3 = df3)
  return(dataset)
}

dataset <- build_data()

# 导入需要的包
library(ggplot2)
library(patchwork)
library(tidyverse)
theme_set(theme_classic() +
            theme(legend.position = "top"))

# 基本绘图单元

p <- dataset$df %>% ggplot(aes(x = dose, y = len, group = 1))

# 基本的线图和点图
p1 <- p + geom_line() + geom_point()

# 更改颜色和线的类型
p2 <- p + geom_line(linetype = "dashed", color = "steelblue") +
  geom_point(color = "steelblue")
# 添加阶梯绘图
p3 <- p + geom_step() + geom_point()
p1 + p2 + p3
# 添加分组线图,这里使用数据集2,用于绘制分组线图

p <- dataset$df2 %>% ggplot(aes(x = dose, y = len, group = supp))
# 根据分组添加不同的线
p4 <- p + geom_line(aes(linetype = supp)) +
  geom_point(aes(shape = supp))

# 更改线的颜色和线的类型
p5 <- p + geom_line(aes(linetype = supp, color = supp)) +
  geom_point(aes(shape = supp, color = supp)) +
  scale_color_manual(values = c("#999999", "#E69F00"))

p4 + p5
# 上述的图形x变量均为离散变量,在实际的科研中,更多的是连续变量
#将x转换为连续变量

p_lianxu <- function(df3) {
  df3$dose <- as.numeric(as.vector(df3$dose))
  p <-
    df3 %>%  ggplot(aes(
      x = dose,
      y = len,
      group = supp,
      color = supp
    )) +
    geom_line() + geom_point()
  return(p)
}


# 将x转换为离散变量
p_lisan <- function(df3) {
  df3$dose <- as.factor(df3$dose)
  p <-
    df3 %>%  ggplot(aes(
      x = dose,
      y = len,
      group = supp,
      color = supp
    )) +
    geom_line() + geom_point()
  return(p)
}
df3 <-
  p_lianxu <- p_lianxu(dataset$df3)
p_lisan <- p_lisan(dataset$df3)
# 可以看出曲线存在差异,因为x的变量因子和连续的处理方式不同
p_lianxu + p_lisan
# 时间序列的绘图
# 这里使用的数据是economics
# date代表时间,pop代表的是人口
head(economics)
## # A tibble: 6 x 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
## 1 1967-07-01  507. 198712    12.6     4.5     2944
## 2 1967-08-01  510. 198911    12.6     4.7     2945
## 3 1967-09-01  516. 199113    11.9     4.6     2958
## 4 1967-10-01  512. 199311    12.9     4.9     3143
## 5 1967-11-01  517. 199498    12.8     4.7     3066
## 6 1967-12-01  525. 199657    11.8     4.8     3018
# 绘图
economics %>%  ggplot(aes(x = date, y = pop)) +
  geom_line()
# 设置亚组,大于2016年1月1日的数据
economics %>% filter(date > as.Date("2006-1-1")) %>%
  ggplot(aes(x = date, y = pop)) + geom_line()
# 更改线的宽度
# 这里使用的是为工作的和总人口的比例
# 因此出来的图为宽度不同的线
economics %>% ggplot(aes(x = date, y = pop)) +
  geom_line(aes(size = unemploy / pop))
# 绘制多个线
# 在总体布局不设置y变量,通过两个line函数绘制两个曲线
economics %>% ggplot(aes(x = date)) +
  geom_line(aes(y = psavert), color = "darkred") +
  geom_line(aes(y = uempmed), color = "steelblue", linetype = "twodash")
# 绘制曲线下面积
# 使用fill函数
economics %>% ggplot(aes(x = date)) +
  geom_area(aes(y = psavert),
            fill = "#999999",
            color = "#999999",
            alpha = 0.5) +
  geom_area(aes(y = uempmed),
            fill = "#E69F00",
            color = "#E69F00",
            alpha = 0.5)

结束语

对于dot、scatter、stripchart,这三个图,有啥区别,我也看不懂,也没有相关的资料,后续用到再说吧

tip:多用管道符,可以节省很多代码的编写,同时函数的使用能够使代码看起来更加有结构感

love&peace