Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Modifying Y axis to present number of observations in each group

I want my plot to display how many people/observations are within each group. How can I do this? I found this answer, however I had a hard time following it.
Modify x-axis labels in each facet

 Raincloud <- iris %>%
  dplyr::group_by(Species) %>%
  dplyr::mutate(mean = mean(Petal.Length),
         se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
  ungroup() %>%
  ggplot(aes(x=Petal.Length, y=Species)) +
  stat_slab(aes(fill = Species, )) +
   stat_dots(aes(color = Species), side = "bottom", shape = 16) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color"))  +
  geom_errorbar(aes(xmin = mean - 1.96 * se,
                    xmax = mean + 1.96 * se), width = 0.2) +
  stat_summary(fun=mean, geom="point", shape=16, size=3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top")+
  labs(title="Raincloud plot with ggdist")
  print(Raincloud + labs(x = "Petal Length"))  [![Result[1]][1]

Example of what I want to do

    RCsex <- uniquePeople %>%
  dplyr::group_by(sex) %>%
  dplyr::mutate(mean = mean(thresh.x),
         se = sd(thresh.x)/sqrt(length(thresh.x))) %>%
  ungroup() %>%
  ggplot(aes(x=thresh.x, y=sex)) +
  stat_slab(aes(fill = sex), scale = 0.5) +
   stat_dots(aes(color = sex), side = "bottom", shape = 16) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
  geom_errorbar(aes(xmin = mean - 1.96 * se,
                    xmax = mean + 1.96 * se), width = 0.2) +
  stat_summary(fun=mean, geom="point", shape=16, size=3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top")+
 scale_x_continuous(name="Estimated Nominal Detection Threshold (log10 units)", limits=c(-4.5, 0.5), 

breaks = seq(-4.5, 0.5, by = 0.5))+
labs(title="Raincloud plots showing odor detection thresholds stratified by sex")
print(RCsex + labs(y = "Sex"))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One option would be to add a new column to your dataset where you glue the number obs. per species to the species name. This column could then be mapped on y:

library(dplyr)
library(ggplot2)
library(ggdist)

iris %>%
  dplyr::group_by(Species) %>%
  dplyr::mutate(
    mean = mean(Petal.Length),
    se = sd(Petal.Length) / sqrt(length(Petal.Length)),
    species_y = paste0(Species, "\n(", n(), ")")
  ) %>%
  ungroup() %>%
  ggplot(aes(x = Petal.Length, y = species_y)) +
  stat_slab(aes(fill = Species)) +
  stat_dots(aes(color = Species), side = "bottom", shape = 16) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
  geom_errorbar(aes(
    xmin = mean - 1.96 * se,
    xmax = mean + 1.96 * se
  ), width = 0.2) +
  stat_summary(fun = mean, geom = "point", shape = 16, size = 3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top") +
  labs(title = "Raincloud plot with ggdist", x = "Petal Length")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading