此问题已在此处有答案:
Order Bars in ggplot2 bar graph(16个答案)
5天前关闭。
我花了几个小时在www.example.com上搜索重新排序一个分组条形图stackoverflow.com,但我没有找到我要找的答案。我想改变条形图和图例的顺序,按顺序打印“before”、“during”和“after”,但R一直按字母顺序打印因子,就像“after”在“during”之前一样,不知道如何修复它。
如果有人能帮助我,我会很感激!
这是我想要改变的R显示顺序。
下面是我的代码:
df <- data.frame(time = c("after","before","during","before","after","before","during","after","after","during", "during","after","before","during","before"),
variable = c("granny", "red del", "other", "golden", "star", "granny", "red del", "other", "golden", "star", "granny", "red del", "other", "golden", "star"),
value = c(34,66,7,12,99,35,66,82,55,48, 27,55,82,42,26))
df$time = as.factor(df$time)
df$variable = as.factor(df$variable)
ggplot(df, aes(x = variable, y = value, fill = time)) +
geom_bar(position='dodge', stat='identity') +
theme_minimal() +
labs(x=' ', y=' ', title=' ') +
theme(plot.title = element_text(hjust=0.5, size=20, face='bold')) +
theme_bw() +
theme(axis.line = element_line(color='black'),
plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank())
1条答案
按热度按时间4uqofj5v1#
ggplot将只按字母顺序排列任何字符变量,如果没有指定的顺序。要指定因子水平的顺序,最简单的方法就是在
factor
函数中说明水平的顺序。而不是