gpt4 book ai didi

r - 如何在ggplot的aes_string中调用重新排序

转载 作者:行者123 更新时间:2023-12-03 15:26:42 33 4
gpt4 key购买 nike

我需要使用ggplot和aes_string()从高到低(从左到右)重新排列小节的顺序。例如对于数据帧df <-f(X,Y,Z)可以用

 ggplot(top10,aes(x=reorder(X,-Y),y=Y,fill=X) + geom_bar(stat="identity")

但是我需要通过引用数据框的列号而不是列名来实现此目标,如下所示
 ggplot(top10, aes_string(x=colnames(top10)[num1],y=meanFeat, 
fill=colnames(top10)[num1])) + geom_bar(stat="identity")

上面的语句使用列号绘制输出。但是,它不会从高到低(从左到右)重新排序

如何使用aes_string中的re-order函数来实现此目的?

最佳答案

由于aes_string适用于字符串,因此请使用paste:

ggplot(top10, aes_string(x=paste0("reorder(",colnames(top10)[num1],", -Y)"),y=meanFeat,
fill=colnames(top10)[num1])) + geom_bar(stat="identity")

关于r - 如何在ggplot的aes_string中调用重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999317/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com