ggplot垂直栏配置文件

时间:2018-09-17 11:41:49

标签: r ggplot2

给出诸如

的数据
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着色的深度vs.站点。我尝试:

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的绘图。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

我很困惑要生成哪种图。

这是你的追求吗?

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

enter image description here