如何使用 ggplot 在 R 中的小提琴图中添加参考线?

问题描述 投票:0回答:1

我在 R 中构建了一个图,其中包含 3 个不同的小提琴图。我想添加 3 条不同的水平线,以便第一个图中的 y = 0.44,第二个图中的 y = 0.413,第三个图中的 y = 0.375。就像这张图片上的红点一样。

enter image description here

我尝试使用“geom_hline”,但没有成功,因为我找不到选择 x 值的方法,因为我的 x 轴不是由连续变量组成 这是我用来构建绘图的当前代码:

library(tidyverse)
library(modeldata)
?geom_boxplot

data <- read.csv("Qualtrics.csv", TRUE, ";")
ggplot(
  data, 
  aes(x = factor(Problem,level = c("Disease", "Cab", "Course")), 
  y = Estimate, 
  fill = Problem)
) + 
geom_violin(fill = "lightgray") + 
geom_boxplot(width = .02, fill= "black") + 
xlab("Problems")
r tidyverse boxplot violin-plot
1个回答
0
投票

您可以使用

geom_segment
告诉
ggplot
在哪里画线

library(ggplot2)

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_violin() +
  geom_segment(aes(x = 0.75, xend = 1.25, y = 5, yend = 5), color = "blue") +
  geom_segment(aes(x = 1.75, xend = 2.25, y = 6, yend = 6), color = "green") +
  geom_segment(aes(x = 2.75, xend = 3.25, y = 7, yend = 7), color = "red")

创建于 2024-06-10,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.