如何创建两个镜像的堆叠面积图ggplot2

时间:2018-11-28 15:29:26

标签: r ggplot2

在ggplot2中是否可以绘制彼此相对镜像的多边形? 在基础图中,这可以使用以下参数来实现:

plot(a,b, xlim = range(0,100))
par(new=T)
plot(c,d, xlim = rev(range(0,100)))

ggplot2有类似的东西吗?

这就是我现在使用基本图形的结果。但是我但是我想通过ggplot2实现这一点。

样本数据:

df <- read.table(text="row a b 
  1    0.018212335  0.000000000    
  2    0.001418477  0.038865917    
  3    0.001418477  0.072564051    
  4    0.007521763  0.086980476    
  5    0.002845853  0.117164836    
  6    0.003793721  0.130435344    
  7    0.004267654  0.146659639    
  8    0.006639574  0.161380530    
  9    0.002850356  0.192757352    
  10    0.003325416  0.116419700    
  11    0.004275534  0.126818707    
  12    0.005225653  0.139539487    
  13    0.005700713  0.163273554    
  14    0.006650831  0.186354346    
  15    0.007125891  0.101023651", header=TRUE)

df2 <- read.table(text="row c
  0.40279511  1
  0.38332869  2
  0.37004052  3
  0.36013069  4
  0.34930768  5
  0.34690361  6
  0.33686601  7
  0.32728786  8
  0.31961345  9
  0.31051904  10
  0.31816200  11
  0.31671358  12
  0.31721331  13
  0.31818252  14
  0.30866629  15", header=TRUE)

基本绘图代码

plot(df2$c, df2$row, yaxt="n", xaxt="n",  
  ylim = rev(range(0,100)), xlim = rev(range(0,1)), 
  col='gray77', type='l', ylab=' ', xlab=' ')
polygon(c(0,df2$c,0), c(min(df2$row), df2$row,max(df2$row)),
  col='gray', border = NA, ylim = rev(range(0,100)))
par(new=T)
plot(df$a, df$row, yaxt="n", xaxt = "n",  
   ylim = rev(range(0,100)), xlim = range(0,1), 
   col='mediumblue', type='l', ylab=' ', xlab=' ')
polygon(c(0,df$a,0), c(min(df$row), df$row,max(df$row)),
  col='mediumblue', border = NA, 
  ylim = rev(range(0,100)), xlim = range(0,1))
par(new=T)
plot(df$b, df$row, yaxt="n", xaxt = "n", 
  ylim = rev(range(0,100)), xlim = range(0,1), 
  col='steelblue', type='l', ylab=' ', xlab=' ', main = "20 mm h^-1")
polygon(c(0,df$b,0), c(min(df$row), df$row, max(df$row)),
  col='steelblue', border = NA, ylim = rev(range(0,100)), xlim = range(0,1))  

example

0 个答案:

没有答案