Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用ggplot2在2x2排列的不同面中添加水平线?_R_Ggplot2_Facet_Geom Hline - Fatal编程技术网

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和我有相同的解决方案,但打字速度更快!!