如何修复“错误:美学必须是长度1或与数据相同(28):yintercept”?

时间:2022-02-27 15:38:37

I'd like to make a forest plot for my project. Since it is not a typical forest plot built-in any R package, I found the first figure of this page is helpful to my goal, a side table accompanied with the forest plot: https://mcfromnz.wordpress.com/2012/11/06/forest-plots-in-r-ggplot-with-side-table/
The code which produces that particular figure is pasted below (the original link:https://github.com/nzcoops/blog_code/blob/master/forest_plot.Rmd)

我想为我的项目制作森林情节。由于它不是一个内置任何R包的典型森林地块,我发现这个页面的第一个数字对我的目标有帮助,一个边桌伴随着森林情节:https://mcfromnz.wordpress.com/2012/ 11/06 / forest-plots-in-r-ggplot-with-side-table /产生该特定数字的代码粘贴在下面(原始链接:https://github.com/nzcoops/blog_code/blob/master /forest_plot.Rmd)

The problem that I ran into is in the "data_table" step. An error pop up when I type the following in R:

我遇到的问题是在“data_table”步骤中。在R中键入以下内容时弹出错误:

data_table

Error: Aesthetics must be either length 1 or the same as the data (28): yintercept

错误:美学必须是长度1或与数据(28)相同:yintercept

I guess the issue came from geom_hlinein data_table. After some online search and some try-and-error, I still cannot get rid of that error message and wonder if I can get some help here. Thanks in advance for your help.

我想这个问题来自geom_hlinein data_table。经过一些在线搜索和一些尝试和错误,我仍然无法摆脱该错误消息,并想知道我是否可以在这里得到一些帮助。在此先感谢您的帮助。

--Code that particular produce the first figure:

- 代码特别产生第一个数字:

library(ggplot2)
library(gridExtra)
dat <- data.frame(group = factor(c("A","B","C","D","E","F","G"), levels=c("F","E","D","C","B","A","G")),
              cen = c(3.1,2.0,1.6,3.2,3.6,7.6,NA),
              low = c(2,0.9,0.8,1.5,2,4.2,NA),
              high = c(6,4,2,6,5,14.5,NA))
theme_set(theme_bw())
theme_update(
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(0,0,0,0), "lines"))

p <- ggplot(dat,aes(cen,group)) + 
geom_point(size=5, shape=18) +
geom_errorbarh(aes(xmax = high, xmin = low), height = 0.15) +
geom_vline(xintercept = 1, linetype = "longdash") +
scale_x_continuous(breaks = seq(0,14,1), labels = seq(0,14,1)) +
labs(x="Adjusted Odds Ratio", y="")
data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) + 
theme(panel.grid.major = element_blank(), 
      legend.position = "none",
      panel.border = element_blank(), 
      axis.text.x = element_text(colour="white"),#element_blank(),
      axis.text.y = element_blank(), 
      axis.ticks = element_line(colour="white"),#element_blank(),
      plot.margin = unit(c(0,0,0,0), "lines")) +
          labs(x="",y="") +
          coord_cartesian(xlim=c(1,4.5))

lab <- data.frame(V0 = factor(c("A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G","A","B","C","D","E","F","G"),, levels=c("G","F","E","D","C","B","A")),
              V05 = rep(c(1,2,3,4),each=7),
              V1 = c("Occuption","Active","","Inactive","","Inactive","","Recreation","Inactive","","Active","","Inactive","","Gender","Men","Women","Men","Women","Men","Women","OR",3.1,2.0,1.6,3.2,3.6,7.6))

data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
geom_hline(aes(yintercept=c(6.5,7.5))) + 
theme(panel.grid.major = element_blank(), 
      legend.position = "none",
      panel.border = element_blank(), 
      axis.text.x = element_text(colour="white"),#element_blank(),
      axis.text.y = element_blank(), 
      axis.ticks = element_line(colour="white"),#element_blank(),
      plot.margin = unit(c(0,0,0,0), "lines")) +
          labs(x="",y="") +
          coord_cartesian(xlim=c(1,4.5))

2 个解决方案

#1


0  

The easiest fix would be separating geom_hline into 2 different calls

最简单的解决方法是将geom_hline分成2个不同的调用

data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
  geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
  geom_hline(aes(yintercept=c(6.5))) +
  geom_hline(aes(yintercept=c(7.5))) +
  theme(panel.grid.major = element_blank(), 
        legend.position = "none",
        panel.border = element_blank(), 
        axis.text.x = element_text(colour="white"),#element_blank(),
        axis.text.y = element_blank(), 
        axis.ticks = element_line(colour="white"),#element_blank(),
        plot.margin = unit(c(0,0,0,0), "lines")) +
  labs(x="",y="") +
  coord_cartesian(xlim=c(1,4.5))  
data_table  

如何修复“错误:美学必须是长度1或与数据相同(28):yintercept”?

Created on 2018-03-31 by the reprex package (v0.2.0).

由reprex包(v0.2.0)于2018-03-31创建。

#2


0  

You don't need to use aes() with geom_hline (only use aes() if you want a horizontal line for every row of your data.) You can just do:

您不需要将aes()与geom_hline一起使用(如果您希望数据的每一行都有一条水平线,则只需使用aes()。)您可以这样做:

geom_hline(yintercept = c(6.5, 7.5))

This is explained in the help, see ?geom_hline for more details.

这在帮助中有解释,有关详细信息,请参阅?geom_hline。

#1


0  

The easiest fix would be separating geom_hline into 2 different calls

最简单的解决方法是将geom_hline分成2个不同的调用

data_table <- ggplot(lab, aes(x = V05, y = V0, label = format(V1, nsmall = 1))) +
  geom_text(size = 4, hjust=0, vjust=0.5) + theme_bw() +
  geom_hline(aes(yintercept=c(6.5))) +
  geom_hline(aes(yintercept=c(7.5))) +
  theme(panel.grid.major = element_blank(), 
        legend.position = "none",
        panel.border = element_blank(), 
        axis.text.x = element_text(colour="white"),#element_blank(),
        axis.text.y = element_blank(), 
        axis.ticks = element_line(colour="white"),#element_blank(),
        plot.margin = unit(c(0,0,0,0), "lines")) +
  labs(x="",y="") +
  coord_cartesian(xlim=c(1,4.5))  
data_table  

如何修复“错误:美学必须是长度1或与数据相同(28):yintercept”?

Created on 2018-03-31 by the reprex package (v0.2.0).

由reprex包(v0.2.0)于2018-03-31创建。

#2


0  

You don't need to use aes() with geom_hline (only use aes() if you want a horizontal line for every row of your data.) You can just do:

您不需要将aes()与geom_hline一起使用(如果您希望数据的每一行都有一条水平线,则只需使用aes()。)您可以这样做:

geom_hline(yintercept = c(6.5, 7.5))

This is explained in the help, see ?geom_hline for more details.

这在帮助中有解释,有关详细信息,请参阅?geom_hline。