簡體   English   中英

在 ggarrange 中拆分圖例

[英]Split legend in ggarrange

我對圖例比例有疑問,因為圖例文本跨越了 plot 的邊界。任何想法,我該如何解決? 拆分還是調整大小?

數據清洗:

過濾數據

df <- df %>%
  filter(!(is.na(review)))

將state改成郵政編碼

df <- df %>%
  mutate(state = case_when(state == 'California' ~ 'CA',
                           state == 'Texas' ~ 'TX',
                           state == 'New York' ~ 'NY',
                           state == 'Florida' ~ 'FL',
                           TRUE ~ state))

編碼審查專欄並添加新的 high_review

df <- df %>%
  mutate(review = case_when(review == 'Poor' ~ 1,
                   review == 'Fair' ~ 2,
                   review == 'Good' ~ 3,
                   review == 'Great' ~ 4,
                   review == 'Excellent' ~ 5,),
         high_review = ifelse(review >= 4, TRUE, FALSE))

框架代碼:

加州

ca <- df %>%
  filter(state == 'CA') %>%
  group_by(book) %>%
  summarize(books_sold = table(book)) %>%
  arrange(-books_sold) %>%
  mutate(rank = 1:5)

紐約

ny <- df %>%
  filter(state == 'NY') %>%
  group_by(book) %>%
  summarize(books_sold = table(book)) %>%
  arrange(-books_sold) %>%
  mutate(rank = 1:5)

佛羅里達

fl <- df %>%
  filter(state == 'FL') %>%
  group_by(book) %>%
  summarize(books_sold = table(book)) %>%
  arrange(-books_sold) %>%
  mutate(rank = 1:5)

得克薩斯州

tx <- df %>%
  filter(state == 'TX') %>%
  group_by(book) %>%
  summarize(books_sold = table(book)) %>%
  arrange(-books_sold) %>%
  mutate(rank = 1:5)

這就是我為每個 state 創建數據框的方式。

地塊代碼:

ca_plot <- ggplot(data = ca, aes(x = reorder(rank, -books_sold), y = books_sold, fill = book))+
  geom_col()+
  geom_text(aes(label = books_sold), vjust = 1.1, size = 3)+
  ylab('Number of books sold')+
  xlab('Ranking')+
  ggtitle('California')+
  theme(legend.position = "none")

ny_plot <- ggplot(data = ny, aes(x = reorder(rank, -books_sold), y = books_sold, fill = book))+
  geom_col()+
  geom_text(aes(label = books_sold), vjust = 1.1, size = 3)+
  ylab('')+
  xlab('Ranking')+
  ggtitle('New York')+
  theme(legend.position = "none")  

fl_plot <- ggplot(data = fl, aes(x = reorder(rank, -books_sold), y = books_sold, fill = book))+
  geom_col()+
  geom_text(aes(label = books_sold), vjust = 1.1, size = 3)+
  ylab('Number of books sold')+
  xlab('Ranking')+
  ggtitle('Florida')+
  theme(legend.position = "none")  

tx_plot <- ggplot(data = tx, aes(x = reorder(rank, -books_sold), y = books_sold, fill = book))+
  geom_col()+
  geom_text(aes(label = books_sold), vjust = 1.1, size = 3)+
  ylab('')+
  xlab('Ranking')+
  ggtitle('Texas')+
  theme(legend.position = "none")  

all_plot <- ggplot()

final_plot <- ggarrange(ca_plot, ny_plot, fl_plot, tx_plot, ncol = 2, nrow = 2,
                        common.legend = TRUE)
final_plot

結果:

多重圖表

正如我在我的評論中所建議的那樣,將guides(fill = guide_legend(nrow =...))添加到您的一個地塊將是將圖例拆分為多行的一種選擇。

由於您沒有提供示例數據,我創建了自己的示例數據。

library(ggplot2)
library(ggpubr)

dat <- data.frame(
  book <- c(
    "Lorem ipsum dolor sit amet",
    "sem in libero class",
    "et posuere vehicula imperdiet dapibus",
    "et ipsum id ac",
    "Eleifend torquent sed egestas"
  ),
  books_sold = 1:5
)

首先,為了重現您的問題,讓我們創建一些簡單的圖表並使用ggarrange將它們粘合在一起,正如您所看到的那樣,由於圖例文本較長,圖例在 plot 邊距處被剪掉。

p1 <- p2 <- p3 <- p4 <- ggplot(dat, aes(as.numeric(factor(book)), books_sold, fill = book)) +
  geom_col() +
  theme(legend.position = "none")

final_plot <- ggarrange(p1, p2, p3, p4,
  ncol = 2, nrow = 2,
  common.legend = TRUE
)
final_plot

現在,將guides(fill = guide_legend(nrow = 3))添加到您的一個繪圖中會將圖例分成三行:

p1 <- p1 + guides(fill = guide_legend(nrow = 3))

final_plot <- ggarrange(p1, p2, p3, p4,
  ncol = 2, nrow = 2,
  common.legend = TRUE
)
final_plot

暫無
暫無

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

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