1

How can I bring the barplots in the right order from very low to very high and anthropogenic? And how can I change the position of the legend?

enter image description here

This is my R-Code:

barplot(bartab, col=colors()[c(23, 89)], beside=TRUE, legend=rownames(bartab))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
Fab
  • 45
  • 3
  • With `barplot` you can change the order of you groups in your data frame and that should change the order of the bars. In future try provide [reproducible code](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – George Feb 22 '19 at 08:34

1 Answers1

2

Just order your data in the first place. For a better control of legend position make it separately.

Example

df1 <- mtcars[grep("^Merc", rownames(mtcars)), c(1, 2)]
df1 <- df1[order(df1$mpg), ]  # this orders your data by "mpg", look into `?order`

# plot
barplot(t(df1), col=c("blue", "green"), border="white", font.axis=2,
        beside=TRUE, xlab="group", font.lab=2)
legend("topleft", legend=c("mpg", "cyl"), pch=15, col=c("blue", "green"))

enter image description here

Note:

There are other possible strings to specify legend position, as the documentation states:

The location may also be specified by setting x to a single keyword from 
the list "bottomright", "bottom", "bottomleft", "left",
"topleft", "top", "topright", "right" and "center".

You can also specify exact coordinates, see ?legend.

jay.sf
  • 60,139
  • 8
  • 53
  • 110