Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying different x-tick labels for two facet groups in ggplot2

I have boxplots representing results of two methods, each with two validation approaches and three scenarios, to be plotted using ggplot2. Everything works fine, but I want to change the x-axis tick label to differentiate between the type of technique used in each group.

I used the following code:

data <- read.csv("results.csv", header = TRUE, sep=',')

ggplot() + 
  geom_boxplot(data = data, aes(x = Validation, y = Accuracy, fill = Scenario)) +
  facet_wrap(~ Method) +
  labs(fill = "")

where the structure of my data is as follows:

Method        Validation        Scenario       Accuracy
-------------------------------------------------------
Method 1      Iterations        Scenario 1     0.90
Method 1      Iterations        Scenario 2     0.80
Method 1      Iterations        Scenario 3     0.86
Method 1      Recursive         Scenario 2     0.82
Method 2      Iterations        Scenario 1     0.69
Method 2      Recursive         Scenario 3     0.75

and got the following plot:

enter image description here

I just want to change the first x-tick label (Iterations) in Method 1 and Method 2 into 100-iterations and 10-iterations, respectively.

I tried to add this code but that changes the labels for both groups.

+ scale_x_discrete(name = "Validation", 
                   labels = c("100-iterations", "Recursive", 
                              "10-iterations", "Recursive")) +

Thanks in advance.

like image 446
Taher A. Ghaleb Avatar asked Oct 20 '25 12:10

Taher A. Ghaleb


1 Answers

The ggplot package's facet options were not designed for varying axis labels / scales across facets (see here for a detailed explanation), but one work around in this instance would be to vary the underlying x-axis variable's values for different facets, & set scales = "free_x" in facet_wrap() so that only the relevant values are shown in each facet's x-axis:

library(ggplot2)
library(dplyr)

ggplot(data %>%
         mutate(Validation = case_when(Validation == "Recursive" ~ "Recursive",
                                       Method == "Method 1" ~ "100-iterations",
                                       TRUE ~ "10-iterations")),
       aes(x = Validation, y = Accuracy, fill = Scenario)) +
  geom_boxplot() +
  facet_wrap(~ Method, scales = "free_x")

plot

Data:

set.seed(1)
data <- data.frame(
  Method = rep(c("Method 1", "Method 2"), each = 100),
  Validation = rep(c("Iterations", "Recursive"), times = 100),
  Scenario = sample(c("Scenario 1", "Scenario 2", "Scenario 3"), 200, replace = TRUE),
  Accuracy = runif(200)
)
like image 182
Z.Lin Avatar answered Oct 23 '25 03:10

Z.Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!