You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中绘制以演讲文字为条形的年份-标题长度图?

在R中实现演讲标题长度可视化(文字构成条形)

这个需求完全可以在R里实现,用ggplot2包就能搞定核心功能,甚至还能优化出更美观的效果。下面是具体的实现步骤和代码:

1. 数据预处理

首先把你提供的演讲列表整理成结构化的数据框,提取年份、演讲标题,并计算标题的字符长度:

# 加载所需包
library(tidyverse)

# 原始演讲数据
speech_raw <- c(
  "1996 Steven Harper - Immunopathogenesis of IgA nephropathy",
  "1997 Nigel Brunskill - Pathogenic implications of proteinuria",
  "1998 Albert Ong - Polycystic kidney disease",
  "1999 David Kluth - Inflammatory mechanisms of glomerular injury",
  "2000 Alice Allen - Pathogenesis of IgA nephropathy",
  "2001 Coralie Bingham - Definition of the Renal Cysts and Diabetes Syndrome",
  "2002 Sally Feather - Genetic basis of foetal malformations of the kidney and urinary tract",
  "2003 Helen Lachman - Amyloidosis",
  "2004 Jeremy Duffield - The role of macrophages in renal inflammation",
  "2005 Anna Richards - Atypical HUS",
  "2006 Menna Clatworthy - Immune inhibition",
  "2007 John Sayer - Molecular genetics of nephronophthisis",
  "2008 Bryan Conway - Finding genes for diabetic kidney disease",
  "2009 David Kavanagh - The pathogenesis of haemolytic uraemic syndrome",
  "2010 Andy Salmon - Regulation of glomerular permeability",
  "2011 Daniel Gale - Gene hunting in the Renal Unit",
  "2012 Amy Jayne McKnight - Unravelling heritable complexities for CKD",
  "2013 Andrew Hall - Power cuts in the proximal tubule: mitochondria, antiretrovirals & the renal Fanconi syndrome",
  "2014 Thomas Hiemstra - Waste not. The business of urine exosomes",
  "2015 James Fotheringham - Long breaks and disappearing PD catheters: lessons from linking UK Renal Registry data",
  "2016 Eoin Mckinney - Better off sick and tired: T cell exhaustion and outcome in autoimmune disease",
  "2017 Edwin Wong - Complement-mediated renal disease",
  "2018 Pippa Bailey - Understanding socioeconomic inequity in living-donor kidney transplantation",
  "2019 Alex Hamilton - The psychosocial health of young adults on renal replacement therapy",
  "2020 Matthew Graham-Brown (rolled over to UKKW 2021 due to the pandemic)",
  "2021",
  "2022 Jennifer Lees - CKD, cardiovascular disease and cancer: the scenic route",
  "2023 Melanie Chan - Genomic insights into urinary tract malformations: moving beyond monogenic inheritance",
  "2024 Benjamin Stewart - Mapping immunity in the human kidney with single cell genomics"
)

# 整理成数据框
speech_data <- tibble(raw = speech_raw) %>%
  mutate(
    # 提取年份
    year = str_extract(raw, "^\\d{4}"),
    # 提取演讲标题(去掉年份和人名部分)
    title = case_when(
      year == "2021" ~ NA_character_,
      TRUE ~ str_remove(raw, "^\\d{4}.*- ")
    ),
    # 计算标题字符长度
    title_length = nchar(title)
  ) %>%
  # 过滤2021年的空数据
  filter(!is.na(title_length))

2. 绘制可视化图表

用geom_text将演讲标题垂直排列,让文字的高度对应标题的字符长度,模拟“文字构成条形”的效果;也可以添加透明条形打底,让视觉效果更清晰:

方案1:纯文字条形

ggplot(speech_data, aes(x = year, y = title_length)) +
  # 垂直放置标题文字,从y=0处开始向上延伸
  geom_text(aes(label = title),
            angle = 90,
            hjust = 0,
            vjust = 0.5,
            size = 3,
            color = "#2c3e50") +
  # 设置y轴范围,避免文字超出图框
  scale_y_continuous(limits = c(0, max(speech_data$title_length) + 5)) +
  # 添加标签
  labs(x = "年份", y = "演讲标题长度(字符数)", title = "历年肾脏主题演讲标题长度") +
  # 优化主题样式
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

方案2:文字+透明条形打底

ggplot(speech_data, aes(x = year, y = title_length)) +
  # 添加透明条形作为背景
  geom_bar(stat = "identity", fill = "#3498db", alpha = 0.2) +
  # 叠加标题文字
  geom_text(aes(label = title),
            angle = 90,
            hjust = 0,
            vjust = 0.5,
            size = 3,
            color = "#2980b9") +
  scale_y_continuous(limits = c(0, max(speech_data$title_length) + 5)) +
  labs(x = "年份", y = "演讲标题长度(字符数)", title = "历年肾脏主题演讲标题长度") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

3. 调整细节

  • 如果标题太长导致排版拥挤,可以调小size参数缩小字体;
  • 调整angle参数可以改变文字的倾斜角度(比如设为45度);
  • 用ggtext包可以支持更复杂的文本样式(比如换行、颜色渐变),但基础ggplot2已经能满足核心需求。

内容的提问来源于stack exchange,提问作者tacrolimus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:16:08