ggpol优雅绘制半分箱线图

2023-09-11 08:47:06 浏览数 (1)

❝本节来介绍如何使用「ggpol」的内置两个函数来绘制两个特殊图形,下面小编就通过案例来进行展示数据为随意构建无实际意义仅作图形展示用,希望各位观众老爷能够喜欢。❞

官方文档

❝https://github.com/erocoar/ggpol❞

R包安装加载

代码语言:javascript复制
install.packages("ggpol")

# 检查是否已安装devtools包,如果没有则安装
if (!require(devtools)) {
  install.packages('devtools')
}

# 使用devtools安装来自GitHub的ggpol包
devtools::install_github('erocoar/ggpol')

library(ggpol)
library(tidyverse)

案例1 构建数据

代码语言:javascript复制
bt <- data.frame(
  parties = factor(c("CDU", "CSU", "AfD", "FDP", "SPD", 
                     "Linke", "Gruene", "Fraktionslos"),
                   levels = c("CDU", "CSU", "AfD", "FDP", "SPD", 
                              "Linke", "Gruene", "Fraktionslos")),
  seats = c(50,406,92,80,153,69,67,2),
  colors = c("black", "blue", "lightblue", "yellow", 
              "red","purple", "green", "grey"),
  stringsAsFactors = FALSE)

ggpol绘制环状图

代码语言:javascript复制
ggplot(bt)   
  geom_parliament(aes(seats = seats, fill = parties), color = "black")   
  scale_fill_manual(values = bt$colors, labels = bt$parties)  
  coord_fixed()   
  theme_void()

案例2 构建数据

代码语言:javascript复制
df <- data.frame(score = rgamma(150, 4, 1), 
                 gender = sample(c("M", "F"), 150, replace = TRUE), 
                 genotype = factor(sample(1:3, 150, replace = TRUE)))

绘制半分箱线图

代码语言:javascript复制
ggplot(df)   geom_boxjitter(aes(x = genotype, y = score, fill = gender),
                            jitter.shape = 21, jitter.color = NA, 
                            jitter.params = list(height = 0, width = 0.04),
                            outlier.color = NA, errorbar.draw = TRUE)  
  scale_fill_manual(values = c("#ecb21e", "#812e91"))  
  theme_minimal()

0 人点赞