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

R语言ggplot2点图绘制:布局调整、垂直线添加及年份问题解决

问题描述

我在R中有一个名为data的数据框,部分结构如下:

Year  Color var value
1  2023   blue   A -0.06
2  2023   blue   A  0.04
3  2023   blue   G -0.27
4  2023   blue   D  0.05
5  2023   blue   L  0.18
6  2023   blue   B  0.28
7  2023   blue   B  0.15
8  2023   blue   E  0.10

完整数据结构:

structure(list(Year = c(2023, 2023, 2023, 2023, 2023, 2023, 2023, 
2023, 2023, 2023, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 
2022, 2022, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019), Color = c("blue", 
"blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", 
"blue", "red", "red", "red", "red", "red", "red", "red", "red", 
"red", "red", "green", "green", "green", "green", "green", "green", 
"green", "green", "green", "purple", "purple", "purple", "purple", 
"purple", "purple", "purple", "purple", "purple"), var = c("A", 
"A", "G", "D", "L", "B", "B", "E", "E", "C", "A", "A", "G", "D", 
"L", "B", "B", "E", "E", "C", "A", "A", "G", "D", "B", "B", "E", 
"E", "C", "A", "A", "G", "D", "B", "B", "E", "E", "C"), value = c(-0.06, 
0.04, -0.27, 0.05, 0.18, 0.28, 0.15, 0.1, 0.02, -0.19, -0.04, 
0.22, -0.13, 0.05, 0.12, 0.19, 0.07, 0, -0.42, -0.05, -0.07, 
0.13, -0.23, 0.23, -0.09, -0.01, -0.11, 0.07, -0.23, -0.3, 0.15, 
-0.19, 0.09, -0.03, 0.03, 0, 0.11, -0.09)), class = "data.frame", row.names = c(NA, 
-38L))

我尝试用ggplot2绘制点图,代码如下:

ggplot(data, aes(x = value, y = Year, color = Color)) +
  geom_point(size = 3) +            
  facet_wrap(~ var) +              
  theme_minimal() +                 
  theme(legend.position = "none")

但遇到三个问题:

  • var列的各水平未横向分隔,想要实现横向排列的分面样式
  • 需要在所有分面中添加x=0的垂直线
  • 图中显示了无数据的2021年,需要移除该年份
解决方案

针对三个问题,分别处理如下:

1. 实现var水平横向分隔

将facet_wrap替换为facet_grid,指定行维度为.(不按行分)、列维度为var,即可让所有var水平横向排列:

facet_grid(. ~ var)

2. 添加x=0的垂直线

使用geom_vline()函数,设置xintercept=0,即可在所有分面中添加垂直参考线。可通过linetype和color参数调整线条样式,避免遮挡数据点:

geom_vline(xintercept = 0, linetype = "dashed", color = "gray50")

3. 移除无数据的2021年

有两种可行方法:

  • 方法一:提前过滤数据
    在绘图前筛选出数据中实际存在的年份:
data_filtered <- data[data$Year %in% unique(data$Year), ]

后续用data_filtered作为绘图数据源即可。

  • 方法二:在ggplot中设置y轴刻度
    通过scale_y_continuous()指定只显示数据中存在的年份:
scale_y_continuous(breaks = unique(data$Year))
完整代码

结合所有修改后的完整代码如下:

library(ggplot2)

# 方法一:先过滤数据再绘图
data_filtered <- data[data$Year %in% unique(data$Year), ]

ggplot(data_filtered, aes(x = value, y = Year, color = Color)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray50") +
  geom_point(size = 3) +            
  facet_grid(. ~ var) +              
  theme_minimal() +                 
  theme(legend.position = "none")

# 方法二:直接设置y轴刻度
ggplot(data, aes(x = value, y = Year, color = Color)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray50") +
  geom_point(size = 3) +            
  facet_grid(. ~ var) +              
  scale_y_continuous(breaks = unique(data$Year)) +
  theme_minimal() +                 
  theme(legend.position = "none")

内容的提问来源于stack exchange,提问作者Homer Jay Simpson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 01:56:04