Sunteți pe pagina 1din 1

# Function to make a polar plot with radius (r) and angle (theta) values.

# It overlays the polar plot with appropriate # constant-theta lines and constant-radius circles. # # # # Few examples of the x <- seq(0, pi * 2, x <- seq(0, pi * 2, x <- seq(0, pi * 2, polar 0.1); 0.1); 0.1); plot: polar.plot(x, sin(x) ^ 2) polar.plot(x, x) # archimedes spiral polar.plot(x, 2 * (sin(4 * x))) # polar rose

# Ref. 1: http://en.wikipedia.org/wiki/Polar_coordinates # Created on: Nov 30, 2010 # Ankit Dangi, CMS 1010, ankit@cms.unipune.ac.in # function to make a polar plot # for the given radius and angle values polar.plot <- function(theta, r) { # # x y to construct the plot, we transform from polar co-ordinates to cartesian co-ordinates <- (r * cos(theta)); <- (r * sin(theta));

# supress the characters for x-axis and y-axis, remove the margins, and # set the plotting region to square (to make a circle look like one) par(xaxt = 'n', yaxt = 'n', mar = c(0, 0, 0, 0), pty = 's'); # plot the computed cartesian co-ordinates, # remove the x-axis label and y-axis label # and set the plot type as lines plot(x, y, xlab = '', ylab = '', type = 'l'); # identify the limit to which the circle grid needs to be drawn to.lim <- max(c(x, y)); # compute a step size, so that the circles have # a visually appropriate distance between each other by.lim <- (to.lim / 20); # obtain the median of both the x and y co-ordinate vectors # and tag them as their respective (x-axis, y-axis) origins x.origin <- median(sort(x)); y.origin <- median(sort(y)); # plot the x-axis and y-axis as per the computed origins abline(h=y.origin, v=x.origin, col = gray(0.6)); # iterate up to the limit identified above for(i in seq(from = 0.0, to = to.lim, by = by.lim)) { # and construct the circles that shall appear as the polar grid symbols(x.origin, y.origin, circles = i, fg = gray(0.8), lty = 3, add = TRUE, inches = FALSE); } # iterate over 0 to 360 degrees by a step size of 30 for(degree in seq(from = 0, to = 360, by = 30)) { # and plot diagonal lines for the polar grid abline(a=0, b=tan(degree), h=y.origin, v=x.origin, lty = 3, col = gray(0.8)); } }

S-ar putea să vă placă și