使用ggplot2向叠置柱状图添加垂直偏移量 [英] Add vertical offset to stacked column plot with ggplot2
本文介绍了使用ggplot2向叠置柱状图添加垂直偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个代表钻井剖面的堆叠柱状图。我要偏移每个钻孔的y位置以表示地面的实际高度。
我的数据如下:
x layer.thickness layer.depth Petrography BSCategory Offset
0 0.2 0.2 silt Drilling1 0
0 1.0 1.2 gravel Drilling1 0
0 3.0 4.2 silt Drilling1 0
4 0.4 0.4 silt Drilling2 -1
4 0.8 1.2 gravel Drilling2 -1
4 2.0 3.2 sand Drilling2 -1
到目前为止,我的最低工作代码是:
df <- data.frame(x=c(0,0,0,4,4,4), layer.thickness = c(0.2,1.0,3.0,0.4,0.8,2.0),
layer.depth = c(0.2,1.2,4.2,0.4,1.2,3.2),
Petrography = c("silt", "gravel", "silt", "silt", "gravel", "sand"),
BSCategory = c("Drilling1","Drilling1","Drilling1","Drilling2","Drilling2","Drilling2"),
Offset = c(0,0,0,-1,-1,-1))
# provide a numeric ID that stops grouping individual petrography items
df <- transform(df,ix=as.numeric(factor(df$BSCategory)));
drilling <- ggplot(data = df, aes(x = x, y = layer.thickness, group = ix, fill = Petrography)) +
theme_minimal() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line.x = element_blank(),
axis.ticks.y = element_line(),
aspect.ratio=1) +
geom_col(position = position_stack(reverse = TRUE), width= .15,color="black") +
scale_y_reverse(expand = c(0, 0), name ="Depth [m]") +
scale_x_continuous(position = "top", breaks = df$x, labels=paste(df$BSCategory,"
",df$x,"m"), name="") +
scale_fill_manual(values = c("gravel"='#f3e03a', "sand"= '#e09637', "silt"='#aba77d'))
print(drilling)
这是我到目前为止的输出(红色表示应该是什么样子):
推荐答案
在这里使用geom_rect
可能更容易(条形图是/应该锚定在零)。首先,我们需要计算每个样本的y
开始和结束位置:
library(data.table)
setDT(df)[ , `:=`(start = start <- c(Offset[1] + c(0, cumsum(head(layer.thickness, -1)))),
end = start + layer.thickness), by = BSCategory]
geom_rect
同时具有fill
和color
美感,因此可以轻松地为每个单独的样本添加边框。
w <- 0.5 # adjust to desired width
ggplot(data = df, aes(xmin = x - w / 2, xmax = x + w / 2, ymin = start, ymax = end,
fill = Petrography, group = Petrography)) +
geom_rect(color = "black") +
scale_y_reverse(expand = c(0, 0), name ="Depth [m]") +
scale_x_continuous(position = "top", breaks = df$x, labels = paste(df$BSCategory,"
", df$x, "m"), name = "") +
scale_fill_manual(values = c("gravel" = '#f3e03a', "sand" = '#e09637', "silt" = '#aba77d')) +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.ticks.x = element_blank())
或者,可以使用geom_segment
。geom_segment
没有fill
aes
,因此需要更改为color
:
ggplot(data = df, aes(x = x, xend = x, y = start, yend = end, color = Petrography, group = ix)) +
geom_segment(size = 5) +
scale_y_reverse(expand = c(0, 0), name ="Depth [m]") +
scale_x_continuous(position = "top", breaks = df$x, labels = paste(df$BSCategory,"
", df$x, "m"), name = "") +
scale_color_manual(values = c("gravel" = '#f3e03a', "sand" = '#e09637', "silt" = '#aba77d')) +
theme_classic() +
theme(axis.line.x = element_blank(),
axis.ticks.x = element_blank())
若要添加边框,请参阅Add border to segments in geom_segment。
这篇关于使用ggplot2向叠置柱状图添加垂直偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文