【问题标题】:ggplot - legend as y-axis labelggplot - 图例作为 y 轴标签
【发布时间】:2019-04-13 02:54:36
【问题描述】:

我有以下图表

是否可以在相应的箱线图下添加图例标签(HPD 和分位数)?我也可以摆脱中间的白条吗?

我的代码如下:

p <- ggplot(Results.Baseline,aes(x=Inference, y=Results, fill=Method)) + 
  scale_y_continuous(limits = c(0, 1))+
  geom_boxplot()+facet_wrap(~Method)+ facet_wrap(~Model)+
  geom_hline(yintercept=0.95, linetype="dashed", color = "red")

我基本上想要在所有箱线图下面这样的东西:

Here is my data: 

   data <- structure(list(Results = c(0.234375, 0.203125, 0.234375, 0.203125, 
0.21875, 0.203125), Model = c("Baseline 1", "Baseline 1", "Baseline 1", 
"Baseline 1", "Baseline 1", "Baseline 1"), Method = c("Quantile", 
"Quantile", "Quantile", "Quantile", "Quantile", "Quantile"), 
    Inference = c("HMDM", "HMDM", "HMDM", "HMDM", "HMDM", "HMDM"
    )), .Names = c("Results", "Model", "Method", "Inference"), row.names = c("1:nrow(transitions)", 
"V2", "V3", "V4", "V5", "V6"), class = "data.frame")

【问题讨论】:

  • + theme(legend.position = "bottom")做你想做的事吗?
  • 我不清楚你在追求什么。您想在图例中的箱线图键下或图中的每个箱线图下添加标签还是...?您指的是两个面板之间的空间吗?如果是这样,请参阅theme() 中的panel.spacing
  • @aosmith 我添加了一张显示我所追求的图片
  • 需要数据来帮助,但要回答你关于删除中间白条的问题,这个主题会有所帮助 - ` theme(panel.spacing = unit(0, "lines"), panel.border = element_rect(fill = NA, color="white"))`
  • @Mike 谢谢,效果很好!我还添加了一些数据。

标签: r ggplot2 legend


【解决方案1】:

我添加了更多数据,以便更好地复制您的图表。你可以 使用geom_textMethod 标签添加到图表中。你必须 每个箱形图只保留一个标签,这就是我创建datalabs 的原因 数据框。此外,您的情节中不需要两个 facet_wraps。做 这有助于回答您的问题吗?

    data <- structure(list(Results = c(0.234375, 0.203125, 0.234375, 0.203125, 
        0.21875, 0.203125), Model = c("Baseline 1", "Baseline 1", "Baseline

 1", 
    "Baseline 1", "Baseline 1", "Baseline 1"), Method = c("Quantile", 
    "Quantile", "Quantile", "Quantile", "Quantile", "Quantile"), 
    Inference = c("HMDM", "HMDM", "HMDM", "HMDM", "HMDM", "HMDM"
    )), .Names = c("Results", "Model", "Method", "Inference"), row.names = c("1:nrow(transitions)", 
    "V2", "V3", "V4", "V5", "V6"), class = "data.frame")


    data2 <- structure(list(Results = c(0.234375, 0.203125, 0.234375, 0.203125, 
    0.21875, 0.203125), Model = c("Baseline 2", "Baseline 2", "Baseline 2", 
    "Baseline 2", "Baseline 2", "Baseline 2"), Method = c("HPD", 
    "HPD", "HPD", "HPD", "HPD", "HPD"), 
    Inference = c("Eco. Inf.", "Eco. Inf.", "Eco. Inf.", "Eco. Inf.",
                  "Eco. Inf.", "Eco. Inf."
    )), .Names = c("Results", "Model", "Method", "Inference"), row.names = c("1:nrow(transitions)", 
    "V2", "V3", "V4", "V5", "V6"), class = "data.frame")

    data3 <- rbind(data,data2)

    data4 <- mutate(data3, Method = ifelse(Method == "Quantile",
                                           "HPD","Quantile"),
                          Inference = ifelse(Inference == "HMDM","Eco. Inf.",
                                             "HMDM"))

    data5 <- rbind(data3,data4)

    datalabs <- data5 %>% 
                group_by(Method,Model) %>% 
                arrange(Method,Model) %>% 
                filter(row_number()==1)

    ggplot(data5,aes(x=Inference, y=Results, fill=Method)) + 
      scale_y_continuous(limits = c(0, 1))+
      geom_boxplot()+
      facet_wrap(~Model)+
      geom_hline(yintercept=0.95, linetype="dashed", color = "red")+
      geom_text(data = datalabs, aes(label=Method) ,
                nudge_y = -.1)+
      theme_bw() +
      theme(panel.grid = element_blank()) +
      theme(panel.spacing = unit(0, "lines"), 
            strip.background = element_blank(),
            panel.border = element_rect(fill = NA, color="white")) 

【讨论】:

  • 太完美了!非常感谢。
猜你喜欢
  • 1970-01-01
  • 2015-10-06
  • 2023-02-11
  • 1970-01-01
  • 2014-07-02
  • 2021-10-15
  • 2012-07-30
  • 1970-01-01
相关资源
最近更新 更多