如何在ggplot2和patchwork中共享轴标签

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

How to have common axis labels with ggplot2 and patchwork

问题

以下是翻译好的部分:

给定一个包含以下变量的数据框 MenRG_Final

名称 描述
P1DistanceRun 球员跑动的距离
PointNumber 球分编号
PointServer 发球球员
PointWinner 赢得球分的球员
ServeDepth 发球深度
ServeNumber 发球编号
ServeWidth 发球方向
Speed_KMH 发球速度

我想要使用 ggplot2 制作以下图表,其中包含两个 geom_boxplot,共享图例和轴标签:

如何在ggplot2和patchwork中共享轴标签

到目前为止,我已经制作了以下图表:

如何在ggplot2和patchwork中共享轴标签

使用以下的 R 代码:

library(tidyverse)
library(patchwork)

theme_set(theme_minimal())
p1 <- ggplot(
  MenRG_Final %>% filter(PointWinner == "Rafael Nadal"), 
  aes(x = PointServer, P1DistanceRun)
) +
  geom_boxplot(aes(fill = ServeNumber)) +
  labs(
    title = "Nadal a gagné le point"
  )

p2 <- ggplot(
  MenRG_Final %>% filter(PointWinner == "Stan Wawrinka"), 
  aes(x = PointServer, P1DistanceRun)
) +
  geom_boxplot(aes(fill = ServeNumber)) + 
  labs(
    title = "Wawrinka a gagné le point",
  ) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

(p1 + p2) + plot_layout(guides = "collect") + ylim(0, 60) + theme(legend.position = "top") + labs(x = "Serveur", y = "Distance Parcourue")

特别是,我想知道如何:

  • 更改图例的标题和标签,并将其放在图的最顶部?
  • 使用 patchwork 使轴标签共享?

如果您想使用数据进行实验,我已将其放在数据集文件夹中,链接在这里:https://github.com/Mathieu-R/ggplot2/tree/main

英文:

Given a dataframe MenRG_Final containing the following variables:

Name Description
P1DistanceRun Distance covered by the player
PointNumber Point number
PointServer Point server
PointWinner Point winner
ServeDepth Serve depth
ServeNumber Serve number
ServeWidth Serve direction
Speed_KMH Serve speed

I would like to make the following plot with ggplot2 consisting in two geom_boxplot with common legend and axis labels:

如何在ggplot2和patchwork中共享轴标签

So far, I've been able to make this plot:

如何在ggplot2和patchwork中共享轴标签

with this R code:

library(tidyverse)
library(patchwork)

theme_set(theme_minimal())
p1 &lt;- ggplot(
  MenRG_Final %&gt;% filter(PointWinner == &quot;Rafael Nadal&quot;), 
  aes(x = PointServer, P1DistanceRun)
) +
  geom_boxplot(aes(fill = ServeNumber)) +
  labs(
    title = &quot;Nadal a gagn&#233; le point&quot;
  )

p2 &lt;- ggplot(
  MenRG_Final %&gt;% filter(PointWinner == &quot;Stan Wawrinka&quot;), 
  aes(x = PointServer, P1DistanceRun)
) +
  geom_boxplot(aes(fill = ServeNumber)) + 
  labs(
    title = &quot;Wawrinka a gagn&#233; le point&quot;,
  ) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  )

(p1 + p2) + plot_layout(guides = &quot;collect&quot;) &amp; ylim(0, 60) &amp; theme(legend.position = &quot;top&quot;) &amp; labs(x = &quot;Serveur&quot;, y = &quot;Distance Parcourue&quot;)

In particular, I'm wondering how can I:

  • Change the legend title and labels and put it at the very top of the plot ?
  • Have common axis labels using patchwork ?

If you want to play with the data, I made it available in the dataset folder here: https://github.com/Mathieu-R/ggplot2/tree/main

答案1

得分: 1

以下是您要翻译的内容:

"如评论中所述,您可以通过使用分面轻松实现您期望的结果,而且代码更少:

library(ggplot2)

theme_set(theme_minimal())

ggplot(
  MenRG_Final,
  aes(x = PointServer, P1DistanceRun)
) +
  geom_boxplot(aes(fill = ServeNumber)) +
  scale_fill_discrete(labels = c("Premier Service", "Deuxième Service")) +
  facet_wrap(~PointWinner, labeller = labeller(
    PointWinner = c(
      "Rafael Nadal" = "纳达尔赢得了分数",
      "Stan Wawrinka" = "瓦林卡赢得了分数"
    )
  )) +
  theme(legend.position = "top") +
  labs(x = "发球者", y = "跑动距离", fill = NULL)

如何在ggplot2和patchwork中共享轴标签

"

英文:

As outlined in the comments you could achieve your desired result easily and with less code by using facetting:

library(ggplot2)

theme_set(theme_minimal())

ggplot(
  MenRG_Final,
  aes(x = PointServer, P1DistanceRun)
) +
  geom_boxplot(aes(fill = ServeNumber)) +
  scale_fill_discrete(labels = c(&quot;Premier Service&quot;, &quot;Deuxi&#232;me Service&quot;)) +
  facet_wrap(~PointWinner, labeller = labeller(
    PointWinner = c(
      &quot;Rafael Nadal&quot; = &quot;Nadal a gagn&#233; le point&quot;,
      &quot;Stan Wawrinka&quot; = &quot;Wawrinka a gagn&#233; le point&quot;
    )
  )) +
  theme(legend.position = &quot;top&quot;) +
  labs(x = &quot;Serveur&quot;, y = &quot;Distance Parcourue&quot;, fill = NULL)

如何在ggplot2和patchwork中共享轴标签

huangapple
  • 本文由 发表于 2023年5月24日 20:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323337.html
匿名

发表评论

匿名网友

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

确定