GGPlot2中具有子组的林区 [英] Forest plot with subgroups in GGPlot2

查看:176
本文介绍了GGPlot2中具有子组的林区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为R中的newby,(没有任何编码经验)我有以下问题.我正在尝试创建比值比(球状图)的分组森林图.水平轴应包含或".垂直轴是变量.每个变量都包含A组和B组的OR(包括下限和上限).因此,每个在垂直轴上显示的变量都应可见2行.此网站和此

As a total newby in R (no coding experience whatsoever) I have the following problem. I am trying to create a grouped forest plot of odds ratio's (blobbogram). The horizontal axis should contain the OR. The vertical axis the variables. Each variable contains the OR (including lower and upper limit) for group A and group B. So, per variable shown on the vertical axis, 2 lines should be visible. This website and this website should give you kind of the idea of the grouping.

我从

I found quite a nice format for forest plots from Mike Barnkob which I have been trying to adapt to my needs.

我一直在争夺stackoverflow(例如,此处).使用这种方法,我必须拆分数据帧,我想也可以使用其他方法.

I have been scrambling through stackoverflow (e.g. here). With this method I have to split the dataframe, I suppose alternate ways may be possible as well.

请在代码下方找到,仅提供一组虚拟数据帧.

Please find below the code, with some dummy dataframe for just one group.

df <- data.frame(Outcome=c("Outcome A", "Outcome B", "Outcome C", "Outcome D"),
   OR=c(1.50, 2.60, 1.70, 1.30),
   Lower=c(1.00, 0.98, 0.60, 1.20),
   Upper=c(2.00, 3.01, 1.80, 2.20)
                      )
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')  

Outcome_order <- c('Outcome C', 'Outcome A', 'Outcome B', 'Outcome D')

p <- ggplot(df, aes(x=factor (Outcome, level=Outcome_order), y=OR, ymin=Lower, ymax=Upper)) + 
  geom_linerange(size=5, colour="#a6d8f0") +
  geom_hline(aes(x=0, yintercept=1), lty=2) +
  geom_point(size=3, shape=21, fill="#008fd5", colour="white", stroke = 0.5) +
  scale_x_discrete(name="(Post)operative outcomes") +
  scale_y_continuous(name="Odds ratio", limits = c(0.5, 5)) +
  coord_flip() +
  theme_minimal()
p

p + annotate("text", x=4.1, y=2.25, label="*")

有人知道如何为每个变量添加第二个组,理想情况下是在图例的上方或旁边添加图例吗?预先谢谢你!

Does anyone know how to add a second group per variable, ideally with a legend above or aside? Thank you in advance!

推荐答案

诀窍是将它们组合成一个数据框,并在下面的示例中引入另一个变量"group".绘制图时,需要指定postion = position_dodge(),以便它们可以并排绘制.看看下面是否是您想要的..颜色我真的很烂,所以您可能需要为另一组指定颜色

The trick is to combine them into a single data frame, and introduce another variable, "group" in my example below. When you make the plot, you need to specify postion=position_dodge() so that they would plot side by side. See if below is what you want.. I am really bad with colors so you might need to specify the colors for the other group

library('ggplot2') 

Outcome_order <- c('Outcome C', 'Outcome A', 'Outcome B', 'Outcome D')

#this is the first dataset you have
df1 <- data.frame(Outcome=c("Outcome A", "Outcome B", "Outcome C", "Outcome D"),
   OR=c(1.50, 2.60, 1.70, 1.30),
   Lower=c(1.00, 0.98, 0.60, 1.20),
   Upper=c(2.00, 3.01, 1.80, 2.20))
# add a group column
df1$group <- "X"
# create a second dataset, similar format to first
df2 <- df1
# different group
df2$group <- "Y"
# and we adjust the values a bit, so it will look different in the plot
df2[,c("OR","Lower","Upper")] <- df2[,c("OR","Lower","Upper")] +0.5

# combine the two datasets                      
df = rbind(df1,df2)
# you can do the factoring here
df$Outcome = factor (df$Outcome, level=Outcome_order)

#define colours for dots and bars
dotCOLS = c("#a6d8f0","#f9b282")
barCOLS = c("#008fd5","#de6b35")


p <- ggplot(df, aes(x=Outcome, y=OR, ymin=Lower, ymax=Upper,col=group,fill=group)) + 
#specify position here
  geom_linerange(size=5,position=position_dodge(width = 0.5)) +
  geom_hline(yintercept=1, lty=2) +
#specify position here too
  geom_point(size=3, shape=21, colour="white", stroke = 0.5,position=position_dodge(width = 0.5)) +
  scale_fill_manual(values=barCOLS)+
  scale_color_manual(values=dotCOLS)+
  scale_x_discrete(name="(Post)operative outcomes") +
  scale_y_continuous(name="Odds ratio", limits = c(0.5, 5)) +
  coord_flip() +
  theme_minimal()

这篇关于GGPlot2中具有子组的林区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆