【发布时间】:2021-09-17 16:47:00
【问题描述】:
我正在尝试用 ggplot 绘制土壤剖面。但是,geom_col 首先将所有沙层组合在一起,然后是所有泥炭层,最后是所有粘土层。我希望订单取决于我的数据框中的订单,或者取决于 depth_min 的订单。所以第一个剖面是沙-粘土-泥炭-沙子,最后一个是泥炭-花萼-泥炭。 我尝试过使用顺序作为一种美学,但这似乎已被弃用并在网上广泛搜索,但只提出了许多关于颠倒堆栈顺序或更改 de legend 顺序的帖子。有什么解决办法吗?或者也许我不应该(ab)为此使用geom_col,而是使用其他一些功能(最好是ggplot)?
可重现的例子:
d <- read.csv(text='Location,depth_min,depth_max, depth,soil
1,0,20,20,sand
1,20,30,10,clay
1,30,60,30,peat
1,60,100,40,sand
2,0,30,30,clay
2,30,90,60,peat
3,0,40,40,peat
3,40,70,30,clay
3,70,120,50,peat',header=T)
d %>%
ggplot(aes(x=Location,y=depth, fill = soil)) +
geom_col(position="stack") +
scale_y_reverse() +
theme_bw()
【问题讨论】:
标签: r ggplot2 stacked-chart