Sidoti, Salvatore A.
2016-May-01 14:32 UTC
[R] Spicing Up Native Circular Plot Using ggplot2
I have some angle data from an animal behavior study that I would like to plot for publication using ggplot2. What follows is my current workflow with some example data. ### BEGIN SCRIPT ### ### Create two data frames of random Cartesian coordinates ### df1 <- data.frame( x = sample(10, 11, replace = TRUE), y = sample(10, 11, replace = TRUE)) df2 <- data.frame( x = sample(10, 11, replace = TRUE), y = sample(10, 11, replace = TRUE)) ### Write a function that converts continuous Cartesian coordinates to velocities ### get.polar <- function(df) { x <- diff(df$x) y <- diff(df$y) d <- complex(real = x, imaginary = y) steps <- data.frame(speed = Mod(d), angle = Arg(d)) steps[-1,] # Deletes the first row as it does not contain an angle measurement steps$time <- (1:nrow(steps))/30 # generates a time column in seconds (1 data point = 1/30 of a second) return(steps) } df1_polar <- get.polar(df1) df2_polar <- get.polar(df2) require(circular) ### Convert angles into an object of type 'circular' ### df1_rad <- circular(df1_polar$angle, type = 'angles', units = 'radians', zero=0, rotation = "counter") df2_rad <- circular(df2_polar$angle, type = 'angles', units = 'radians', zero=0, rotation = "counter") ### Convert radians to degrees with a clockwise rotation and zero at "north" ### df1_deg <- conversion.circular(df1_rad, type = "angles", units = "degrees", zero = pi/2, rotation = "clock") df2_deg <- conversion.circular(df2_rad, type = "angles", units = "degrees", zero = pi/2, rotation = "clock") ### Convert negative rotations to positive ### df1_deg[df1_deg < 0] <- df1_deg[df1_deg < 0] + 360 df2_deg[df2_deg < 0] <- df2_deg[df2_deg < 0] + 360 par(pty = "s") plot(df1_deg, units = "degrees") ticks.circular(circular(seq(0,(11/6)*pi, pi/6)), zero = pi/2, rotation = "clock", tcl = 0.075) points(df2_deg, zero = pi/2, rotation = "clock", pch = 16, col = "darkgrey", next.points = -0.2) ### END SCRIPT ### Some suggestions for turning this rough plot into something publishable using ggplot2? Thank you! Salvatore A. Sidoti