I'd like to be able to produce a simple well log graph with ggplot2. Test data is simple:
From To Lith
0 25 A
25 50 B
50 75 C
75 100 B
100 125 C
125 150 A
150 175 B
175 200 C
The plot should be in a form of a strip, colored according to lith column with correctly displayed depths and should look something like this ("SLF" used instead of "Lith"):
As you can see, certain units repeat down the column and I need them to be in their correct place. I was hoping there is a simple way to force geom_bar or geom_col to simply draw the data as it is, without summarising it in any way, but it seems I was wrong.
How should I go about doing this? Which *geom" should I use?
TIA
CodePudding user response:
It's fairly straighforward to do this using geom_rect
instead of geom_bar
library(ggplot2)
ggplot(df, aes(x = 1, y = From, fill = Lith))
geom_rect(aes(xmin = 0.5, xmax = 1.5, ymax = From, ymin = To))
scale_y_reverse(name = 'Depth')
scale_x_continuous(limits = c(0, 2), name = NULL)
theme_minimal(base_size = 16)
theme(axis.text.x = element_blank())
scale_fill_brewer(palette = 'OrRd')
Data taken from question in reproducible format
df <- structure(list(From = c(0L, 25L, 50L, 75L, 100L, 125L, 150L,
175L), To = c(25L, 50L, 75L, 100L, 125L, 150L, 175L, 200L), Lith = c("A",
"B", "C", "B", "C", "A", "B", "C")), class = "data.frame",
row.names = c(NA, -8L))
CodePudding user response:
Use geom_rect
. You have the min and max heights so it's fairly easy.
x <- 'From To Lith
0 25 A
25 50 B
50 75 C
75 100 B
100 125 C
125 150 A
150 175 B
175 200 C'
df1 <- read.table(textConnection(x), header = TRUE)
library(ggplot2)
ggplot(df1, aes(fill = Lith))
geom_rect(aes(xmin = 0, xmax = 1, ymin = From, ymax = To))
scale_x_continuous(breaks = NULL)
theme(aspect.ratio = 8)
Created on 2022-09-27 with reprex v2.0.2