让数据达到ggplot中的限制,而不要使用NA

时间:2018-07-09 01:50:20

标签: r ggplot2 range limit

我正在尝试使用ggplot2绘制一些标准误差(SE)线。在此设置中,我用粗条显示典型的SE条形,但是在这些条上,我覆盖显示“替代” SE的细条(在数据的标题“ se2”下)。这些替代的SE条总是比数据大。

我遇到的问题是删除了较大的备用SE,并且警告消息告诉我,删除了两行,因为它们包含缺少的值。我想要的只是无论如何都要显示这些值,如果备用SE栏达到我设置的极限,那么它会在那里停下来,仍然显示出来(对读者有暗示,然后它就会继续过去)。

我已经发布了我正在使用的简化版本:

# Load packages
library(dplyr)
library(ggplot2)
library(ggpubr)

# Make dataframe for group 1

df_values1 <- data.frame(
  beta = c(0.07,0.04,0.3),
  se = c(.01,0.01,0.008),
  se2 = c(0.1,0.05,0.2),
  outcome = c("Name 1",
          "Name 2",
          "Name 3"),
  sample = c(rep("Group1",3))
)

# Make dataframe for group 2

df_values2 <- data.frame(
  beta = c(0.15,-0.04,0.03),
  se = c(.01,0.01,0.008),
  se2 = c(0.1,.2,0.05),
  outcome = c("Name 1",
          "Name 2",
          "Name 3"),
  sample = c(rep("Group2",3))
)

# Make dataframe for group 3

df_values3 <- data.frame(
  beta = c(0.22,0.18,-0.03),
  se = c(.01,0.01,0.008),
  se2 = c(1,0.05,0.01),
  outcome = c("Name 1",
          "Name 2",
          "Name 3"),
  sample = c(rep("Group3",3))
)

# Position dodge
pd <- position_dodge(0.7)

# Merge datasets
df_all <- rbind(df_values1, df_values2, df_values3)

# NOTE: use the levels of outcome from one of the non-merged datasets
df_all$outcome <- factor(df_all$outcome, levels = df_values1$outcome)

# Because the coordinates will be flipped, the order of the levels is 'reversed' here
df_all$sample <- factor(df_all$sample, levels = c('Group3', 'Group2', 'Group1'))


# Plot
picture <- ggplot(df_all, aes(x = outcome, y = beta, group = sample, colour = sample)) + 
  geom_hline(yintercept = c(-0.375, -0.125, 0.125, 0.375), size = 0.25, colour = 'grey95') +
  geom_errorbar(aes(ymin = beta-1.96*se, ymax = beta+1.96*se), width = 0, alpha = 1, size = 2, position = pd) +
  geom_errorbar(aes(ymin = beta-1.96*se2, ymax = beta+1.96*se2), width = 0, alpha = 1, size = 0.5, position = pd) +
  geom_hline(yintercept = 0, size = 0.25) + 
  guides(colour = guide_legend(reverse = TRUE), shape = guide_legend(reverse = TRUE)) + 
  ylim(-0.5,0.5) + 
  coord_flip() + 
  scale_x_discrete(limits = rev(levels(df_all$outcome)))

picture

Here is the picture of the result

我希望在上面的示例中能够解决这两种情况:1)“名称1”的粉红色替代SE太大,因此理想情况下它们应该位于图的两端。 2)“名称3”的蓝色替代SE在右侧过大,但在图中应停止在左侧。因此在左侧它停止在图中,而在右侧继续直到达到极限。谢谢!

1 个答案:

答案 0 :(得分:1)

在此处查看两个答案:How to set limits for axes in ggplot2 R plots?通常使用coord_cartesian来防止数据被裁剪,但是如果您使用coord_flip,则可以在其中设置限制:

picture <- ggplot(df_all, aes(x = outcome, y = beta, group = sample, colour = sample)) + 
     geom_hline(yintercept = c(-0.375, -0.125, 0.125, 0.375), size = 0.25, colour = 'grey95') +
     geom_errorbar(aes(ymin = beta-1.96*se, ymax = beta+1.96*se), width = 0, alpha = 1, size = 2, position = pd) +
     geom_errorbar(aes(ymin = beta-1.96*se2, ymax = beta+1.96*se2), width = 0, alpha = 1, size = 0.5, position = pd) +
     geom_hline(yintercept = 0, size = 0.25) + 
     guides(colour = guide_legend(reverse = TRUE), shape = guide_legend(reverse = TRUE)) + 
     coord_flip(ylim = c(-0.5,0.5)) + 
     scale_x_discrete(limits = rev(levels(df_all$outcome)))