8

Using R , I have drawn a hatched plot similar to this. I want to do following 4 things in R

  1. Add legend as shown in the link.
  2. Replace x-axis name by the greek symbol of delta
  3. To mention various points of intersection on the plot. for eg, at x=0.75 a few curves meet x-axis, I want to put the value 0.75 near that point.
  4. If you see the curves, they are not smooth. How to make them smooth ? Even excel plots far more smoother curves.

How to achieve this ?

Here is the plot.

plot

Following code is used to draw the plot.

plot(NA,xlim=c(0,1),ylim=c(0,1),xlab="delta",ylab="K", xaxs="i",yaxs="i") # Empty plot
a1 <- curve((x+x^7-x^2-x^4)/(1+x-x^3-x^4), from=0, n=450000, add = TRUE) # First curve
a2 <- curve((x^2+x^3-x-x^5)/(x+x^2), to=0.9, n=45000, add = TRUE)
a3 <- curve((x+x^7-x^2-x^4)/(1+x-x^2-x^3-x^4+x^7),from=0, n=45000, add = TRUE)
a4 <- curve((x+x^8-x^3-x^5)/(x+x^8-x^3-x^5+1-x^4),from=0, to=0.9, n=45000, add = TRUE)
a5 <- curve((x+x^8-x^3-x^5)/(1+x-x^5-x^4),from=0, n=45000, add = TRUE)
a6 <- curve((x+x^2-x^4-1)/(x-x^4), to=0.84, n=45000, add = TRUE)
a7 <- curve((x+x^6-x^3-x^4)/(1+x-x^3-x^4), from=0.83 ,to=1,  n=45000, add = TRUE)
a8 <- curve((1+x^7-x^2-x^4)/(1+x^3-x-x^4), from=0.819, n=45000, add = TRUE)
a9 <- curve((x)/(1+x), n=45000,from=0.819, to =1, add = TRUE)


names(a1) <- c('xA1','yA1')
names(a2) <- c('xA2','yA2')
names(a3) <- c('xA3','yA3')
names(a4) <- c('xA4','yA4')
names(a5) <- c('xA5','yA5')
names(a6) <- c('xA6','yA6')
names(a7) <- c('xA7','yA7')
names(a8) <- c('xA8','yA8')
names(a9) <- c('xA9','yA9')


with(as.list(c(a1,a2,a3,a4,a5,a6,a7,a8,a9)),{

idA <- yA3 >=0
idB <- yA2 >=0 & yA2 <= yA4
idC <- yA4 >= yA2

idD <- yA5 >=0

idE <- yA6 >=0 & yA6 <= yA7
idF <- yA7 <= yA6

idG <- yA8 >=0 & yA8 <= yA9 
idH <- xA9 >= xA8 &  xA9 >0.8

idI <- xA1 >=0 & xA1 <= 0.755
idJ <- xA3 >=0 & xA3 <= 0.755



 polygon(x = c(xA3[idA],xA2[idB],rev(xA4[idC])),
        y = c(yA3[idA],yA2[idB],rev(yA4[idC])), 
        density=20, angle=90, border=NULL)

 polygon(x = c(xA5[idD],1,1,0),
        y = c(yA5[idD],0,1,1), 
        density=20, angle=0, border=NULL)

 polygon(x = c(xA6,xA7),
        y = c(yA6,yA7), 
        density=20, angle=45, border=NULL)

 polygon(x = c(rev(xA8[idG]),xA9[idH],1),
        y = c(rev(yA8[idG]),yA9[idH],0), 
        density=20, angle=135, border=NULL)

 polygon(x = c(xA1[idI],rev(xA3[idJ])),
        y = c(yA1[idI],rev(yA3[idJ])), 
        col="black", border=NULL)


 })
Community
  • 1
  • 1
Ashni Goyal
  • 819
  • 3
  • 10
  • 20
  • 3
    You can't seriously expect an answer from posting this? Add some details of how you have plotted your graphic. How to add the legend will differ depending on what you use to plot your graph (lattice , grid , base etc) – Simon O'Hanlon Mar 15 '13 at 12:03
  • Why didn't you add it to this question? http://stackoverflow.com/questions/15385063/easiest-way-to-plot-inequalities-with-hatched-fill – Jonas Tundo Mar 15 '13 at 12:19
  • 3
    Great edit and nice graph. +1 for a clear, reproducible question. In future, consider framing all your questions in this manner. – Simon O'Hanlon Mar 15 '13 at 12:37

2 Answers2

11
layout(matrix(c(1,2),nrow=1),
       width=c(4,1)) #Divide your plotting region in two inequal part
par(mar=c(5,4,4,0)) #Get rid of the margin on the right side
plot(NA,xlim=c(0,1),ylim=c(0,1),
     xlab=expression(delta),ylab="K", xaxs="i",yaxs="i") # Here's your delta
a1 <- curve((x+x^7-x^2-x^4)/(1+x-x^3-x^4), from=0, n=450000, add = TRUE)

...

par(mar=c(5,0,4,2)) #No margin on the left side
plot(c(0,1),type="n", axes=F, xlab="", ylab="") #Empty plot
legend("top",legend=c("1","2","3","4","5"), 
       density=c(20,20,20,20,NA), angle=c(90,0,45,135,NA), 
       col=c(NA,NA,NA,NA,"black"), bty="n", cex=1.5)

enter image description here

As for the point you want to label, either use function text (or mtext) to do it "programmaticaly" or locator to do it interactively.

Edit: Alternatively (as I said in the comments), this would work as well to put your legend outside the plot area and is probably simpler:

par(mar=c(5,4,4,8))
plot(NA,xlim=c(0,1),ylim=c(0,1),
     xlab=expression(delta),ylab="K", xaxs="i",yaxs="i") # Here's your delta
     a1 <- curve((x+x^7-x^2-x^4)/(1+x-x^3-x^4), from=0, n=450000, add = TRUE)

...

legend(1,1,legend=c("1","2","3","4","5"), 
   density=c(20,20,20,20,NA), angle=c(90,0,45,135,NA), 
   col=c(NA,NA,NA,NA,"black"), bty="n", cex=1.5, xpd=TRUE)
plannapus
  • 18,529
  • 4
  • 72
  • 94
  • Thanks! An alternative to using `layout` here might have been to increase the right margin (say `par(mar=c(5,4,4,8))`) and plot the legend outside using argument `xpd=TRUE`: `legend(1,1,legend=..., xpd=TRUE)` – plannapus Mar 15 '13 at 12:50
  • Actually I think I'm gonna change my answer to that, because it s a way simpler solution. – plannapus Mar 15 '13 at 13:02
  • @plannapus is it possible to write the legend `left to right` , because i want to put it below the plot – Ashni Goyal Mar 15 '13 at 13:17
  • 1
    @AshniGoyal yes with parameter `horiz=TRUE` (see `?legend`) – plannapus Mar 15 '13 at 13:18
  • @plannapus why are graphs not smooth even after using very high `n` in the `curve` ? – Ashni Goyal Mar 15 '13 at 13:58
  • 1
    @AshniGoyal I think you'll have to ask a new separate question for that precise point: I might be wrong but i don't think it has to do with the actual "plot-making" process but with the device itself. – plannapus Mar 15 '13 at 14:51
  • @plannapus I used `mtext("B",4)` , but it places `B` in horizontal orientation. How to control the orientation and change it to vertical one. – Ashni Goyal Mar 16 '13 at 09:24
  • 1
    @AshniGoyal `mtext("B", 4, las=2)` – plannapus Mar 16 '13 at 09:48
  • For question 4 , follow here http://stackoverflow.com/questions/15447622/how-to-plot-smoother-curves-in-r – Ashni Goyal Mar 16 '13 at 10:11
8

The traditional graphics system provides the legend() function for adding a legend or key to a plot. The legend is usually drawn within the plot region, and is located relative to user coordinates.

The function has many arguments, here we need to use angle and density arguments to differentiate hashed regions.

legend(0.5, 0.8, paste("region", 1:5),
       density=c(20,20,20,20,0),
       angle=c(90,0,45,135,0))

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261