简体   繁体   English

ggplot2 - 具有不同标签的第二个 y 轴

[英]ggplot2 - Second y-axis with different labels

I created a graph showing the effect size and confidence intervals.我创建了一个显示效果大小和置信区间的图表。 I would like to put on the right side of this graph (second y-axis) exact text information about "ES" and "CI" in the form: -0.07 [-1.16, 0.99], which means: ES [+95 CI, -95CI].我想把关于“ES”和“CI”的确切文本信息放在这张图的右侧(第二个y轴),格式为:-0.07 [-1.16, 0.99],这意味着:ES [+95 CI , -95CI]。 I can't handle it.我应付不来。 Could I please help?我能帮忙吗? Such a chart can certainly be useful to someone else.这样的图表当然对其他人有用。 Of course, it would be nice to keep those vertical gray lines next to the variable names ... I struggled to draw it in paint :-)当然,将那些垂直的灰线保留在变量名旁边会很好......我努力在油漆中绘制它:-)

library(ggplot2)

my_data <- data.frame(
  index = c(1L, 1L, 1L, 3L, 3L, 3L, 5L, 5L, 5L),
  es = c(0.14, -0.18, -0.46, -0.07, 0.42, 0.51, 0.56, 0.24, -0.35),
  lower_CI = c(-0.92, -1.29, -1.63, -1.16, -0.59, -0.5, -0.44, -0.79, -1.5),
  upper_CI = c(1.23, 0.86, 0.55, 0.99, 1.58, 1.69, 1.75, 1.36, 0.67),
  varname = as.factor(c("Crawl Legs (s)","Crawl Legs (s)","Crawl Legs (s)",
                        "Crawl Arms (s)","Crawl Arms (s)","Crawl Arms (s)",
                        "Front Crawl (s)","Front Crawl (s)","Front Crawl (s)")),
  period = as.factor(c("TP/DP","TP/RP","DP/RP","TP/DP","TP/RP","DP/RP","TP/DP","TP/RP","DP/RP"))
)
ggplot(my_data, aes(y = varname, x = es, color = period, fill = period)) +
  geom_point(shape = 21, size = 2, position=position_dodge(width = 0.5)) +  
  geom_errorbar(aes(xmin = lower_CI, xmax = upper_CI), width = 0.2, position=position_dodge(width = 0.5)) +
  scale_color_manual("period", values=c("#0073C2FF", "#EFC000FF", "#CD534CFF")) +
  geom_vline(xintercept = c(-2, -2, -1.2, -.6, -.2, 0, .2, .6, 1.2, 2, 2),
             color = c("grey", "grey", "grey", "grey", "grey", "black", "grey", "grey", "grey", "grey", "grey"), linetype = c("dashed", "dashed","dashed", "dashed", "dashed", "solid", "dashed","dashed", "dashed", "dashed", "dashed"),
             cex = c(.5, .5, .5, .5, .5, 1, .5, .5, .5, .5, .5), alpha = 0.5) +
  scale_x_continuous(limits=c(-2.8, 2.8), breaks=c(-2, -1.2, -.6, -.2, .2, .6, 1.2, 2), labels = unicode_minus) +
  scale_fill_manual(values = c("#0073C2FF", "#EFC000FF", "#CD534CFF")) +
  xlab("Standardized mean difference (±95% CI)") + 
  ylab("") + 
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.background = element_blank(),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line.y = element_blank(),
        axis.line.x = element_blank(),
        axis.ticks.y = element_line(size = 18, colour = "grey"),
        axis.ticks.x = element_blank(),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.text.x.bottom = element_text(size = 10, colour = "black"),
        axis.title.x = element_text(size = 12, colour = "black", vjust = -0.75),
        legend.position = c(0.08, 0.9), legend.title = element_blank(),
        legend.text = element_text(color = "black", size = 11))

在此处输入图像描述

You can use a geom_text layer and some segment annotations:您可以使用geom_text图层和一些段注释:

ggplot(my_data, aes(y = varname, x = es, color = period, fill = period)) +
  geom_point(shape = 21, size = 2, position=position_dodge(width = 0.5)) +  
  geom_errorbar(aes(xmin = lower_CI, xmax = upper_CI), width = 0.2, 
                position=position_dodge(width = 0.5)) +
  scale_color_manual("period", values=c("#0073C2FF", "#EFC000FF", "#CD534CFF")) +
  geom_vline(xintercept = c(-2, -2, -1.2, -.6, -.2, 0, .2, .6, 1.2, 2, 2),
             color = c(rep("grey", 5), "black", rep("grey", 5)),
             linetype = c(rep("dashed", 5), "solid", rep("dashed", 5)),
             cex = c(rep(0.5, 5), 1, rep(0.5, 5)), alpha = 0.5) +
  geom_text(aes(x = 2.3, label = paste0(es, " [", lower_CI, ", ", upper_CI, "]"),
                group = period), color = "black",
            position = position_dodge(width = 0.5), hjust = 0) +
  annotate("segment", x = rep(2.1, 3), xend = rep(2.1,  3),
           y = 0.75 + 0:2, yend = 0.25 + 1:3, size = 1.5, color = "gray") +
  scale_x_continuous(limits = c(-2.8, 3), 
                     breaks = c(-2, -1.2, -.6, -.2, .2, .6, 1.2, 2)) +
  scale_fill_manual(values = c("#0073C2FF", "#EFC000FF", "#CD534CFF")) +
  labs(x = "Standardized mean difference (±95% CI)", y = NULL) + 
  theme_minimal() +
  theme(panel.grid = element_blank(), 
        axis.ticks.y = element_line(size = 18, colour = "grey"),
        axis.ticks.x = element_blank(),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.text.x.bottom = element_text(size = 10, colour = "black"),
        axis.title.x = element_text(size = 12, colour = "black", vjust = -0.75),
        legend.position = c(0.08, 0.9), legend.title = element_blank(),
        legend.text = element_text(color = "black", size = 11))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM