R语言 斑马森林图ggplot

0s0u357o  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(168)

我已将logistic回归模型的结果(OR,95% CI)制成表格,我希望以斑马为主题并按性别(=组女性或男性)排序的“森林图样”图显示比值比。

structure(list(aOR = c(0.755657618643583, 1.62135330604067, 0.865398256316296, 
0.727203230474939, 2.11334255563662, 0.898407262562538), confidence_lower = c(0.613593844349365, 
1.35046132165669, 0.841880115298544, 0.6196490131542, 1.79796736338675, 
0.877221949869505), confidence_upper = c(0.930613046191133, 1.94658410489249, 
0.889573382749048, 0.853425934984244, 2.48403661179473, 0.920104210280172
), group = c("Female", "Female", "Female", "Male", "Male", "Male"
), labels = c("Age", "Diabetes", "BMI", "Age", "Diabetes", "BMI"
)), row.names = c(NA, -6L), class = "data.frame")

我开始使用ggplot编码,多亏了我在这里发现的另一个问题,我发现为了显示按性别分组的点和条,我需要分解标签。
这是我的代码:

#Factors
Variable_order <- c('Age', 'Diabetes', 'BMI')
df$labels = factor (df$labels, level=Variable_order)

#Define colours for dots and bars
dotCOLS = c("orchid2","dodgerblue2")
barCOLS = c("black","black")

#Plot
p <- ggplot (df, aes(x=aOR, xmin=confidence_lower, xmax=confidence_upper, y=labels, col=group, fill=group)) +
  geom_linerange(linewidth=0, position=position_dodge(width = 0.5)) +
  geom_vline(xintercept=1, lty=2) +
  geom_point(size=3, shape=21, stroke = 0, position=position_dodge(width = 0.5)) +
  scale_fill_manual(values=dotCOLS)+
  labs(x="Odds ratio") +
  scale_color_manual(values=barCOLS)+
  coord_cartesian(ylim=c(1,4), xlim=c(-2.5, 4.5)) +
  annotate("text", x=-1, y=4.5, label = "Reduce death") +
  annotate("text", x=3, y=4.5, label = "Increase death") +
  theme_classic() 
print(p)

和我的图表

我想达到的结果是这样的,正如你所看到的,我离它还很远:

我在这里发现了一个类似的问题[https://stackoverflow.com/questions/15420621/reproduce-table-and-plot-from-journal/20266137#20266137],然而我的问题是csv数据集不能再下载了,我不知道如何复制代码。
我的问题是我需要将OR分组,但我在这里找不到解决方案。请帮助。我想使用ggplot,因为与其他现有的软件包相比,它更容易定制。

yqlxgs2m

yqlxgs2m1#

您可以使用group美学和position_dodge

ggplot(df, aes(x = aOR, y = labels, group = group)) +
  geom_hline(yintercept = c("Age", "Diabetes"), linewidth = 50, 
             colour = "gray92") +
  geom_point(aes(shape = group), position = position_dodge(0.4), size = 3) +
  geom_errorbar(aes(xmin = confidence_lower, xmax = confidence_upper),
                 position = position_dodge(0.4), width = 0.05) +
  geom_vline(xintercept = 1) +
  annotate(geom = "text", x = c(0.2, 5), y = c(Inf, Inf), size = 6, fontface = 2,
           label = c("Reduce death", "Increase death"), vjust = 1.5) +
  scale_x_log10(breaks = c(0.1,0.2,  0.5, 1, 2, 5, 10), limits = c(0.1, 10)) +
  labs(y = NULL, x = "Odds ratio (log scale)") +
  theme_classic(base_size = 16) +
  theme(axis.line.y = element_blank())

相关问题