首页 > 解决方案 > 在点须图之外添加其他文本

问题描述

我有一个整洁的数据框,其中包含多个模型,我将其绘制为点须图。下面我重新创建了一个类似的数据框。此图需要说明有关预测变量的信息的标题,理想情况下它会位于图例下方。我曾尝试将其作为标题、标签和注释,每次在侧面添加括号都会导致格式出现问题。有没有办法在不引起格式问题的括号的情况下在此处添加其他文本?

#create dataframe
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
#create dotwhisker
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 = model)))
#add graph elements
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)) + #here is where I've tried using plot.caption and plot.tag, plot.tag.position
  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)
#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) 

图表图像:

在此处输入图像描述

标签: rggplot2textlegend

解决方案


我不确定您所说的格式混乱是什么意思。您可以像这样在图例下添加标题:

 full_plot + 
 labs(caption = "Here's a caption explaining
                 what the different colors        
                 and shapes represent           ") + 
 theme(plot.caption = element_text(vjust = 70, hjust = 0.9, size = 12))

在此处输入图像描述


推荐阅读