英文:
How to have common axis labels with ggplot2 and patchwork
问题
以下是翻译好的部分:
给定一个包含以下变量的数据框 MenRG_Final:
| 名称 | 描述 | 
|---|---|
| P1DistanceRun | 球员跑动的距离 | 
| PointNumber | 球分编号 | 
| PointServer | 发球球员 | 
| PointWinner | 赢得球分的球员 | 
| ServeDepth | 发球深度 | 
| ServeNumber | 发球编号 | 
| ServeWidth | 发球方向 | 
| Speed_KMH | 发球速度 | 
我想要使用 ggplot2 制作以下图表,其中包含两个 geom_boxplot,共享图例和轴标签:
到目前为止,我已经制作了以下图表:
使用以下的 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:
So far, I've been able to make this plot:
with this R code:
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")
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)
"
英文:
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("Premier Service", "Deuxième Service")) +
  facet_wrap(~PointWinner, labeller = labeller(
    PointWinner = c(
      "Rafael Nadal" = "Nadal a gagné le point",
      "Stan Wawrinka" = "Wawrinka a gagné le point"
    )
  )) +
  theme(legend.position = "top") +
  labs(x = "Serveur", y = "Distance Parcourue", fill = NULL)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论