How to make and enhance barplot with data labels, error bar and legend using R programming?

Опубликовано: 10 Май 2026
на канале: Rajendra Choure
9,098
154

#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:
[email protected]

YouTube 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")