How to make and enhance barplot with data labels, error bar and legend using R programming?
HTML-код
- Опубликовано: 25 июл 2022
- #barplot #rprogramming #datavisualization #barcharts #errorbars
In this video I discussed how to get a complete publication ready grouped barplot using base R function.
To get the summarize data to plot bars and std. dev for error bars , i have used summarise _all function of dplyr.
Adjustment of margin, y axis limits and Orientation of labels to get text accommodated is also discussed.
Facebook page:
/ rajendrachoureisc
Mail Id:
rajuchoure@gmail.com
RUclips playlist:
• R programming tutorials
Script
#barplot with error bars
#datasummary
head(iris)
library(dplyr)
iris_sum_mean = group_by(iris, Species)
iris_sum_mean = summarise_all(iris_sum_mean,mean)
iris_sum_mean_mat = as.matrix(iris_sum_mean[,-1])
iris_sum_sd =group_by( iris, Species)
iris_sum_sd =summarise_all(iris_sum_sd ,sd)
iris_sum_sd_mat =as.matrix(iris_sum_sd[,-1])
#barplot
par(mar=c(7,3,2,2))
gbp = barplot(iris_sum_mean_mat,beside=TRUE,las=2, col=terrain.colors(3),ylim=c(0,7.5),main="barplot with errorbars")
text(x=gbp, y=iris_sum_mean_mat+0.15,labels = round(iris_sum_mean_mat,2), cex=0.6)
#Errorbars
arrows(x0=gbp, x1=gbp,y0=iris_sum_mean_mat-iris_sum_sd_mat,y1=iris_sum_mean_mat+iris_sum_sd_mat, code=3, angle=90,length=0.05)
#legend
legend(x="topright", fil=terrain.colors(3),legend=iris_sum_mean$Species,bty="n")
well explained.
always perfect.
Well done! Always enjoy watching base R do just as well as tidyverse options.
Tidyverse extends functionality.
Thank you so much
Your youtube channel is the best resources I came across so far for learning R. Please keep teaching us. God bless you!
Thank you very much for this appreciation. It motivates a lot.
Thanks very much Dr. Rajendra
you are welcome!
Thanks. Hw to reorder legends such as virginica, versicolor, and setosa?
Sorry for the delay in answeing, add layer - scale_fill_discrete(breaks=c("virginica", "versicolor", "setosa"))
library(ggplot2)
library(ggplot2)
ggplot(data=iris, aes(x=Species, y=Sepal.Length, fill=Species)) +
geom_boxplot() +
scale_fill_discrete(breaks=c("virginica", "versicolor", "setosa"))
Mr choure ur videos helpful my thesis writting. I need one help please upload two y axis graph with line with bar
Ok. I will soon.
year