Yunus Simsek Yunus Simsek - 1 year ago 152
R Question

Reverse x-axis two time in R

I want reverse the x axis two times. Now I have simply reverse the range of x axis and my result is 3, 2, 1, 0 instead of 0, 1, 2, 3.

My code:

temp2 = table(mFT$Vorschlaege, mFT$Bewertung)

barplot(temp2 , main="10 Vorschl├Ąge pro Methode", xlab="Bewertung", beside=TRUE, ylim = c(0,40), xlim= c(43,3), col = colours10)
legend("top", legend = rownames(temp2), fill = colours10)


see my barplot

Now I want reverse the bars into 3, 2, 1 and 0.
The "Vorschlag 1" should be in the first place and "Vorschlag10" in last.

How can I do this?

Answer Source

Assuming mFT$Vorschlaege is a factor you can reverse the order of the factor levels.Try:

mFT$Vorschlaege2 <- factor(mFT$Vorschlaege,levels = levels(mFT$Vorschlaege)[10:1])

This reverses the order of your factor levels. If mFT$Vorschlaege is not a factor you'll have to turn it into a factor first. I renamed the variable mFT$Vorschlaege2 to avoid overwriting your original variable so you'll have to repeat the table command with the new variable.

temp2 = table(mFT$Vorschlaege2, mFT$Bewertung)
    barplot(temp2 , main="10 Vorschl├Ąge pro Methode", xlab="Bewertung", beside=TRUE, ylim = c(0,40), xlim= c(43,3), col = colours10) 
    legend("top", legend = rownames(temp2), fill = colours10)

Without a reproducible example I cannot test this solution but it works with my own data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download