R 如何使用ggplot2在2x2排列的不同面中添加水平线?
我有一个数据库,它是由面绘制和分隔的。第一行(行R 如何使用ggplot2在2x2排列的不同面中添加水平线?,r,ggplot2,facet,geom-hline,R,Ggplot2,Facet,Geom Hline,我有一个数据库,它是由面绘制和分隔的。第一行(行a)的面需要一条水平线,位于0.5,而第二行(行b)的面需要一条水平线,位于1。我已经部分实现了以下目标。但是,0.5和1处的水平线显示在所有面中 library(ggplot2) #Data values <- c(0.4, 0.6, 0.9, 1.1) Column <- c("UW", "LW", "UW", "LW") Row <- c(&q
a
)的面需要一条水平线,位于0.5,而第二行(行b
)的面需要一条水平线,位于1。我已经部分实现了以下目标。但是,0.5和1处的水平线显示在所有面中
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
Target <- c("a", "b")
Lines <- c(0.5, 1)
Lines_in_plot <- data.frame(Target, Lines)
Lines_in_plot$Target <- factor(Lines_in_plot$Target)
#Plot
ggplot(data = DF, aes(y = values)) +
geom_bar() +
facet_grid(Row~Column,
scales = "free") +
geom_hline(data = Lines_in_plot,
yintercept = Lines,
linetype = "dashed",
color = "red")
要在特定面板中显示截距,您需要将
刻面网格中引用的行作为绘图中的行中的变量提供。您还需要将yintercept
放入aes
中,以便ggplot知道如何引用yintercept
中的行
...
#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$Target)
#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")
。。。
#辅助测向仪
行对于要在特定面板中显示的截距,您需要将刻面网格中引用的行作为测线图中的变量提供。您还需要将yintercept
放入aes
中,以便ggplot知道如何引用yintercept
中的行
...
#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$Target)
#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")
。。。
#辅助测向仪
行以下是您的解决方案:
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")
图书馆(GG2)
#资料
值以下是您的解决方案:
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")
图书馆(GG2)
#资料
值如果从geom_hlineth中减少data=Lines_in_plot part会发生什么情况修复错误但不修复分割线…如果从geom_hlineth中减少data=Lines_in_plot part会发生什么情况修复错误但不修复分割线…啊@Jon Spring和我有相同的解决方案,但打字速度更快!!啊!@Jon Spring和我有相同的解决方案,但打字速度更快!!