3
CODE修訂增添光彩本地圓形地塊使用GGPLOT2
我有一個從動物行爲的研究,我想用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一些建議嗎?
謝謝!這當然是一個更清晰的圖表。有沒有一種方法來繪製兩組角度數據,如我在OP中所描述的那樣? –
你有兩套,一套黑色,一套灰色。只要玩數據幀temp3和temp4,你會移動點。如果你想保留黑點作爲4個角度的刻度線,只需要補充一個新的數據幀,並添加新的數據幀和一個額外的geom_point() – MLavoie
查看編輯和新圖像,2套點 – MLavoie