-2

Given data such as

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))))

I want to plot Depth vs. Site colored by comp. I try:

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

but get a y axis that does not correspond to my data, why? any fix? I have also tried:

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

There the y axis is correct, but segments are discontinuous and lines do not let me fill with patterns. I've seen package aqp, but does not use ggplot-based plots. Any help appreciated.

user2955884
  • 405
  • 2
  • 11
  • 1
    Given your data, `geom_col` behaves as expected. I'm confused what you expected. Can you provide a mock-up of the plot you'd like to generate? – Maurits Evers Sep 17 '18 at 11:57
  • `geom_col` is plotting this `library(dplyr); df %>% group_by(Site, comp) %>% summarise(SUM = sum(Depth))` and that's the expect behaviour as @MauritsEvers said. – AntoniosK Sep 17 '18 at 12:02
  • @Maurits Evers: geom_line is providing a graphic that is close to what I need, except that the segments are discontinuous and will not be able to use a fill variable. I can create the graphic I need with geom_col if the data are presented by intervals of depth, as in https://stackoverflow.com/questions/36431036/how-to-make-a-stacked-bar-plot-using-ggplot-to-represent-soil-column-types, but I would like to do get the same kind of graphic with data such as the df in my original question. – user2955884 Sep 17 '18 at 13:20
  • 1
    @AntoniosK: thanks for the clarification on what is geom_col actually doing. Clearly, I cannot get what I want with geom_col and data such as df – user2955884 Sep 17 '18 at 13:25

1 Answers1

1

I'm confused what kind of plot you want to generate.

Is this what you're after?

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

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • No this not at all what I need. I need 1 single bar per site, colors according to variable comp at a given depth. – user2955884 Sep 17 '18 at 13:23
  • @user2955884 But your data have multiple measurements per site per comp. So how do you want to deal with them. Summarise them perhaps? By summing them? Averaging them? This is not clear at all. It would help if you were to provide a mock-up of how you'd like to visualise the data. – Maurits Evers Sep 17 '18 at 13:29
  • I have one observation at each depth at each site. The plot with geom_line in my question is enough as a mock-up. One single bar per site, describing comp at each depth. – user2955884 Sep 17 '18 at 13:54
  • @user2955884 Nope, not getting it. Sadly this is not going anywhere. Perhaps somebody else will be able to help. Good luck. – Maurits Evers Sep 17 '18 at 13:57