1

Working with borehole data, attempting to plot the cross section with R. I'm rusty and am having trouble organizing the plot the way I want. From the image, my bar plot is not tracking with y axis values displaying the depth of the borehole, instead it tracks with the Layers (categorical data).

Very similar question was asked here but I could not get the code to work for my situation because my data is formatted differently.

Just to clarify, I want to put the y axis in increasing numerical order, starting at 0, with the categorical layer data mapped to the correct part of that depth.

my code:

g2 <- ggplot(data=df3,
        mapping = aes(x=PointID,y=End_Depth,
                      fill=`Layer`)) +
  geom_col(colour="black") +
  labs(y="Depth")

How the Chart Turns Out Now

The Data

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 2
    Please do not post code or data as images. It's easier to help you if you include a simple reproducible example with sample input that can be used to test and verify possible solutions (for example, with dput()). See the link for ways to improve your question: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Skaqqs Oct 19 '21 at 19:30
  • (There are plenty of reasons to not provide an image of data, see https://meta.stackoverflow.com/a/285557 for several of them.) – r2evans Oct 19 '21 at 19:32
  • FYI, for formatting: the "fence" for demarcating a code block is three **backticks**, `\`\`\``, not three single-quotes `'''`; *and* they must be on a line of their own, shared with no code. This means something like `\`\`\`\ng2 <- ggplot(...) +\n geom_col(...) +\n labs(...)\n\`\`\``. The one exception is that with the Stack interface, one can provide a syntax "hint" for aesthetics, such as `\`\`\`r` ... but that must still be on a line of its own. Please see https://stackoverflow.com/editing-help and https://meta.stackexchange.com/a/22189. Thanks! – r2evans Oct 19 '21 at 19:35
  • @r2evans Amazing! Thanks for the help. My first time in stackoverflow – allencadbim1 Oct 19 '21 at 19:36

1 Answers1

1

The question you were pointing to contains a very good idea, to use geom_rect instead. You could do something like the following (comments in code)

library(tidyverse)

# just some fake data, similar to yours
foo <- data.frame(id = "id", layer = letters[1:6], depth = c(5,10,12,15,20,25))

foo2 <- 
  foo %>%
  # using lag to create ymin, which is needed for geom_rect
  # transforming id into integers so i can add / subtract some x for xmin/xmax
  mutate( ymin = lag(depth, default = 0),
         id_int = as.integer(factor(id))) 
  
# I am turning off the legend and labelling the layers directly instead 
# using geom_text
# this creates a "wrong" y axis title which I'm changing with labs(y = ... )
# the continuous x axis needs to be turned into a fake discrete axis by
# semi-manually setting the breaks and labels
ggplot(foo2) +
  geom_rect(aes(xmin = id_int - .5, xmax = id_int +.5, 
                ymin = ymin, ymax = depth,
                fill = layer), show.legend = FALSE) +
  geom_text(aes(x = id_int, y = (depth + ymin)/2, label = layer)) +
  scale_x_continuous(breaks = foo2$id_int, labels = foo2$id) +
  labs(y = "depth") 

Created on 2021-10-19 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Amazing! How would I reverse the y-axis (making 0 the top and showing blank space past 25 to represent that the hypothetical ground continues from that point)? Also how would I make the bar itself thinner (making the plot look more cylindrical like a vertical pipe)? How should I go about editing/customizing the colors myself? Finally, how do I add the label information back? – allencadbim1 Oct 19 '21 at 22:10
  • @allencadbim1 these are many follow up questions and I guess you can find answers to them elsewhere on SO. If you don't succeed, please ask new questions, and don't forget to link to your previous questions, so people know what work has been done on that before. – tjebo Oct 21 '21 at 09:58