Home > Mobile >  Add count of samples used below columns in ggplot
Add count of samples used below columns in ggplot

Time:01-25

I have a data frame that looks similar to the one below and I would like to add the number A, B and C used in in each column and state it on the x-axis. To get something like A(n=1), B(n=2), C(n=0) written below the columns instead of just A, B and C. How could this be done?


df <- tibble(X = c("A","A","B","B","C","C","B","B","B","C","C"),
             value = c(5,2,7,3,1,6,9,4,2,5,2),
             type = c("t","t","t","t","t","s","s","s","s","s", "s"),
             group = c("Group1","Group2","Group1","Group2","Group1","Group1","Group3","Group2","Group3","Group3", "Group3"))


df %>% ggplot(aes(X,value))  
  geom_col(aes(fill=type))  
  facet_grid(~group)

enter image description here

Hope I explained my self clear enough.

CodePudding user response:

How about this:

df <- tibble(X = c("A","A","B","B","C","C","B","B","B","C","C"),
             value = c(5,2,7,3,1,6,9,4,2,5,2),
             type = c("t","t","t","t","t","s","s","s","s","s", "s"),
             group = c("Group1","Group2","Group1","Group2","Group1","Group1","Group3","Group2","Group3","Group3", "Group3"))


all_cats <- expand_grid(X = c("A", "B", "C"), 
                        group = c("Group1", "Group2", "Group3"), 
                        type="s", 
                        value=0)

df %>% 
    full_join(all_cats) %>% 
    group_by(X, group) %>% 
    mutate(n = sum(value), 
           Xlab = paste0(X, "\n(n=", n, ")")) %>%  
           ggplot(aes(Xlab,value))  
    geom_col(aes(fill=type))  
    facet_grid(~group, scales = "free_x") 

enter image description here

CodePudding user response:

Here the solution:

library(tibble)
library(ggplot2)

# Your data.
df <- tibble(X = c("A","A","B","B","C","C","B","B","B","C","C"),
             value = c(5,2,7,3,1,6,9,4,2,5,2),
             type = c("t","t","t","t","t","s","s","s","s","s", "s"),
             group = c("Group1","Group2","Group1","Group2","Group1","Group1","Group3","Group2","Group3","Group3", "Group3"))

# Creating new column.
df$n = numeric(nrow(df)) # Pre-allocating memory.
df$n[df$X == "A"] = sum(df$X == "A")
df$n[df$X == "B"] = sum(df$X == "B")
df$n[df$X == "C"] = sum(df$X == "C")

df$n = paste(paste(df$X, paste("n", df$n, sep = "="), sep = "("), ")", sep = "")

# Plotting.
df %>% ggplot(aes(n, value))  
  geom_col(aes(fill=type))  
  labs(x = "X")   
  facet_grid(~group)

I just created a new column in df storing the labels you wanted, and used this new variable in aes(). I also renamed the x-axis, so that you do not have issues in that sense. Below the resulting graph:

enter image description here

  •  Tags:  
  • Related