[英]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.