data <- data %>% arrange(mean) # 将数据按 mean 从小到大排列 p9 <- ggplot()+ geombar(data = data,…
- User
data <- data %>% arrange(mean) # 将数据按 mean 从小到大排列
p9 <-
ggplot()+ geom_bar(data = data, aes(y= reorder(aaa, mean),x=mean, fill=classify), stat = "identity")+ scale_fill_manual(values=c("#e3716e", "#eca680", "#7ac7e2", "#f7df87","#54beaa"))+ labs(y="",x="Relative variable importance", title = "") + scale_x_continuous(expand = c(0,0),limits = c(0, 0.3),breaks = c(0, 0.1, 0.2,0.3))+ theme_test()+ coord_flip()+ theme(plot.margin = unit(c(0,.5,.2,0), "cm"), legend.position = c(0.75, 0.2), text = element_text(size = 12), axis.text.y = element_text(size = 10), axis.text.x = element_text(angle = 45, hjust = 1)) + guides(fill=guide_legend(title="Predictor category"))p9 要让图内顺序才大到小
- Assistant
在你的代码中,你正试图将数据基于
mean进行排序,并且在纵轴上使用reorder函数来确保按照mean的值从小到大排列。这是正确的思路。如果你希望图中的条形图按mean从大到小排列,你可以在reorder函数中将mean的顺序反转。为了达到这个目的,你可以将mean作为负值进行重新排序。以下是修改后的代码:Rlibrary(dplyr) library(ggplot2) data <- data %>% arrange(mean) # 将数据按 mean 从小到大排列 p9 <- ggplot() + geom_bar(data = data, aes(y = reorder(aaa, -mean), x = mean, fill = classify), # 这里将 mean 取负 stat = "identity") + scale_fill_manual(values = c("#e3716e", "#eca680", "#7ac7e2", "#f7df87", "#54beaa")) + labs(y = "", x = "Relative variable importance", title = "") + scale_x_continuous(expand = c(0, 0), limits = c(0, 0.3), breaks = c(0, 0.1, 0.2, 0.3)) + theme_test() + coord_flip() + theme(plot.margin = unit(c(0, .5, .2, 0), "cm"), legend.position = c(0.75, 0.2), text = element_text(size = 12), axis.text.y = element_text(size = 10), axis.text.x = element_text(angle = 45, hjust = 1)) + guides(fill = guide_legend(title = "Predictor category")) p9