0

Is it possible in ggplot2 to draw polygons that are mirrored opposite each other? In the base graph, this can be achieved using parameters such as:

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

is there something similar for the ggplot2?

That's what I have now with basic graph. But I but I would like to achieve this with the ggplot2.

Sample data:

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)

Base plotting code

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Kate
  • 21
  • 3
  • 1
    I don't think there's a built in geom specifically for that. You can probably just use two geom layers to achieve the same thing. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data that can be used to test and verify possible solutions. – MrFlick Nov 28 '18 at 15:35
  • @MrFlick I edit my question with an example – Kate Nov 28 '18 at 16:07
  • 2
    Did I format the code correctly? The output I get run I run the sample looks very different than your plot. – MrFlick Nov 28 '18 at 16:36
  • 1
    I'm not sure what you're trying to get, since the plot I get is different from the one you show, and the plot you show doesn't seem to be mirrored. Maybe I'm not understanding what you mean by mirroring? – camille Nov 28 '18 at 16:38
  • @MrFlick I posted the final graph which I made using the base graphics, just to make it clear what I mean. In this code example only the first lines of my data frame... – Kate Nov 29 '18 at 17:19
  • @camille I need two different polygons drawn on one chart, opposite each other. Probably I didn't quite clearly express what I need, sorry. – Kate Nov 29 '18 at 17:21

0 Answers0