以下是您的解决方案:
library(ggplot2)
#Data
values <- c(0.4, 0.6, 0.9, 1.1)
Column <- c("UW", "LW", "UW", "LW")
Row <- c("a", "a", "b", "b")
DF <- data.frame(Row, Column, values)
DF$Column <- factor(DF$Column,
levels = c("UW", "LW"))
DF$Row <- factor(DF$Row,
levels = c("a", "b"))
#Auxiliar DF
Row <- c("a", "b")
Lines <- c(0.5, 1)
Lines_in_plot <- data.frame(Row, Lines)
Lines_in_plot$Row <- factor(Lines_in_plot$Row)
#Plot
ggplot(data = DF, aes(y = values)) +
geom_bar() +
facet_grid(Row~Column,
scales = "free") +
geom_hline(data = Lines_in_plot,
aes(yintercept = Lines),
linetype = "dashed",
color = "red")
两个变化:
-
将y轴截距移动到美学中
-
将目标重命名为Row以匹配Facet,以便它知道如何处理它们