1

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?

Richard Telford
  • 9,558
  • 6
  • 38
  • 51

2 Answers2

0

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.

Niek
  • 1,594
  • 10
  • 20
0

Yep, factor is the key here. I created a bar chart with ggplot2 by using a toy dataset:

library(ggplot2)

### toy dataset
Vorschlaege <- c("V1", "V3", "V2", "V5", "V4")
Bewertung <- c(100, 20, 30, 40, 50)
df <- data.frame(Vorschlaege, Bewertung)

## define sorting order by factor as you want to have it in your bar chart:
df$Vorschlaege <- factor(df$Vorschlaege, levels = c("V5", "V4", "V3", "V2", "V1"))

ggplot(df, aes(df$Vorschlaege)) + 
  geom_bar(aes(weight = df$Bewertung)) +
  labs(title = "Meine Vorschläge",
       x = "Vorschlag", y = "Bewertung",
       subtitle = "Zeitraum: x bis y", caption = "Quelle: Meine Forschung") +
  theme_bw() ## white background theme
user1766682
  • 400
  • 3
  • 14
  • for a grouped bar plot see the answer to this one: https://stackoverflow.com/questions/18158461/grouped-bar-plot-in-ggplot – user1766682 Oct 02 '17 at 12:33