Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot2将垂直偏移添加到堆叠柱打印_R_Ggplot2_Bar Chart_Offset_Column Chart - Fatal编程技术网

R 使用ggplot2将垂直偏移添加到堆叠柱打印

R 使用ggplot2将垂直偏移添加到堆叠柱打印,r,ggplot2,bar-chart,offset,column-chart,R,Ggplot2,Bar Chart,Offset,Column Chart,我有几个表示钻井剖面的堆叠柱形图。我想偏移每个钻孔的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

我有几个表示钻井剖面的堆叠柱形图。我想偏移每个钻孔的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,"\n",df$x,"m"), name="") +
  scale_fill_manual(values = c("gravel"='#f3e03a', "sand"= '#e09637', "silt"='#aba77d'))

print(drilling)
到目前为止,我的最低工作代码是:

 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,"\n",df$x,"m"), name="") +
  scale_fill_manual(values = c("gravel"='#f3e03a', "sand"= '#e09637', "silt"='#aba77d'))

print(drilling)

df在这里使用
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_段
geom_段
没有
填充
aes
,因此我们需要更改为
颜色

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,"\n", 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())


要添加边框,请参阅。

非常好,再次感谢!仅针对其他人:我添加了两个额外的
geom_段
来创建黑色边框,如我问题中的图像所示:
geom_段(aes(x=(x),xend=(x),y=start-0.014,yend=end+0.014),size=drill_width+1,color=“black”)+geom_段(size=drill_width)+geom_段(size=drill_width,color=“black”,aes(y=start-0.014,yend=start,x=x,xend=x))+…