英文:
plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top") delivered duplicated legend
问题
Here's the translated content:
我使用了这个问题中的虚拟数据 <https://stackoverflow.com/questions/60879858/bar-plot-and-scatterplot-in-ggplot-facets>
我只需要一个图例,位于已修补图的顶部,所以我为面板a使用了 `theme(legend.position = "top")`,而对于面板b则使用了 `theme(legend.position = "none")`。
```R
library(ggplot2)
library(ggpubr)
library(ggpmisc)
library(reshape2)
## 为每个处理(A - D)生成连续变量
A <- rnorm(10, 1, 1)
B <- rnorm(10, 3, 1)
C <- rnorm(10, 5, 1)
D <- rnorm(10, 7, 1)
## 生成对处理的响应
res_A <- rnorm(10, 28, 3)
res_B <- rnorm(10, 22, 3)
res_C <- rnorm(10, 18, 3)
res_D <- rnorm(10, 12, 3)
## 为处理和响应建立数据框
treatments <- data.frame(A, B, C, D)
response <- data.frame(res_A, res_B, res_C, res_D)
## 将每个数据框转换为长格式
treatments <- treatments %>% gather(Treatment, cont_x, A:D)
response <- response %>% gather(Treatment, Response, res_A:res_D)
## 使用所需的数据创建最终数据框
data <- data.frame(treatments$Treatment, treatments$cont_x, response$Response)
colnames(data) <- c("Treatment", "X", "Response")
a <- data %>%
group_by(Treatment) %>%
summarise(Response=mean(Response)) %>%
mutate(se = sd(Response)/sqrt(length(Response))) %>%
ggplot(aes(x=Treatment,y = Response, fill = as.factor(Treatment))) +
geom_bar(position = "stack", stat= "identity") +
geom_text(aes(label=tolower(Treatment)), position=position_stack(vjust = 1.3)) +
geom_errorbar(aes(ymin = Response - se, ymax = Response + se), width = 0.1) +
labs(fill="TREATMENT EFFECTS") + theme(legend.position = "top")
b <- data %>%
ggplot(aes(x=X,y=Response)) +
geom_smooth(method="lm") +
geom_point(aes(color = as.factor(Treatment)), size=3) +
scale_fill_manual(values = cols) +
labs(color = "TREATMENT EFFECTS") +
xlab("Continuous Variable") +
stat_poly_eq(formula = y ~ x, label.x = 0.9, label.y = 0.95, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE) +
theme(legend.position = "none")
在下面的代码中,`theme(legend.position = "top")` 忽略了 `plot_layout(guides = "collect")`
a + b +
plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top")
[![enter image description here][1]][1]
我可以使用默认设置来仅显示一个图例。
a + b +
plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect")
[![enter image description here][2]][2]
感谢您的时间。
[1]: https://i.stack.imgur.com/4ZEQI.png
[2]: https://i.stack.imgur.com/cerPh.png
Please note that I've translated the code part and left out the specific URL links and image descriptions as requested.
英文:
I used dummy data from this question <https://stackoverflow.com/questions/60879858/bar-plot-and-scatterplot-in-ggplot-facets>
I need one legend only, at the top of the patched graph, so I have theme(legend.position = "top")
for panel a and theme(legend.position = "none")
for panel b.
library(ggplot2)
library(ggpubr)
library(ggpmisc)
library(reshape2)
## GENERATE CONTINUOUS VARIABLES FOR EACH TREATMENT (A - D)
A <- rnorm(10, 1, 1)
B <- rnorm(10, 3, 1)
C <- rnorm(10, 5, 1)
D <- rnorm(10, 7, 1)
## GENERATE RESPONSE TO TREATMENTS
res_A<-rnorm(10, 28, 3)
res_B<-rnorm(10, 22, 3)
res_C<-rnorm(10, 18, 3)
res_D<-rnorm(10, 12, 3)
## ESTABLISH DATA FRAMES FOR TREATMENTS AND RESPONSE
treatments<-data.frame(A, B, C, D)
response<-data.frame(res_A, res_B, res_C, res_D)
## CONVERT EACH DATA FRAME TO LONG FORM
treatments <-treatments %>% gather(Treatment, cont_x, A:D)
response <-response %>% gather(Treatment, Response, res_A:res_D)
## CREATE FINAL DATA FRAME WITH REQUIRED DATA
data<-data.frame(treatments$Treatment, treatments$cont_x, response$Response)
colnames(data) <- c("Treatment", "X", "Response")
a <- data %>%
group_by(Treatment) %>%
summarise(Response=mean(Response)) %>%
mutate(se = sd(Response)/sqrt(length(Response))) %>% ungroup %>%
ggplot(aes(x=Treatment,y = Response, fill = as.factor(Treatment))) +
geom_bar(position = "stack", stat= "identity") +
geom_text(aes(label=tolower(Treatment)), position=position_stack(vjust = 1.3)) +
geom_errorbar(aes(ymin = Response - se, ymax = Response + se), width = 0.1) +
labs(fill="TREATMENT EFFECTS") + theme(legend.position = "top")
b <- data %>%
ggplot(aes(x=X,y=Response)) +
geom_smooth(method="lm") +
geom_point(aes(color = as.factor(Treatment)), size=3) +
scale_fill_manual(values = cols) +
labs(color = "TREATMENT EFFECTS") +
xlab("Continuous Variable") +
stat_poly_eq(formula = y ~ x, label.x = 0.9, label.y = 0.95, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE) +
theme(legend.position = "none")
In the following code, theme(legend.position = "top")
disregarded plot_layout(guides = "collect")
a + b +
plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top")
I can have one legend only by using the default.
a + b +
plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect")
Thanks for your time.
答案1
得分: 2
以下是要翻译的内容:
问题在于,使用 & theme(...)
进行的所有主题调整将分别应用于补丁的每个绘图,因此将覆盖之前进行的任何调整,即使用 & theme(legend.position = "top")
,您将覆盖 theme(legend.position = "none")
对于您的 b
绘图,这两个绘图最终都会在顶部具有一个图例,由于它们不同而无法合并。
但是,补丁具有一个总体的 theme
,可以通过 plot_annotation
的 theme
参数来设置,以实现您期望的结果:
library(patchwork)
a + b +
plot_annotation(
tag_levels = "a",
theme = theme_grey() + theme(legend.position = "top")
) +
plot_layout(guides = "collect")
<details>
<summary>英文:</summary>
The issue is that all theme adjustments done using e.g. `& theme(...)` will be applied individually to each plot of the patch and hence will override any adjustments you have done before, i.e. using `& theme(legend.position = "top")` you override the `theme(legend.position = "none")` for your `b` plot and both plots will end up with a legend on top which will not get merged as they differ.
However, the patch has an overall `theme` which could be set via the `theme` argument of `plot_annotation` to achieve your desired result:
library(patchwork)
a + b +
plot_annotation(
tag_levels = "a",
theme = theme_grey() + theme(legend.position = "top")
) +
plot_layout(guides = "collect")
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/btdhn.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论