是否可以更改线条的位置,使其开始和结束在堆积条形图的边缘而不是中心?

代码:

library(ggplot2)
plot11 = ggplot(CombinedThickness2[CombinedThickness2$DepSequence == "Original",], aes(x = Well, y = Thickness, fill = Sequence, alpha = Visible, width = 0.3)) +
  geom_bar(stat = "identity") +
  scale_y_reverse()
plot11 = plot11 + geom_line(aes(group = Sequence, y = Depth, color = Sequence))
plot11

当前图像:

数据:

http://pastebin.com/D7uSKBmA

最佳答案

似乎需要的是段而不是线;也就是说,使用 geom_segment() 代替 geom_line()geom_segment 需要段的起点和终点的 x 和 y 坐标。获取结束 y 值有点笨拙。但它适用于您的数据框,假设每个“井”有 30 个观测值,并且每个“井”的“序列”顺序相同。

library(ggplot2)

df = CombinedThickness2[CombinedThickness2$DepSequence == "Original",]

# Get the y end values
index = 1:dim(df)[1]
NWell = length(unique(df$Well))
df$DepthEnd[index] = df$Depth[index + dim(df)[1]/NWell]

BarWidth = 0.3

plot11 = ggplot(df,
   aes(x = Well, y = Thickness, fill = Sequence, alpha = Visible)) +
   geom_bar(stat = "identity", width = BarWidth) +
   scale_y_reverse() + scale_alpha(guide = "none")

plot11 = plot11 +
   geom_segment(aes(x = as.numeric(Well) + 0.5*BarWidth, xend = as.numeric(Well) + (1-0.5*BarWidth),
      y = Depth, yend = DepthEnd, color = Sequence))

plot11

关于r - 如何在堆积条形图的边缘定位线条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30291988/

10-12 17:05