我正在尝试绘制一个从高值到低值排序的条形图,高值是彩虹渐变的开始,低值是彩虹的结束。我可以让它们从高到低排列,或者以适当的彩虹渐变排列,但不能同时排列,颜色不断变得混乱。我还没有切换第一个图的轴,但希望最终的图与第二个图的方向相同。
p <-ggplot(Herb_Nums, aes(x = reorder(name, -amount), y = amount, fill = Herb_Nums$name), show.legend = FALSE) + geom_bar(stat = "identity")
p + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + theme(legend.position = "none")
p + theme(legend.position = "none")
df <- Herb_Nums %>% arrange(desc(amount))
ggplot(df, aes(x= name, y= amount, fill = Herb_Nums$name)) + geom_col(show.legend = FALSE) + coord_flip() + scale_color_continuous()
提前谢谢您!
为了达到您想要的结果,请将重新排序的
name
列映射到 fill
aes 上。
使用一些虚假的随机示例数据:
set.seed(123)
df <- data.frame(
name = LETTERS[1:20],
amount = sample(20)
)
library(ggplot2)
ggplot(df, aes(
x = reorder(name, -amount), y = amount,
fill = reorder(name, -amount)
)) +
geom_col(show.legend = FALSE) +
coord_flip() +
scale_color_continuous()