ggplot垂直条形图

问题描述 投票:-2回答:1

给出的数据如

df <- data.frame(Site="A",Depth=0:-20,comp=c(rep("sable",14),rep("gres",7)))
df <- rbind(df,data.frame(Site="B",Depth=0:-15,comp=c(rep("sable",3),rep("gres",13))))

我想用comp绘制Depth vs. Site彩色图。我尝试:

ggplot(data=df) +
  geom_col(aes(Site, Depth,fill = comp)) + labs(y = "Depth (m)")

得到一个与我的数据不对应的y轴,为什么?任何修复?我也尝试过:

ggplot(data=df) +
  geom_line(aes(Site, Depth,col = comp),size=8) + labs(y = "Depth (m)")

在那里,y轴是正确的,但是段是不连续的,线条不允许我填充图案。我见过包aqp,但不使用基于ggplot的图。任何帮助赞赏。

r ggplot2
1个回答
1
投票

我很困惑你想要产生什么样的情节。

这就是你要追求的吗?

ggplot(df, aes(Site, Depth, fill = comp)) +
    geom_col(position = "dodge2") +
    labs(y = "Depth (m)")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.