Vipin Verma
Vipin Verma

Reputation: 5738

X-axis sub-labels in ggplot R

I have the following ggplot code that renders the box plot show below.

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)")

In this plot I want the groups 1 and 2 on the x-axis to be renamed as "Stealth" and "Non-stealth", but I am not able to find a way to do so. Is it possible without changing the group names in data?

enter image description here

Upvotes: 3

Views: 884

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26630

You can change the labels via the scale, e.g.

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"))

example.png

Upvotes: 2

Related Questions