I want to add count statistics to multiple boxplots for multiple columns with ggplot2
, I tried of using tally
from dplyr but count statistics is not right. How can I make it right? any quick idea to do this?
reproducible data and my attempt:
here is reproducible data and my attempt down below. Basically, I want to add count statistics like tot_#_Tool_A
, tot_#_Tool_B, tot_#
in each subplot. How can I do that in R? any quick idea to make this happen? thanks
ID <- c("DJ45","DJ46","DJ47","DJ48","DJ49","DJ53","DJ54","DJ55","DJ56","DJ57")
Tool <- c("Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_B", "Tool_B", "Tool_B", "Tool_B", "Tool_B")
Name <- c("CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP")
MS1 <- c(51,55,50,59,50,47,48,42,43,46)
MS2 <- c(13,11,14,11,10,17,18,17,20,21)
MS3 <- c(2,3,2,5,6,4,9,6,4,4)
MS4 <- c(16,13,14,11,16,16,18,16,19,15)
MS5 <- c(3,6,3,6,3,4,4,8,5,4)
MS6 <- c(7,7,5,5,8,9,8,6,6,9)
df <- data.frame(ID,Tool,Name,MS1,MS2,MS3,MS4,MS5,MS6)
my updated attempt:
library(reshape2)
library(dplyr)
df1_long <- melt(df, id.vars=c("ID","Tool"))
df1_long %>% group_by(Tool, variable)%>%
tally %>% ungroup %>% as.data.frame() %>%
setNames(c("tool", "cat_vars", "count")) %>%
{
bind_rows(., setNames(., c("tool", "cat_vars", "count")))
} %>% as.data.frame() %>%
ggplot(aes(x=tool,y=count,fill=tool))+
geom_boxplot() + labs(title="CMP") +facet_wrap(~variable)
but I didn't get correct expected boxplot where expecting count statistics didn't show up. Any idea to make this work? what's the issue in my code? any thoughts? thanks
goal:
I want to add count statistics like tot_#_Tool_A
, tot_#_Tool_B, tot_#
in each subplot. any idea?
desired output:
I am trying to get plot something like this post, whereas tot_#_Tool_A
, tot_#_Tool_B, tot_#
should be placed on the top of each subplot. How can I make this happen? thanks