[英]Reverse x-axis two time in R
我想兩次反轉x軸。 現在,我簡單地反轉了x軸的范圍,我的結果是3、2、1、0,而不是0、1、2、3。
我的代碼:
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)
現在,我要將條形反轉為3、2、1和0。“ Vorschlag1”應該放在第一位,“ Vorschlag10”應該放在最后。
我怎樣才能做到這一點?
假設mFT$Vorschlaege
是一個因子,您可以顛倒因子水平的順序。
mFT$Vorschlaege2 <- factor(mFT$Vorschlaege,levels = levels(mFT$Vorschlaege)[10:1])
這會顛倒因子水平的順序。 如果mFT$Vorschlaege
不是一個因素,則必須首先將其轉換為一個因素。 我重命名了變量mFT$Vorschlaege2
以避免覆蓋原始變量,因此您必須使用新變量重復執行table
命令。
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)
沒有可重現的示例,我無法測試此解決方案,但它可用於我自己的數據。
是的,因素是關鍵。 我通過使用玩具數據集使用ggplot2創建了條形圖:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.