I have a multiple bar plot with 5 bar plots.
barplot1=c(8,3,4,1)
barplot2=c(18,11,11,13)
barplot3=c(39,42,42,45)
barplot4=c(26,30,31,34)
barplot5=c(9,13,12,7)
data <- data.frame(barplot1,barplot2,barplot3,barplot4,barplot5)
percantage <- 50
# plotting multiple bar plots
barplot(as.matrix(data),
main="", yaxp=c(0, max(percantage), 5),
col = c("#b62528","#f49401","#f9e536","#018404"),
ylab="Percentage", xlab = "Elevation [m. a. s. l.]",
beside=T,
legend.text = c("CR","EN","VU","LC or NT"),
args.legend=list(bty="n",horiz=F, x="topright", inset = c(- 0.05, -0.1)),
)
How can I display the corresponding number above each Bar? And how can I include a label for each bar plot instead of the "barplot1, barplot2, barplot3, barplot4 and barplot5" expression?
CodePudding user response:
Just add the names.arg
argument.
labels = c("label1", "label2", "label3", "label4", "label5")
percantage <- 50
# plotting multiple bar plots
barplot(as.matrix(data),
main="", yaxp=c(0, max(percantage), 5),
col = c("#b62528","#f49401","#f9e536","#018404"),
ylab="Percentage", xlab = "Elevation [m. a. s. l.]",
beside=T,
legend.text = c("CR","EN","VU","LC or NT"),
names.arg = labels,
args.legend=list(bty="n",horiz=F, x="topright", inset = c(- 0.05, -0.1)),
)
CodePudding user response:
Please check the below code where we can use names.arg
and text
to get the expected output
barplot1=c(8,3,4,1)
barplot2=c(18,11,11,13)
barplot3=c(39,42,42,45)
barplot4=c(26,30,31,34)
barplot5=c(9,13,12,7)
data <- data.frame(barplot1,barplot2,barplot3,barplot4,barplot5)
percantage <- 55
# plotting multiple bar plots
bar <- barplot(as.matrix(data),
main="", ylim = c(0,percantage),
col = c("#b62528","#f49401","#f9e536","#018404"),
ylab="Percentage", xlab = "Elevation [m. a. s. l.]",
beside=T,
legend.text = c("CR","EN","VU","LC or NT"),
names.arg=c("group1","group2","group3","group4","group5"),
args.legend=list(bty="n",horiz=F, x="topright", inset = c(- 0.05, -0.1)),
)
text(bar, as.matrix(data) 2, labels = as.matrix(data))
Created on 2023-01-29 with reprex v2.0.2
CodePudding user response:
You could use text
with all the values in your dataframe as a vector by converting your data to a longer format using pivot_longer
and assign them to each bar like this:
barplot1=c(8,3,4,1)
barplot2=c(18,11,11,13)
barplot3=c(39,42,42,45)
barplot4=c(26,30,31,34)
barplot5=c(9,13,12,7)
data <- data.frame(barplot1,barplot2,barplot3,barplot4,barplot5)
percantage <- 50
library(dplyr)
library(tidyr)
values <- data %>% pivot_longer(cols = everything()) %>% arrange(name)
# plotting multiple bar plots
plot = barplot(as.matrix(data),
main="", yaxp=c(0, max(percantage), 5),
col = c("#b62528","#f49401","#f9e536","#018404"),
ylab="Percentage", xlab = "Elevation [m. a. s. l.]",
beside=T,
legend.text = c("CR","EN","VU","LC or NT"),
ylim = c(0, 50),
args.legend=list(bty="n",horiz=F, x="topright", inset = c(-0.05, -0.1)),
)
text(x = plot, y = values$value, label = values$value, pos = 3)
Created on 2023-01-29 with reprex v2.0.2
To add percentage to labels you could use paste0
with percent sign like this:
plot = barplot(as.matrix(data),
main="", yaxp=c(0, max(percantage), 5),
col = c("#b62528","#f49401","#f9e536","#018404"),
ylab="Percentage", xlab = "Elevation [m. a. s. l.]",
beside=T,
legend.text = c("CR","EN","VU","LC or NT"),
ylim = c(0, 50),
args.legend=list(bty="n",horiz=F, x="topright", inset = c(-0.05, -0.1)),
)
text(x = plot, y = values$value, label = paste0(values$value, "%"), pos = 3)
Created on 2023-01-29 with reprex v2.0.2