1

I have a number of values that come from my data, e.g. x1, x2, x3 etc., and I'm trying to plot shaded regions that are dependent on functionals of these values. These regions are defined by inequalities of the form:

f(x1, x2) <= A <= f(x3, x4)

f(x5, x6) <= A + B <= f(x7, x8)

and I would like to have A on the x-axis, B on the y-axis.

I've tried putting the values in a data frame and playing around with ggplot, but my r skills are lacking and beyond simple line-plots/bar-charts etc. I'm a bit lost, I especially can't figure out the syntax for plotting inequalities as regions.

Thanks

Edit for example:

        x1    x2    x3    x4    x5    x6
Plot1   0.2   0.3   0.24  0.14  0.17  0.31
Plot2   0.14  0.35  0.30  0.11  0.21  0.39

with the hope of plotting two separate graphs (one for Plot1, one for Plot2) with the regions defined by:

max(x2 + x3, x4 + x5) <= A <= min(1 - x1, 1 - x6)

max(x3, x5) <= A + B <= min(x2 + x3, x5 + x6)
Richard Border
  • 3,209
  • 16
  • 30
Milhouse
  • 177
  • 3
  • 11
  • 5
    AFAIK there are no built in geoms for plotting inequalities as regions. It would help if you actually provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data and actual values that can be used to make a plot. Are you trying to solve the equations and plot the contours? Are these at least linear inequalities? – MrFlick Aug 10 '16 at 18:09
  • sorry I should have given a clearer example! I've edited now. Basically just trying to plot the range of values of A and B that satisfy the two inequalities above. – Milhouse Aug 10 '16 at 18:47

1 Answers1

3

I'm not aware of any build-in R function to do this. You can however make a data.frame with a grid and compute for each gridpoint if the inequalities are satisfied. Then you can plot with geom_tile. A working example:

## Equation 1: f1(A,B) <= A < f2(A,B)
## Equation 2: f3(A,B) < A + B < f4(A,B)

library(ggplot2)

grid <- expand.grid( A = seq(-2,2, length.out = 100), B = seq(-1, 1, length.out = 100))

f1 <- function(A, B) B
f2 <- function(A, B) 2*(A^2 + B^2)
f3 <- function(A, B) A*B
f4 <- function(A, B) 2*A+B^3

grid$inside_eq1 <- (f1(grid$A, grid$B) <= grid$A) & (grid$A < f2(grid$A, grid$B))
grid$inside_eq2 <- (f3(grid$A, grid$B) < grid$A + grid$B) & (grid$A + grid$B < f4(grid$A, grid$B))
grid$inside <- grid$inside_eq1 & grid$inside_eq2

ggplot(grid) +
    geom_tile(aes(x = A, y = B, color = inside, fill = inside))

Edit: You asked in the comment how you could draw a border around the region. I think a general soultion is a bit complex, but in your case it's not so hard. Your functions in the inequalities are independent of A and B, they are constant and thus convex. Since the intersection of convex sets is again convex the region in which the inequalities are fulfilled is also convex. This leads to a solution which makes use of the convex hull of the region:

## if you want a border for the region (works only if all equations are convex!)

f1 <- function() 0.1
f2 <- function() 1.8
f3 <- function() -1.2
f4 <- function() 0.3

grid$inside_eq1 <- (f1() <= grid$A) & (grid$A < f2())
grid$inside_eq2 <- (f3() < grid$A + grid$B) & (grid$A + grid$B < f4())
grid$inside <- grid$inside_eq1 & grid$inside_eq2

hull <- chull(grid$A[grid$inside], grid$B[grid$inside])
ggplot(grid, aes(x = A, y = B)) +
    geom_tile(aes(color = inside, fill = inside)) +
    geom_path(data = grid[grid$inside, ][c(hull, hull[1]), ], size = 2)
AEF
  • 5,408
  • 1
  • 16
  • 30
  • thanks for replying! This works well, is there any way of plotting the region more like a graph though? i.e. I was hoping for a line for the border of the area satisfying the inequalities, with a shade in the middle. It doesn't seem that the aesthetics of geom_tile allows for this? – Milhouse Aug 10 '16 at 19:46
  • No, I don't think you can do this with geom_tile. However I edited my answer for another solution. – AEF Aug 10 '16 at 20:08