2016-05-01 5 views
3

CODEAufpeppen indiaRund Plot Mit ggplot2

Ich habe einige Winkeldaten von einem Tier Verhaltensstudie AKTUALISIERT, die ich für die Veröffentlichung mit ggplot2 plotten möchten. Was folgt, ist mein aktueller Workflow mit einigen Beispieldaten und wie es mit der generischen Plot-Funktion aussehen würde.

### 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) 

# Suggested solution by MLavoie with modifications 
temp1 <- data.frame(Exercise = c(1, 1, 1, 1), Name = c(1, 2, 3, 4), 
    Score = c(90, 180, 270, 360)) 
temp2 <- data.frame(Name=c(replicate(length(df1_deg), 3)), 
    Score = c(df1_deg)) 
temp3 <- data.frame(Name=c(replicate(length(df2_deg), 4)), 
    Score = c(df2_deg)) 
temp4 <- data.frame(Name=c(4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8), 
    Score = c(0, 45, 90, 135, 180, 225, 270, 315)) 

ggplot() + 
geom_bar(data = temp1, aes(x = factor(Name), y = Score, fill = factor(Exercise)), 
    width = 1, stat = 'identity') + 
geom_point(data = temp2, aes(x = Name, y = Score), 
    color = "green", size = 2) + 
geom_point(data = temp3, aes(x = Name, y = Score), 
    color = "red", size = 2) + 
geom_point(data = temp4, aes(x = Name, y = Score), 
    color = "black", shape = 8, size = 2) + 
geom_vline(xintercept = 4.8) + 
annotate("text", x = 0, y = 0, label = "+", size = 6) + 
scale_y_continuous(breaks = c(0, 45, 90, 135, 180, 225, 270, 315)) + 
coord_polar(theta = "y", start = 0) + 
theme_bw() + ylab("") + xlab("") + 
theme(panel.border = element_blank(), 
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(), 
    strip.text  = element_blank(), 
    strip.background = element_blank(), 
    axis.text.y  = element_blank(), 
    legend.position = "none", 
    axis.ticks  = element_blank()) + 
scale_fill_manual(values = c("transparent", "transparent", "transparent", "transparent")) 

Less than optimal...

Einige Vorschläge für diese grobe Handlung in etwas drehen veröffentlichbare ggplot2 mit?

Vielen Dank! Much better!

Antwort

2

Was ist dies für einen Start:

temp <- data.frame(Exercise=c(1, 1, 1, 1), Name=c(1, 2, 3, 4), Score=c(90, 180, 270, 360)) 
temp2 <- data.frame(Name=c(2.8, 2.8, 2.8, 2.8), Score=c(90, 180, 270, 360)) 
temp3 <- data.frame(Name=c(4.2, 4.2, 4.2, 4.2), Score=c(90, 180, 270, 360)) 
temp4 <- data.frame(Name=c(0), Score=c(180)) 
temp5 <- data.frame(Name=c(4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8), Score=c(45, 90, 135, 180, 225, 270, 305, 360)) 

ggplot() + 
geom_bar(data=temp, aes(x = factor(Name), y=Score, fill = factor(Exercise)), width = 1, stat='identity') + 
geom_point(data=temp2, aes(x=Name, y=Score), color="grey") + 
coord_polar(theta = "y", start=0) + 
theme_bw() + ylab("") + xlab("") + 
scale_y_continuous(breaks = c(90, 180, 270, 360)) + 
theme(panel.border=element_blank(), 
panel.grid.minor=element_blank(), 
panel.grid.major=element_blank(), 
strip.text=element_blank(), 
strip.background=element_blank(), 
axis.text.y=element_blank(), 
legend.position="none", 
axis.ticks = element_blank()) + 
scale_fill_manual(values = c("transparent", "transparent", "transparent", "transparent")) + 
geom_vline(xintercept=4.8) + 
geom_point(data=temp4, aes(x=Name, y=Score), color="black", shape=3, size=4) + 
geom_point(data=temp3, aes(x=Name, y=Score), color="black") + 
geom_point(data=temp5, aes(x=Name, y=Score), color="black", shape=3, size=2) 

enter image description here

+0

Vielen Dank! Das ist sicherlich ein viel schärfer aussehender Graph. Gibt es eine Möglichkeit, zwei Sätze von Winkeldaten wie in meinem OP beschrieben zu zeichnen? –

+1

Sie haben zwei Sätze, einen schwarzen, einen grauen. Spielen Sie einfach mit den Datenframes temp3 und temp4 und Sie werden die Punkte bewegen. Wenn Sie die schwarzen Punkte als Häkchen für die 4 Winkel behalten möchten, erstellen Sie einfach einen neuen Datenrahmen und fügen den neuen Datenrahmen mit einem zusätzlichen geom_point() hinzu. – MLavoie

+1

siehe Bearbeitungen und neues Bild, 2 Sätze Punkte – MLavoie

Verwandte Themen