ggplot - легенда в качестве метки оси Y - PullRequest
0 голосов
/ 09 ноября 2018

У меня есть следующий график

enter image description here

Можно ли добавить надписи с надписями (HPD и Quantile) под соответствующими коробками? Также я могу избавиться от белого бара в середине?

Мой код следующий:

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")

Я, в принципе, хочу что-то подобное только для всех коробочных сюжетов: enter image description here

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")

1 Ответ

0 голосов
/ 09 ноября 2018

Я добавил больше данных, чтобы лучше воспроизвести ваш график. Вы можете используйте geom_text, чтобы добавить Method метки на график. Ты должен сохраняйте только одну метку на каждый участок, поэтому я создал datalabs dataframe. Также вам не нужно два 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")) 

enter image description here

...