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 ?
-
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*! – Simon O'Hanlon Mar 13 '13 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. – Ashni Goyal Mar 13 '13 at 12:29
-
2This may be related: http://stackoverflow.com/q/11345838 (also in conjonction with this if you want the hatched regions: http://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 '13 at 12:37
-
1I 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 '13 at 12:48
-
@NickCox stata can do plotting. I have access to stata. So, I wanted some stata expert to comment if this can be achieved in stata or not. Thanks for your valuable response. – Ashni Goyal Mar 13 '13 at 12:54
-
2I 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 '13 at 13:24
4 Answers
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)
})
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)
})

- 18,529
- 4
- 72
- 94
-
+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 '13 at 14:08
-
-
@AshniGoyal why not to update your question to add theses equations? it is better to work on a concrete example. – agstudy Mar 13 '13 at 14:32
-
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 '13 at 14:42
-
Possibly, one could use SpatialPolygons? Not my area of expertise, but there might be some convenient methods for dealing with overlapping polygons and such. – Roland Mar 13 '13 at 14:50
-
@plannapus so what happens if two shaded regions overlap, will only topmost shading be visible or both shadings will be there indicating overlap. I have not worked with R ever, so cant answer these questions myself. I am yet to decode your reply. – Ashni Goyal Mar 13 '13 at 16:16
-
1Technically 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 '13 at 16:25
-
@agstudy although graphs look simple, the underlying 9 equations are very complex. that is why I am avoiding to write here. – Ashni Goyal Mar 14 '13 at 10:09
-
@AshniGoyal don't worry for people here:) but I think you can pick up 3 or 4 equations, maybe if we have time we can apdate our answers to your example. – agstudy Mar 14 '13 at 10:11
-
@plannapus when I run the first code, only graph is drawn, no shading is done. And this error is shown `Error in names(a) <- c("xA", "yA") : attempt to set an attribute on NULL` and `Error in names(b) <- c("xB", "yB") : attempt to set an attribute on NULL` – Ashni Goyal Mar 14 '13 at 10:29
-
-
@plannapus I am R -illiterate. I do not know what you require. I simply copy pasted your code in script file, selected the whole code, right click and run. – Ashni Goyal Mar 14 '13 at 10:43
-
@AshniGoyal the only way I could think of for getting this error implied you entered something wrong for a and b but if you say you just entered exactly what's written here i honestly can think of a reason it wouldn't work. I just tried it in osx' terminal, in a vanilla session, and it worked as well as in the GUI, so i really don't see how you got this error. What version of R do you have? – plannapus Mar 14 '13 at 11:11
-
-
also, what values should be there in `a` and `b`? if I print `a` and `b` in console, i get `NULL` . Probably that is what is indicated by the error statement. – Ashni Goyal Mar 14 '13 at 11:54
-
@plannapus it worked finally. problem was with the old version, i installed lates just now. – Ashni Goyal Mar 14 '13 at 12:21
-
@AshniGoyal glad to hear it! Yes the fact that a and b printed NULL was the issue: they should have both printed a list of two vectors (x and y). – plannapus Mar 14 '13 at 12:38
-
1Indeed `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 '13 at 12:57
-
@plannapus thanks a lot. this solves my problem and this is quiet easy to do as well. – Ashni Goyal Mar 14 '13 at 13:18
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))
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)

- 119,832
- 17
- 199
- 261
There are several submissions on the MATLAB Central File Exchange that will produce hatched plots in various ways for you.

- 23,951
- 1
- 40
- 64