Add number of observations per group in barplot (ggplot2)
By : Andrei Arkhanguelski
Date : July 31 2020, 09:00 PM

To fix the issue you can do As you have only one value to plot as a label per interval, you can filter your dataframe using distinct function from dplyr and set a single y value to plot it: code :
library(tidyverse)
Euk_one %>%
drop_na(`Length Stretch`) %>%
mutate_at(c("GGA","GGG","GGT","GGC"),~.x/`Length Stretch`) %>%
group_by(interval) %>% dplyr::select(c(interval, "GGA","GGG","GGT","GGC", Obs))%>%
summarize_all(mean) %>%pivot_longer(c(interval, Obs)) %>%
ggplot(aes(x=interval, y=value, fill=name)) + geom_col() +
ggtitle("Eukarya 1 Codon Stretches  Distribution of codons ")+
labs(fill= "Codons") +
theme(axis.text.x = element_text(angle = 45)) +
geom_text(inherit.aes = FALSE, data = . %>% distinct(interval, Obs),
aes(x = interval, y = 0.25, label=Obs))
Share :

How to add a number of observations per group and use group mean in ggplot2 boxplot?
By : Nomide
Date : March 29 2020, 07:55 AM
I wish this helpful for you Is this anything like what you're after? With stat_summary, as requested: code :
# function for number of observations
give.n < function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
# function for mean labels
mean.n < function(x){
return(c(y = median(x)*0.97, label = round(mean(x),2)))
# experiment with the multiplier to find the perfect position
}
# plot
ggplot(mtcars, aes(factor(cyl), mpg, label=rownames(mtcars))) +
geom_boxplot(fill = "grey80", colour = "#3366FF") +
stat_summary(fun.data = give.n, geom = "text", fun.y = median) +
stat_summary(fun.data = mean.n, geom = "text", fun.y = mean, colour = "red")

Add number of observations per group in ggplot2 boxplot
By : user3271035
Date : March 29 2020, 07:55 AM
Does that help Following this question: How to add a number of observations per group and use group mean in ggplot2 boxplot?, I want to add number of observations per group in ggplot boxplot too. But I have added a colour into aes mapping. , You can just use position: code :
p < ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p
p2 < ggplot(mtcars, aes(factor(vs), mpg, colour = factor(cyl))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p2

Add a number of observations per group AND SUBGROUP in ggplot2 boxplot
By : Swatantra Soni
Date : March 29 2020, 07:55 AM
Any of those help This might seem like a duplicate of this question, but in fact I want to expand the original question. , is it what you want? code :
require(ggplot2)
give.n < function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(gear))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median, position=position_dodge(width=0.75))

Add group mean line to barplot with ggplot2
By : yaro
Date : March 29 2020, 07:55 AM
may help you . I combined the solution from @bouncyball with my original approach using `geom_errorbar. Here is the code: code :
df.mean = df %>%
group_by(class) %>%
mutate(ymean = mean(y))
ggplot(df, aes(x, y, fill=class)) +
geom_col() +
geom_errorbar(data=df.mean, aes(x, ymax = ymean, ymin = ymean),
size=0.5, linetype = "longdash", inherit.aes = F, width = 1)

Barplot of numerical categorical data of number of observations ordered numerically in matplotlib
By : man
Date : March 29 2020, 07:55 AM
I wish this help you I assume you want to order the bars by the value of the index. That can be accomplished by using sort_index(): code :
df['income_cat'].value_counts().sort_index().plot(kind='bar',sort_columns=True)



Related Posts :
