簡體   English   中英

如何使用 ggplot 在圖形底部添加圖形圖例?

[英]How do I add a graphical legend to the bottom of a figure using ggplot?

我有興趣使用ggplot2創建一個類似於此處起草的圖形。 我已經使用ggplotfacet_wrap創建了頂部部分,但還沒有找到一種方法來將平鋪圖例添加到圖的底部。 下面列出的是一個假數據集的代碼和我到目前為止的嘗試,它產生了這個.

data1 <- data.frame(lower = c(0, 2, 1, 0, 4,
                              3, 2, 2, 3, 0),
                    upper = c(7, 6, 9, 10, 9,
                              10, 6, 5, 6, 10),
                    point = c(3.5, 4, 5, 5, 6.5,
                              6.5, 4, 3.5, 4.5, 5),
                    variable = c("Var 1", "Var 1", "Var 1", "Var 1", "Var 1",
                                 "Var 2", "Var 2", "Var 2", "Var 2", "Var 2"),
                    specification = c("Study 1A", "Study 1B", "Study 1C", "Study 2A", "Study 2B",
                                      "Study 1A", "Study 1B", "Study 1C", "Study 2A", "Study 2B"),
                    treatment_size = c(1, 2, 3, 1, 4,
                                       1, 2, 3, 1, 4),
                    treatment_info = c("No", "Yes", "Yes", "No", "Yes",
                                       "No", "Yes", "Yes", "No", "Yes"))        

rect <- data.frame(xmin = c("Study 1A", 
                            "Study 1A"),
                   xmax = c("Study 2B", 
                            "Study 2B"),
                   ymin = c(data1$lower[3], 
                            data1$lower[6]),
                   ymax = c(data1$upper[3], 
                            data1$upper[6]),
                   alpha = c(0.1, 0.1),
                   fill = c("blue", "blue"))

plot <- ggplot(data1, aes(x=specification, y=point)) +
  geom_errorbar(width=0.1, aes(ymin=lower, ymax=upper)) +
  geom_point(shape=21, size=0.5, fill="black") +
  ylab("Effect") +
  xlab("") +
  theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1)) +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            alpha = 0.1, fill = "blue",
            data = transform(rect, variable = c("Var 1", "Var 2")),
            inherit.aes = FALSE)

plot + facet_wrap(vars(variable), ncol = 1)

實現或至少接近您想要的結果的一種選擇是將圖例創建為第二個ggplot ,然后可以使用patchwork將其粘貼到您的主要 plot 上。 基本上這涉及一些數據爭論作為第一步,或多或少通過主題選項進行樣式設置以實現表格外觀。

注意:我還稍微調整了主 plot 以實現您作為所需結果示例添加的圖像中的外觀。

library(dplyr)
library(tidyr)
library(ggplot2)
library(patchwork)

plot <- ggplot(data1, aes(x = specification, y = point)) +
  geom_errorbar(width = 0.1, aes(ymin = lower, ymax = upper)) +
  geom_point(shape = 21, size = 0.5, fill = "black") +
  ylab("Effect") +
  xlab("") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
  geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = ymin, ymax = ymax),
    alpha = 0.1, fill = "blue",
    data = transform(rect, variable = c("Var 1", "Var 2")),
    inherit.aes = FALSE
  )

pmain <- plot + 
  facet_wrap(vars(variable), ncol = 1) +
  theme_void() +
  theme(strip.background.x = element_rect(fill = "grey95", color = NA),
        strip.text.x = element_text(margin = margin(t = 5, b = 5))
        )

data2 <- data1 |> 
  distinct(specification, treatment_size, treatment_info) |> 
  mutate(across(everything(), as.character)) |> 
  pivot_longer(-specification, names_prefix = "^treatment_") |> 
  mutate(fill = if_else(value %in% c("Yes", "No"), value, "grey"),
         label = if_else(value %in% c("Yes", "No"), NA_character_, value))

ptable <- ggplot(data2, aes(specification, name)) +
  geom_tile(aes(fill = fill), width = .975, height = .975, color = "black") +
  geom_text(aes(label = label)) +
  scale_fill_manual(values = c(Yes = "green", No = "red", grey = "grey95")) +
  scale_y_discrete(limits = c("size","info"), labels = c("Size", "Info"), expand = c(0, 0.5)) +
  scale_x_discrete(position = "top", expand = c(0, 0.5)) +
  theme_void() +
  guides(fill = "none") +
  theme(axis.text.x = element_text(margin = margin(t = 5, b = 5)),
        axis.text.y = element_text(margin = margin(l = 5, r = 5))) +
  coord_fixed(ratio = .5)

pmain / ptable

在此處輸入圖像描述

這是與 Stefan 類似的想法

plot <- ggplot(data1, aes(x=specification, y=point)) +
  geom_errorbar(width=0.1, aes(ymin=lower, ymax=upper)) +
  geom_point(shape=21, size=0.5, fill="black") +
  ylab("Effect") +
  xlab("") +
  theme(axis.text.x=element_text(angle=90, vjust=0.5, hjust=1)) +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            alpha = 0.1, fill = "blue",
            data = transform(rect, variable = c("Var 1", "Var 2")),
            inherit.aes = FALSE) +
  theme_minimal(base_size = 16) +
  theme(axis.text.x = element_blank(), 
        strip.background = element_rect(fill = "gray85", color = NA)) + 
  facet_wrap(vars(variable), ncol = 1)

plot2 <- ggplot(data1, aes(x = 1, y = 2, fill = treatment_info)) +
  geom_tile(color = "black", size = 0.5) +
  geom_text(aes(y = 1, label = treatment_size), check_overlap = TRUE, size = 5) +
  scale_y_continuous(limits = c(0.5, 2.5), expand = c(0, 0), name = "",
                     breaks = 1:2, labels = c("Size", "Count")) +
  facet_grid(~specification) +
  theme_minimal(base_size = 16) +
  scale_x_discrete(expand = c(0, 0), name = "") +
  scale_fill_manual(values = c("#d9ead3", "#e6b8af")) +
  theme(axis.text.y = element_text(),
        strip.background = element_rect(fill = "gray85", color = NA),
        legend.position = "none",
        panel.spacing = unit(0, "mm"),
        panel.background = element_rect(),
        panel.grid = element_blank(),) 

現在patchwork而成的plot

library(patchwork)

plot + plot2 + plot_layout(ncol = 1, heights = 2:1)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM