I want to change the text in the legend, I have not come around how to do that. For exampe change A and B to D and C. Any suggestions?
A <- c(10,5,5)
B <- c(30,10,10)
df <- cbind(A,B)
df <- t(df)
as.matrix(df)
barplot(df, beside=TRUE, legend=TRUE)
You simply supply a vector with the legend text (one for each color) to the argument legend.text
:
barplot(as.matrix(df), beside = TRUE, legend.text = c("C", "D", "E"))
If you want to style the legend any further you need to put arguments inside a named list and pass it to the args.legend
argument (look at ?legend
) for further arguments.
df <- data.frame(A = c(10,5,5),
B = c(30,10,10))
barplot(as.matrix(df), beside = TRUE, legend.text = c("C", "D", "E"), args.legend = list(x = "bottomright"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.