首页 > 解决方案 > ggplot R中的X轴子标签

问题描述

我有以下 ggplot 代码呈现下面的箱形图。

ggplot(comparisonData, aes(Group,score)) + 
  geom_boxplot(notch = TRUE, varwidth = TRUE, aes(colour = Group)) +
  geom_jitter(width = 0.2, aes(colour = Group)) + 
  theme(legend.position = "none") + 
  labs(title="User Engagement Score", x="Condition", y="Score (max 140)")

在此图中,我希望将 x 轴上的第 1 组和第 2 组重命名为“隐形”和“非隐形”,但我无法找到这样做的方法。是否可以不更改数据中的组名?

在此处输入图像描述

标签: rggplot2

解决方案


您可以通过比例更改标签,例如

library(tidyverse)
library(palmerpenguins)

penguins %>% 
  na.omit() %>%
  mutate(species = factor(ifelse(species == "Adelie", 1, 2))) %>% 
  ggplot(aes(x = species, y = bill_length_mm)) +
  geom_boxplot(aes(colour = species), notch = TRUE, varwidth = TRUE) +
  geom_jitter(width = 0.2, aes(colour = species)) + 
  theme(legend.position = "none") + 
  labs(title="User Engagement Score", x="Condition", y="Score (max 140)") +
  scale_x_discrete(label = c("Stealth", "Non-stealth"))

例子.png


推荐阅读