plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top") delivered duplicated legend

huangapple go评论77阅读模式
英文:

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 = &quot;top&quot;) for panel a and theme(legend.position = &quot;none&quot;) for panel b.

library(ggplot2)
library(ggpubr)
library(ggpmisc)
library(reshape2)

  ## GENERATE CONTINUOUS VARIABLES FOR EACH TREATMENT (A - D)

        A &lt;- rnorm(10, 1, 1)
        B &lt;- rnorm(10, 3, 1)
        C &lt;- rnorm(10, 5, 1)
        D &lt;- rnorm(10, 7, 1)

        ## GENERATE RESPONSE TO TREATMENTS

        res_A&lt;-rnorm(10, 28, 3)
        res_B&lt;-rnorm(10, 22, 3)
        res_C&lt;-rnorm(10, 18, 3)
        res_D&lt;-rnorm(10, 12, 3)

        ## ESTABLISH DATA FRAMES FOR TREATMENTS AND RESPONSE

        treatments&lt;-data.frame(A, B, C, D)
        response&lt;-data.frame(res_A, res_B, res_C, res_D)

        ## CONVERT EACH DATA FRAME TO LONG FORM



        treatments &lt;-treatments %&gt;% gather(Treatment, cont_x, A:D)
        response &lt;-response %&gt;% gather(Treatment, Response, res_A:res_D)

        ## CREATE FINAL DATA FRAME WITH REQUIRED DATA

        data&lt;-data.frame(treatments$Treatment, treatments$cont_x, response$Response)
        colnames(data) &lt;- c(&quot;Treatment&quot;, &quot;X&quot;, &quot;Response&quot;)
        


a &lt;- data %&gt;% 
       group_by(Treatment) %&gt;% 
       summarise(Response=mean(Response)) %&gt;% 
       mutate(se = sd(Response)/sqrt(length(Response))) %&gt;% ungroup %&gt;%
     ggplot(aes(x=Treatment,y = Response, fill = as.factor(Treatment))) +
      geom_bar(position = &quot;stack&quot;, stat= &quot;identity&quot;)  +
       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=&quot;TREATMENT EFFECTS&quot;) + theme(legend.position = &quot;top&quot;)

b &lt;- data %&gt;% 
     ggplot(aes(x=X,y=Response)) + 
      geom_smooth(method=&quot;lm&quot;) + 
      geom_point(aes(color = as.factor(Treatment)), size=3) +
      scale_fill_manual(values = cols) +
      labs(color = &quot;TREATMENT EFFECTS&quot;) +
      xlab(&quot;Continuous Variable&quot;) +
      stat_poly_eq(formula = y ~ x, label.x = 0.9, label.y = 0.95, aes(label = paste(..eq.label.., ..rr.label.., sep = &quot;~~~&quot;)), parse = TRUE)  +
   theme(legend.position = &quot;none&quot;)

In the following code, theme(legend.position = &quot;top&quot;) disregarded plot_layout(guides = &quot;collect&quot;)

a + b  +
  plot_annotation(tag_levels = &#39;a&#39;) + plot_layout(guides = &quot;collect&quot;)  &amp;  theme(legend.position = &quot;top&quot;)   

plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top") delivered duplicated legend

I can have one legend only by using the default.

a + b  +
  plot_annotation(tag_levels = &#39;a&#39;) + plot_layout(guides = &quot;collect&quot;) 

plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top") delivered duplicated legend

Thanks for your time.

答案1

得分: 2

以下是要翻译的内容:

问题在于,使用 &amp; theme(...) 进行的所有主题调整将分别应用于补丁的每个绘图,因此将覆盖之前进行的任何调整,即使用 &amp; theme(legend.position = &quot;top&quot;),您将覆盖 theme(legend.position = &quot;none&quot;) 对于您的 b 绘图,这两个绘图最终都会在顶部具有一个图例,由于它们不同而无法合并。

但是,补丁具有一个总体的 theme,可以通过 plot_annotationtheme 参数来设置,以实现您期望的结果:

library(patchwork)

a + b +
  plot_annotation(
    tag_levels = "a",
    theme = theme_grey() + theme(legend.position = "top")
  ) +
  plot_layout(guides = "collect")

plot_annotation(tag_levels = 'a') + plot_layout(guides = "collect") & theme(legend.position = "top") delivered duplicated legend



<details>
<summary>英文:</summary>

The issue is that all theme adjustments done using e.g. `&amp; theme(...)` will be applied individually to each plot of the patch and hence will override any adjustments you have done before, i.e. using `&amp;  theme(legend.position = &quot;top&quot;)` you override the `theme(legend.position = &quot;none&quot;)` 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>



huangapple
  • 本文由 发表于 2023年5月18日 01:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定