Ggplot和reorder不工作,即使使用统计信息:
我一定是做了些傻事,但是在ggplot的Ggplot和reorder不工作,即使使用统计信息:,r,ggplot2,R,Ggplot2,我一定是做了些傻事,但是在ggplot的aes()部分定期地对reorder进行重新排序,但莫名其妙地对我不起作用。这里有两个版本的MWE在我的系统上重现了这个问题。两人都没有按总数对字母重新排序。绘图应在X轴上显示b、c、a (df <- data.frame(letters = c("a", "b", "c"), nums = 1:3, total = c("150", "50", "100"))) reorder.plot <- ggplot(df, aes(reorder(l
aes()
部分定期地对reorder
进行重新排序,但莫名其妙地对我不起作用。这里有两个版本的MWE在我的系统上重现了这个问题。两人都没有按总数对字母重新排序。绘图应在X轴上显示b、c、a
(df <- data.frame(letters = c("a", "b", "c"), nums = 1:3, total = c("150", "50", "100")))
reorder.plot <- ggplot(df, aes(reorder(letters, total), y = nums)) +
geom_point()
reorder.plot
reorder.plot.stats <- ggplot(df, aes(stats::reorder(letters, total), y = nums)) +
geom_point()
reorder.plot.stats
(dftotal
列是一个因子,但是reorder()
采用数字或逻辑。如果在指定总计
值时删除150、50、100
周围的引号,或者首先使用as.numeric()将总计
转换为数字
,它会起作用。你应该总是喜欢重新排序你的数据,而不是你的审美观。重新排序aes
只会产生意外和不想要的结果。虽然它通常会在你玩过它之后起作用,但重新排序数据本身应该马上就能起作用:
df$letters <- reorder(df$letters, total)
df$字母