[R] polygon graph

Peter Ehlers ehlers at ucalgary.ca
Wed Dec 2 18:06:10 CET 2009


Jose Narillos de Santos wrote:
> Hi all,
> 
> I have a matrix dd with dates and data. i WANT to create a grpah with shade
> between the line and the zero axis. I´m trying to use polygon but something
> doesn´ t work
> 
> Imagine.
> 
> I want to plot
> 
> g<-read.table("dd.txt", col.names=c("fecha","DP"))
> 
> g$fecha <- as.Date(g$fecha, format="%d/%m/%Y")
> 
> t<-g$fecha
> st<-length(g$DP)
> 
> ft<-plot(t,g$DP,type="l",ylab="DP")
> 
> polygon(c(1, 1:st, st), c(0, g$USGDP, 0), col = "blue")
> 
> abline(h = 0, lwd = 1, col = "black")
> 
> If I ommit ploygon line the doc works properly (it plots a graph line).
> 
> But I want to colur the area below or over the line and the zero axis.
> 
> Can you guide or indicate because in this case polygon doesn´t work.
> 
> I attach the two files.
> 
(You need to get your variable names straight: DP, not USGDP)
As you have it, polygon is looking for values 1:28 on the x-axis.
It won't find them:

  par('usr')

will tell you what the range of x-values is on the plot.

Change your line to:

  polygon(c(t[1],t,t[st]), c(0, g$DP, 0), col = "blue")

This should work (untested).

  -Peter Ehlers

> 
> 
> Thanks in advance for all.
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list