Home > OS >  How do I change the y-axis labels of the first plot in a grouped_ggwithinstats() dplyr ggplot?
How do I change the y-axis labels of the first plot in a grouped_ggwithinstats() dplyr ggplot?

Time:01-10

I would like to have the same y-axis for the left plot as for the right one.

q1_g <- grouped_ggwithinstats(
data            = dplyr::filter(df, Gender %in% c("Male", "Female"), Test %in% c("1. Pre", "2. Post")),
x               = Test,
y               = Ques.1,
xlab="",
ylab="",
effsize.type = "d",
pairwise.comparisons = "significant",
mean.plotting = TRUE,
results.subtitle = TRUE,
type            = "p",
grouping.var    = Gender,
outlier.tagging = FALSE,
)   
ggplot2::scale_y_continuous(
limits = c(0, 2),
breaks = seq(from = 0, to = 2, by = 1),
labels = c("Agree","Somehow agree","Not agree")
)

I was hoping to change the labels with ggplot_build, but ggplot_build(q1_g)$layout$panel_params[1]$y$get_labels() gives the correct labels.

Where could I find the labels for the first graph? Thank you in advance!

CodePudding user response:

What you could do is create multiple plots based on your groups. Since you haven't provided a reproducible dataset, I will use the built-in dataset bugs_long where the grouped variable is gender. So you will create two plots one for Male and one for Female with each a different y label. You could use plot_grid to combine them later like this:

library(ggstatsplot)

p1 <- grouped_ggwithinstats(
  data = bugs_long[bugs_long$gender == "Male",],
  x = condition,
  y = desire,
  grouping.var = gender,
  xlab = "Continent",
  ylab = "Your label of plot1",
  type = "nonparametric", 
  pairwise.display = "significant", 
  p.adjust.method = "BH", 
  package = "ggsci",
  palette = "default_jco",
  outlier.tagging = TRUE,
  outlier.label = education,
  k = 3,
  annotation.args = list(title = "Desire to kill bugs across genders"),
  plotgrid.args = list(ncol = 1)
) 

p2 <- grouped_ggwithinstats(
  data = bugs_long[bugs_long$gender == "Female",],
  x = condition,
  y = desire,
  grouping.var = gender,
  xlab = "Continent",
  ylab = "Your label of plot2",
  type = "nonparametric", 
  pairwise.display = "significant", 
  p.adjust.method = "BH", 
  package = "ggsci",
  palette = "default_jco",
  outlier.tagging = TRUE,
  outlier.label = education,
  k = 3,
  annotation.args = list(title = "Desire to kill bugs across genders"),
  plotgrid.args = list(ncol = 1)
) 
library(cowplot)
plot_grid(p1, p2, ncol = 1)

Created on 2023-01-09 with reprex v2.0.2

As you can see two different y-axis labels.

  • Related