[R] Customise a symbol in a scatterplot with for loop

Greg Snow 538280 at gmail.com
Mon Mar 24 19:29:12 CET 2014


To plot a bunch of pentagons I would suggest using the my.symbols and
ms.polygon functions in the TeachingDemos package.

If this is more to learn programming, then you can just loop over an
index of the vectors containing the x and y coordinates (are they in 2
vectors?, 2 columns of a data frame?, other?).

Do note that the pentagons will look flattened unless the aspect ratio
of the plot is 1.

With the iris data, here is a basic start:

plot(Sepal.Width~Sepal.Length, data=iris, asp=1)
for( i in 1:nrow(iris) ) {
  tmp <- mk_pent( iris$Sepal.Length[i], iris$Sepal.Width[i], 0.3 )
  polygon( tmp[,1], tmp[,2] )
}

You might want to leave additional room in the initial plot.  You
could also create one pentagon centered at 0, then add the x and y
coordinates for each point.  This is a basic start, there are multiple
ways to improve on the above code.



On Sun, Mar 23, 2014 at 2:00 PM, amcc <martinangelamccourt at eircom.net> wrote:
> Hi,
> I have the code below to generate a pentagon in R.  I have generated a
> regular scatterplot and need to draw a scatterplot with a pentagon at each
> (x,y) position, for the radius use r x 0.3.  The hint I've been given is
> that I do not need to store the polygons in a list, simply construct and
> draw the polygons within a for loop.  Four hours have gone by and I'm no
> closer, so any help would be appreciated.  My original scatterplot code is
>
> plot(Sepal.Width~Sepal.Length, data=iriss)
>
> mk_pent<-
>  function(x,y,r){
>   angle<-(0:4)*360/5+90
>   angle<-angle*pi/180
>   xs<-r*cos(angle)+x
>   ys<-r*sin(angle)+y
>  cbind(x=xs,y=ys)}
>
> Thanks
> A
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Customise-a-symbol-in-a-scatterplot-with-for-loop-tp4687386.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com




More information about the R-help mailing list