16

enter image description here

Refer to the above plot. I have drawn the equations in excel and then shaded by hand. You can see it is not very neat. You can see there are six zones, each bounded by two or more equations. What is the easiest way to draw inequalities and shade the regions using hatched patterns ?

6
  • 7
    +1 for a beautiful hand-drawn image!! But, you may find yourself on the end of a downvote from someone less forgiving because you have not shown any code, sample data or what you have already tried to do! Mar 13, 2013 at 12:23
  • @SimonO101 thanks. I have tried in excel and matlab. Matlab requires mupad which I dont have. Also, I think a no. of softwares can do this, although the difficulty level will vary hugely. When somebody can confirm which software is really cool in plotting inequalities, I can get down to learning that software in detail. Mar 13, 2013 at 12:29
  • 2
    This may be related: stackoverflow.com/q/11345838 (also in conjonction with this if you want the hatched regions: blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches). But as you mentioned in your comment, Matlab may not be the best tool for this task.
    – Aabaz
    Mar 13, 2013 at 12:37
  • 1
    I don't know why Stata is a tag here. But Stata does not support hatching or stippling, pretty much as a matter of principle.
    – Nick Cox
    Mar 13, 2013 at 12:48
  • 2
    I qualify, I believe, as an experienced Stata user. What you want is not absolutely impossible in Stata, but it's best to look elsewhere.
    – Nick Cox
    Mar 13, 2013 at 13:24

4 Answers 4

18

To build up on @agstudy's answer, here's a quick-and-dirty way to represent inequalities in R:

plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
with(as.list(c(b,a)),{
    id <- yB<=yA
    # b<a area
    polygon(x = c(xB[id], rev(xA[id])),
            y = c(yB[id], rev(yA[id])), 
            density=10, angle=0, border=NULL)
    # a>b area
    polygon(x = c(xB[!id], rev(xA[!id])),
            y = c(yB[!id], rev(yA[!id])), 
            density=10, angle=90, border=NULL)
    })

enter image description here

If the area in question is surrounded by more than 2 equations, just add more conditions:

plot(NA,xlim=c(0,1),ylim=c(0,1), xaxs="i",yaxs="i") # Empty plot
a <- curve(x^2, add = TRUE) # First curve
b <- curve(2*x^2-0.2, add = TRUE) # Second curve
d <- curve(0.5*x^2+0.2, add = TRUE) # Third curve

names(a) <- c('xA','yA')
names(b) <- c('xB','yB')
names(d) <- c('xD','yD')

with(as.list(c(a,b,d)),{
    # Basically you have three conditions: 
    # curve a is below curve b, curve b is below curve d and curve d is above curve a
    # assign to each curve coordinates the two conditions that concerns it.

    idA <- yA<=yD & yA<=yB
    idB <- yB>=yA & yB<=yD
    idD <- yD<=yB & yD>=yA
    polygon(x = c(xB[idB], xD[idD], rev(xA[idA])),
            y = c(yB[idB], yD[idD], rev(yA[idA])), 
            density=10, angle=0, border=NULL)
    })

enter image description here

19
  • +1! I just use with to make your good example readable. Using polygon is not evident , so I don't find your code as dirty as you say).
    – agstudy
    Mar 13, 2013 at 14:08
  • what if the area is surreounded by 3 or 4 equations ? Mar 13, 2013 at 14:18
  • 2
    @agstudy thanks! AshniGoyal: please see the edit. Automatizing this is however relatively hard: the solution has to be custom-made to every problem.
    – plannapus
    Mar 13, 2013 at 14:42
  • 1
    Technically every polygon is blank except for the hatchings so the two shadings will overlap (if one has horizontal lines and the other vertical ones you'll have a pattern with both vertical and horizontal line).
    – plannapus
    Mar 13, 2013 at 16:25
  • 1
    Indeed curve returning the coordinates of the points that have been drawn was one of the new feature of R version 2.10.0
    – plannapus
    Mar 14, 2013 at 12:57
10

In R, there is only limited support for fill patterns and they can only be applied to rectangles and polygons.This is and only within the traditional graphics, no ggplot2 or lattice.

It is possible to fill a rectangle or polygon with a set of lines drawn at a certain angle, with a specific separation between the lines. A density argument controls the separation between the lines (in terms of lines per inch) and an angle argument controls the angle of the lines.

here an example from the help:

plot(c(1, 9), 1:2, type = "n")
polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
         density = c(10, 20), angle = c(-45, 45))

enter image description here

EDIT

Another option is to use alpha blending to differentiate between regions. Here using @plannapus example and gridBase package to superpose polygons, you can do something like this :

library(gridBase)
vps <- baseViewports()
pushViewport(vps$figure,vps$plot)
with(as.list(c(a,b,d)),{
  grid.polygon(x = xA, y = yA,gp =gpar(fill='red',lty=1,alpha=0.2))
  grid.polygon(x = xB, y = yB,gp =gpar(fill='green',lty=2,alpha=0.2))
  grid.polygon(x = xD, y = yD,gp =gpar(fill='blue',lty=3,alpha=0.2))
}
)
upViewport(2)

enter image description here

3

There are several submissions on the MATLAB Central File Exchange that will produce hatched plots in various ways for you.

2

I think a tool that will come handy for you here is gnuplot.

Take a look at the following demos:

feelbetween
statistics
some tricks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.