【问题标题】:Dealing with factors in geom_pointrange in ggplot处理ggplot中geom_pointrange中的因素
【发布时间】:2018-05-31 09:50:23
【问题描述】:

我正在尝试可视化包含嵌套在国家/地区中的区域的优势比和置信区间的一些数据。我为此使用了 geom_pointrange 选项,它通常效果很好。

我的问题是,由于优势比(和上置信区间)可以获得相当高的值,因此图的轴被拉伸以适应这一点。因此,位于 0 和 1 之间的置信区间看起来不够清晰。我通过这个社区找到的一个选择是将值更改为因子,并且对于每次测量,它们之间的距离将被认为是相同的。这适用于优势比(仍然需要调整轴刻度线),但是当涉及上下置信区间的值时,位置完全错误,置信区间不包括点估计。我试图通过将所有值作为因子的级别来解决这个问题,但这似乎并没有解决问题。

我想要做的是要么能够“放大”图中 0 和 1 之间的区域,同时保持绘图区域的其余部分不变,要么设法使 ggplot 正确放置置信区间优势比。

下面我包含了我的数据的简化版本和我用于重现性的代码。

dat <- data.frame(region = rep(LETTERS[1:5], 2), 
              country = rep(c("A1", "A2"), each = 5), 
              or = c(6.459578, 1.696221, 0.895115, 3.393235, 2.325510, 
                     4.457805, 0.407111, 22.760861, 3.354883, 2.214915), 
              lower = c(5.768999699, 0.237062909, 0.347443105, 0.369881529, 
                        0.010233696, 1.020315696, 0.004419494, 3.87391259, 
                        0.808667764, 0.874415935), 
              upper = c(7.2328221, 12.1367207, 2.3060778, 31.1290104, 
                        28.4497981, 19.4763489, 0.750188, 337.2960785, 
                        13.9182469,    5.610429))

library(ggplot2)

 ggplot(dat, aes(x = region, y = or, ymin = lower, ymax = upper))+
   geom_pointrange() +
   geom_hline(yintercept = 1, linetype = 2) +
   theme_bw() + 
   theme(plot.margin = unit(c(1, 1, 1, 4), "lines"),
         axis.title = element_blank(),
         axis.ticks.y = element_blank(),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(), 
         legend.position="none") + 
   facet_wrap(~ country) + 
   coord_flip(ylim = c(0, 100))

 # Change numeric variable into factors
 f.levels <- c(dat$or, dat$lower, dat$upper)
 f.levels <- unique(f.levels)
 f.levels <- as.character(f.levels[order(f.levels)])

 dat$or <- factor(dat$or, levels = f.levels)
 dat$lower <- factor(dat$lower, levels = f.levels)
 dat$upper <- factor(dat$upper, levels = f.levels)

 ggplot(dat, aes(x = region, y = or, ymin = lower, ymax = upper))+
    geom_pointrange() +
    geom_hline(yintercept = 1, linetype = 2) +
    theme_bw() + 
    theme(plot.margin = unit(c(1, 1, 1, 4), "lines"),
          axis.title = element_blank(),
          axis.ticks.y = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(), 
          legend.position="none") + 
    facet_wrap(~ country) + 
    coord_flip(ylim = c(0, 30))

我对 ggplot 比较陌生,所以请原谅任何新手的错误。 高度赞赏有关此问题的任何建议。

谢谢!

【问题讨论】:

  • 我认为您可能应该考虑以对数标度呈现优势比(无论如何,这是 OR 的自然选择)。你可以很容易地做到这一点,例如scale_y_log10().

标签: r ggplot2


【解决方案1】:

我认为这个问题的标准解决方案是以 log(10) 比例绘制 OR。有关简洁的解释,请参阅https://blogs.sas.com/content/iml/2015/07/29/or-plots-log-scale.html

ggplot(dat, aes(x = region, y = or, ymin = lower, ymax = upper)) +
geom_pointrange() +
  geom_hline(yintercept = 1, linetype = 2) +
  scale_y_log10() + ### This is the line that makes the transfomation
  theme_bw() + 
  theme(plot.margin = unit(c(1, 1, 1, 4), "lines"),
        axis.title = element_blank(),
        axis.ticks.y = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        legend.position="none") + 
  facet_wrap(~ country) + 
  coord_flip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多