R中轻松绘制南丁格尔图

2023-09-29 10:10:35 浏览数 (1)

加载R包

代码语言:javascript复制
library(tidyverse)
library(ggtext)

导入数据

代码语言:javascript复制
df <- readr::read_csv('data.csv')

构建标签数据
labs <- data.frame(Episode_order = c(7, 18, 29), y = c(28, 32, 40), 
                   lab = c("<span style = 'color: #4169E1;'>Season 1</span>", 
                           "<span style = 'color: #5D478B;'>Season 2</span>", 
                           "<span style = 'color: darkorange3;'>Season 3</span>"))

数据可视化

代码语言:javascript复制
ggplot(df, aes(x = Episode_order))  
    # 为y轴的值添加文本注释
    annotate('text', x = 34.5, y = c(12, 22, 32, 42, 52),
             label = c('10', '20', '30', '40', '50'), color = "black")   
    # 在y轴上以10为间隔添加水平线
    geom_hline(yintercept = seq(0, 50, by = 10), colour = "grey70", linewidth = 0.3)   
    # 添加柱状图,表示总数
    geom_col(aes(y = F_count_total, fill = as.factor(Season)), alpha = 0.8, show.legend = FALSE)   
    # 添加柱状图,表示RK的计数
    geom_col(aes(y = F_count_RK, fill = as.factor(Season)), show.legend = FALSE)   
    # 添加富文本,用于显示季节标签
    geom_richtext(data = labs, aes(x = Episode_order, y = y, label = lab), fill = NA,
                  label.color = NA, size = 8)   
    # 手动设置填充颜色
    scale_fill_manual(values = c("#788FCE","#E6956F","#A88AD2"))   
    # 设置y轴的范围和间隔
    scale_y_continuous(limits = c(0, 52), breaks = seq(0, 50, 10))  
    coord_polar()       # 使用极坐标
    theme_void()       # 使用空白主题
    # 设置主题,移除面板网格和背景
    theme(panel.grid = element_blank(),
          panel.background = element_rect(fill = NA, color = NA), 
          plot.background = element_rect(fill = NA, color = NA)) 

0 人点赞