CODE UPDATED
我有一些来自动物行为研究的角度数据,我想用ggplot2绘制出版物。接下来是我当前的工作流程,其中包含一些示例数据以及使用通用绘图函数的外观。
### 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"))
使用ggplot2将这个粗略的情节变成可以发布的东西的一些建议?
答案 0 :(得分:2)
一开始怎么样:
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)