Deniz
Deniz

Reputation: 5

How to adjust the Bar plot to make the bars smaller?

I need help to adjust the size of column width in a bar plot. I use the following R script to get a barplot with a p-value, but the width of the bars (column) is too wide. Thanks

library(ggpubr)
library(rstatix)

    df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df, 6)

# Statistical test
stat.test <- df %>%
  t_test(len ~ supp) %>%
  add_significance()
stat.test

# Box plots with p-values
bxp <- ggboxplot(df, x = "supp", y = "len", fill = "supp", 
palette = c("#00AFBB", "#E7B800"))


stat.test <- stat.test %>% add_xy_position(x = "supp")
bxp + 
  stat_pvalue_manual(stat.test, label = "p") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))

# Customize p-value labels using glue expression 
# https://github.com/tidyverse/glue
bxp + stat_pvalue_manual(
  stat.test, label = "T-test, p = {p}",
  vjust = -1, bracket.nudge.y = 1
) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))

Upvotes: 0

Views: 894

Answers (1)

elielink
elielink

Reputation: 1202

If it's just a matter of width, you can specify it in this way:

bxp <- ggboxplot(df, x = "supp", y = "len", fill = "supp", 
                 palette = c("#00AFBB", "#E7B800"),width = 0.5)


stat.test <- stat.test %>% add_xy_position(x = "supp")

bxp + stat_pvalue_manual(
  stat.test, label = "T-test, p = {p}",
  vjust = -1, bracket.nudge.y = 1
) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.15)))+
  scale_x_discrete(expand = c(2, 2))

then, using the rest it looks like this:

enter image description here

Is that what you wanted?

Upvotes: 0

Related Questions