2017-11-05 24 views
1

드릴링 프로파일을 나타내는 여러 개의 누적 기둥 형 차트가 있습니다. 은지면의 실제 높이를 나타 내기 위해 각 시추공의 y 위치를 오프셋 ()하고 싶습니다.ggplot2를 사용하여 스택 된 열 플롯에 수직 옵셋 추가

내 데이터는 다음과 같습니다

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) 

(그것이 무엇을 나타내는 다음과 같이 표시되어야 함) :

Example of drilling profiles

답변

2

여기서 geom_rect으로 작업하는 것이 더 쉬울 수도 있습니다 (막 대형 차트는 0에 고정되어야 함). 처음에 우리는 각 샘플에 대해 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는 쉽게 각 샘플에 테두리를 추가 할 수 있습니다 모두 fillcolor 미학을 가지고 있습니다. 또한

enter image description here


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

enter image description here

Add border to segments in geom_segment를 참조 테두리를 추가하려면.

+1

대단히 감사합니다! 다른 모든 사람들을 위해서 : 나는 두 개의 추가 된'geom_segments'를 더 추가하여 검은 색 테두리 모양을 만들었습니다. 질문에있는 이미지는 다음과 같습니다 : 'geom_segment (x =, xend, (x), xend = (x), y = start-0.014) , yend = end + 0.014), size = drill_width + 1, color = "black") + geom_segment (size = drill_width) + geom_segment (크기 = drill_width, color = "black", aes (y = 시작 -0.114, yend = start, x = x, xend = x)) + ...' – Valentin