简体   繁体   中英

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)

看到我的情节

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?

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.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM