[英]Split legend in ggarrange
I have a problem with scale of legend, because text of legend crosses borders of plot. Any idea, how can I fix it?我对图例比例有疑问,因为图例文本跨越了 plot 的边界。任何想法,我该如何解决? Split or resize?
拆分还是调整大小?
df <- df %>%
filter(!(is.na(review)))
df <- df %>%
mutate(state = case_when(state == 'California' ~ 'CA',
state == 'Texas' ~ 'TX',
state == 'New York' ~ 'NY',
state == 'Florida' ~ 'FL',
TRUE ~ state))
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))
Code of frames:框架代码:
California加州
ca <- df %>%
filter(state == 'CA') %>%
group_by(book) %>%
summarize(books_sold = table(book)) %>%
arrange(-books_sold) %>%
mutate(rank = 1:5)
New York纽约
ny <- df %>%
filter(state == 'NY') %>%
group_by(book) %>%
summarize(books_sold = table(book)) %>%
arrange(-books_sold) %>%
mutate(rank = 1:5)
Florida佛罗里达
fl <- df %>%
filter(state == 'FL') %>%
group_by(book) %>%
summarize(books_sold = table(book)) %>%
arrange(-books_sold) %>%
mutate(rank = 1:5)
Texas得克萨斯州
tx <- df %>%
filter(state == 'TX') %>%
group_by(book) %>%
summarize(books_sold = table(book)) %>%
arrange(-books_sold) %>%
mutate(rank = 1:5)
This is how I created data frames for each state.这就是我为每个 state 创建数据框的方式。
Code of plots:地块代码:
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
And result:结果:
As I suggested in my comment adding guides(fill = guide_legend(nrow =...))
to one of your plots would be one option to split the legend into multiple rows.正如我在我的评论中所建议的那样,将
guides(fill = guide_legend(nrow =...))
添加到您的一个地块将是将图例拆分为多行的一种选择。
As you provided no example data I created my own.由于您没有提供示例数据,我创建了自己的示例数据。
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
)
First, to reproduce your issue let's create some simple charts and use ggarrange
to glue them together, which as you can see results in a legend clipped off at the plot margins because of the long legend text.首先,为了重现您的问题,让我们创建一些简单的图表并使用
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
Now, adding guides(fill = guide_legend(nrow = 3))
to just one of your plots will split the legend into three rows:现在,将
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.