How to arrange lines along the edges of stacked histograms

Is it possible to change the position of the lines so that they begin and end at the edges of the graphs laid in the column, and not in the center?

R code:

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

Current Image:

enter link description here

Data:

http://pastebin.com/D7uSKBmA

+4
source share
1 answer

It seems that segments are required, not lines; i.e. use geom_segment()instead geom_line(). geom_segmentx and y coordinates are required for the start and end points of the segments. Getting the end y value is a little cumbersome. But it works with your data frame, assuming that for each “Well” there are 30 observations, and the order for the “Sequence” is the same for each “Well”.

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

enter image description here

+1
source

All Articles