How to add title to legend in this barplot in R
I have the following data and code to make this barcode:
tt = structure(c(21.5, 19.75, 15.05, 26.925, 19.75, NA, 28.2, 19.7,
15.4), .Dim = c(3L, 3L), .Dimnames = list(c("4", "6", "8"), c("3",
"4", "5")))
tt
3 4 5
4 21.50 26.925 28.2
6 19.75 19.750 19.7
8 15.05 NA 15.4
barplot(tt, beside=T, legend=rownames(tt))
I want to add a title (say "Test") to the legend field. I tried following, but it doesn't work:
barplot(tt, beside=T, legend=rownames(tt), legend.text="Test")
and
barplot(tt, beside=T, legend=rownames(tt))
legend("topright", legend="test")
Thank you for your help.
+3
source to share
1 answer
You can use the argument args.legend
to pass additional arguments to the function legend()
, for example:
barplot(tt, beside=TRUE, legend=rownames(tt),args.legend=list(title="aTitle"))
gives:
Note that you can also pass other arguments to the function legend()
in the same way so that you can further customize the look with the arguments you find on the help page ?legend
.
+3
source to share