在ggplot2中,如何将图例序列调整为条形序列?(从上到下,想要从“abcd”到“dcba”的图例序列)
plot_data <- data.frame(cat=c('a','b','c','d'),value=c(1:4))
plot_data %>% ggplot(aes(x='1',y=value,fill=cat))+
geom_bar(stat='identity',position = position_stack(reverse=TRUE))+
geom_text(aes(label=value),position = position_stack(reverse=TRUE,vjust=0.5))

发布于 2022-09-29 05:29:41
类似于position_stack,您可以通过guide_legend的reverse参数,即do guides(fill = guide_legend(reverse = TRUE))来逆转图例中的顺序。
library(ggplot2)
plot_data <- data.frame(cat = c("a", "b", "c", "d"), value = c(1:4))
ggplot(plot_data, aes(x = "1", y = value, fill = cat)) +
geom_bar(stat = "identity", position = position_stack(reverse = TRUE)) +
geom_text(aes(label = value), position = position_stack(reverse = TRUE, vjust = 0.5)) +
guides(fill = guide_legend(reverse = TRUE))

https://stackoverflow.com/questions/73890122
复制相似问题