ggplot2:指定geom_polygon相对于geom_point大小的坐标

时间:2019-02-13 05:36:57

标签: r ggplot2 forestplot

我正在尝试创建一个森林图,并为汇总汇总统计信息添加菱形。

我设法用geom_polygon创建菱形,但是菱形的大小相对于上方geom_point层的大小而变化。这意味着根据情节的高度,它要么看起来好,要么是愚蠢的。

我想以某种方式将多边形固定为其与上方的白色菱形相同的高度(ymin至ymax)。这可能吗?是否可以测量geom_points的高度(精确坐标),然后将其用于多边形的构造?

下图显示了问题,geom_points保持相同的大小,但是菱形随情节而伸展。

link to image to show the issue

我用于生成情节和菱形的代码如下:

df<-structure(list(cluster = c("All", "Mostly female", "Mixed gender", 
"Mostly male", "study 1", "study 2", "study 3", "study 4", "study 5", 
"study 6"), k = c(22, 10, 8, 9, NA, NA, NA, NA, NA, NA), n = c(35, 
12, 8, 11, NA, NA, NA, NA, NA, NA), est = c(0.61, 0.49, 0.55, 
0.79, 0.41, 0.7, 0.4, 0.67, 0.38, 0.91), lower = c(0.49, 0.31, 
0.35, 0.64, 0.39, 0.64, 0.34, 0.61, 0.32, 0.84), upper = c(0.71, 
0.67, 0.74, 0.89, 0.44, 0.75, 0.46, 0.73, 0.44, 0.95), type = c("baseline", 
"moderator", "moderator", "moderator", "data", "data", "data", 
"data", "data", "data"), setting = c("Pooled", "Pooled", "Pooled", 
"Pooled", "Effect sizes", "Effect sizes", "Effect sizes", "Effect sizes", 
"Effect sizes", "Effect sizes"), year = c(1, 2, 3, 4, 1991, 1992, 
1992, 1992, 1992, 1994)), row.names = c(NA, 10L), class = "data.frame")

我这样创建一个森林图:

library(tidyverse)  

my_plot <-
  ggplot(df, 
         aes(y = reorder(cluster, year), x = est,
             xmin = lower, xmax = upper)) + 
    geom_point(color = "black") +
    geom_errorbarh(data = df[df$type != "baseline", ] , height = .1) +
    geom_vline(xintercept = .5, color = 'black', linetype = 'dashed') + 
    geom_point(data = df[df$type == "moderator", ],
               color = 'black', shape = 23, size = 3.2, fill = "white") + 
    facet_grid(setting ~ ., scales = 'free', space = 'free') +
    theme_classic() + 
    scale_x_continuous(limits = c(0, 1)) +
    geom_point(
      #add summary points
      data = df[df$type == "baseline", ],
      color = 'black', shape = 23, size = 4, fill = "black") + 
    geom_errorbarh(data = df[df$type == "baseline", ] , height = .2)

我这样创建钻石:

# create diamond polygon values #####
bd = df %>% filter(type == "baseline")

diamond_shape <-
  data.frame(
    x = c(bd$lower, bd$est, bd$upper, bd$est),
    y = c(1, 1.4, 1,0.6),
    names = c("xmin", "ymax", "xmax", "ymax"),
    setting = "Pooled"
  )

    #add diamond to plot ######        
my_plot + 
  geom_polygon(data = diamond_shape, aes(x = x, y = y), inherit.aes = F)

0 个答案:

没有答案