如何在ggplot中制作此图

时间:2019-10-25 01:02:45

标签: r ggplot2 figure step

我想创建一个这样的人物。我不知道它是什么类型的图,也无法在ggplot中找到参考。我应该在搜索互联网上使用的“关键字”上是否有任何输入?

core profile

1 个答案:

答案 0 :(得分:1)

可以使用单个geom_segment()完成此操作。设置一列以定义线段是垂直还是对角线。使用比例将该列分别映射到(实线,粗体,紫色)和(虚线,细线,黑色)。

library( tidyverse )

# Data
X <- tibble(
  x = rep(c(100, 150, 800, 750, 850, 1250, 4300, 2300, 3300, 6100), each=2),
  y = c(0,2, 4,8, 10,15, 20,45, 50,70, 80,100, 110,130, 140,160, 230,245, 280,310),
  x1 = c(x[-1],0), y1 = c(y[-1],310), type=ifelse(x==x1,"vert","diag") )
# # A tibble: 20 x 5
#        x     y    x1    y1 type 
#    <dbl> <dbl> <dbl> <dbl> <chr>
#  1   100     0   100     2 vert 
#  2   100     2   150     4 diag 
#  3   150     4   150     8 vert 
#  4   150     8   800    10 diag 
# ...

ggplot(X) + theme_bw() + scale_y_reverse() +
    geom_segment( aes(x=x, y=y, xend=x1, yend=y1,
                      linetype=type, size=type, color=type) ) +
    scale_linetype_manual( values=c(diag="dashed", vert="solid") ) +
    scale_size_manual( values=c(diag=0.5, vert=2) ) +
    scale_color_manual( values=c(diag="black", vert="darkorchid4") ) +
    guides( linetype=FALSE, size=FALSE, color=FALSE ) +
    xlab( "Concentration" ) + ylab( "Depth (cm)" )

enter image description here