首页 > 解决方案 > 在不改变标题格式的情况下,如何在 R 中将括号添加到点须?

问题描述

我有一个整洁的数据框,其中包含多个模型,我将其绘制为点须图。下面我重新创建了一个类似的数据框。此图需要说明有关预测变量的信息的标题,并且在没有括号时格式正确,但是当添加括号时,它会在左下角重复。有没有办法解决这个问题,或者发生这种情况的原因?我也有兴趣将标题放在情节的图例下方,但尝试使用注释和标签也会导致格式问题。感谢您的任何帮助!(此外,如果这看起来很奇怪,因为 'model' 和 'term' 已经相同,实际代码中的问题是标准化模型的某些名称以使颜色正确,但在这里不应该成为问题)。

results_df <- data.frame(
  model = c("Model 1","Model 1","Model 1","Model 1","Model 1", "Model 1",             
            "Model 2",  "Model 2",   "Model 2",  
            "Model 2",   "Model 2",   "Model 2",  
            "Model 3", "Model 3", "Model 3",
            "Model 3", "Model 3", "Model 3",
            "Model 2",   "Model 2",   "Model 2",  
            "Model 2",   "Model 2",   "Model 2",  
            "Model 2",   "Model 2",   "Model 2"),
  estimate = c(-0.4890,  0.0966, -0.0911, -0.1700,  0.3620,  0.1980, -2.0920,
               -1.1620, -1.6910,-1.5320, -0.8340, -1.4350,  0.8240,  0.9750,
               0.9650, 0.5210,  0.9190,  0.9560, -0.9580, -0.1950, -1.1470, 
               -2.6430,-1.7420, -2.2500, -2.9990, -1.8100, -1.8270),
  conf.low = c(-0.6,  0.0, -0.2,-0.6,  0.0, -0.2, -2.4, -1.8, -1.9, -2.4,
               -1.8, -1.9,  0.0,  0.0,  0.0,  0.0,  0.0,0.0, -3.0, -2.0,
               -1.9, -5.6, -3.6, -3.8, -3.0, -2.0, -1.9),
  conf.high = c(0.9, 1.1, 1.0, 0.9, 1.1, 1.0, 0.0, 0.5, 0.7, 0.0, 0.5, 0.7,
                0.9, 1.1, 1.0, 0.9, 1.1, 1.0, 0.0, 0.5, 0.7, 0.0,0.5, 0.7,
                0.0, 0.5, 0.7),
  term = c("A","B","C","A","B","C","A","B","C","A",
           "B","C","A","B","C","A","B","C","D","E",
           "F","G","H","I","J","K",
           "L"),
  fixed = c("Without Fixed Effects", "Without Fixed Effects", "Without Fixed Effects",
            "With Fixed Effects",    "With Fixed Effects",    "With Fixed Effects",   
            "Without Fixed Effects", "Without Fixed Effects", "Without Fixed Effects",
            "With Fixed Effects",    "With Fixed Effects",    "With Fixed Effects",   
            "Without Fixed Effects", "Without Fixed Effects", "Without Fixed Effects",
            "With Fixed Effects",    "With Fixed Effects",    "With Fixed Effects",   
            "Without Fixed Effects", "Without Fixed Effects", "Without Fixed Effects",
            "Without Fixed Effects", "Without Fixed Effects", "Without Fixed Effects",
            "Without Fixed Effects", "Without Fixed Effects", "Without Fixed Effects"),
  type = c("Model 1",              "Model 1",              "Model 1",             
           "Model 1",          "Model 1",              "Model 1",             
           "Model 2",  "Model 2",   "Model 2",  
           "Model 2",   "Model 2",   "Model 2",  
           "Model 3", "Model 3", "Model 3",
           "Model 3", "Model 3", "Model 3",
           "Model 2",   "Model 2",   "Model 2",  
           "Model 2",   "Model 2",   "Model 2",  
           "Model 2",   "Model 2",   "Model 2")
)
#recode model
results_df$model = results_df$type
#graph with all elements
full_graph = dwplot(results_df,
                    vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2), 
                    dot_args = list(aes(shape = fixed)),
                    whisker_args = list(aes(colour = type)))
#add aesthetics to graph
full_graph = full_graph +
  theme_bw() +
  theme(legend.justification=c(.02, .993),
        legend.background = element_rect(color="gray90"),
        plot.title = element_text(hjust = 0.52),
        plot.caption = element_text(size=8, hjust = 0)) + 
  xlab("Coefficient Estimate") +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  ggtitle("Graph Title") +
  scale_color_manual(name="Model",values=c("#52D871","#17D0E5","#EF5B3D"),
                     na.translate = F)+
  scale_shape_manual(name = "Shape",values=c(16,17,16,17,16,17), na.translate = F)+
  scale_fill_manual(name="Model",values = c("#52D871","#17D0E5","#EF5B3D"),
                    na.translate = F)+
  labs(caption = "Note:
- notes about predictors
- additional notes
- additional notes
- additional notes
")
#look at graph, no formatting issues
full_graph

括号后的图表

#add brackets
brackets = list(c("Bracket 1", "A", "C"), 
                c("Bracket 2", "D", "F"),
                c("Bracket 3", "G", "I"),
                c("Bracket 4","J","L"))
full_plot = full_graph %>% add_brackets(brackets)
#look at graph -- caption formatting now weird
full_plot[1]

标签: rggplot2brackets

解决方案


推荐阅读