r - 使用 ggplot2 为原生圆形图增添趣味

标签 r plot 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"))

Less than optimal...

关于使用 ggplot2 将这个粗略的图变成可发布的东西的一些建议?

谢谢! Much better!

最佳答案

从这个开始:

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

关于r - 使用 ggplot2 为原生圆形图增添趣味,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36962325/

相关文章:

r - 跨不同 ggplot2 map 的一致 alpha 比例

r - 在ggplot2对象的现有图层下插入图层

r - 如何为 R 设置 rselenium?

r - 在R中,有一种算法可以创建大小近似相等的簇

r - 如何在 Flexdashboard 中包含 javascript 和 css?

python - 如何在 matplotlib.pyplot.imshow 中使用 'extent'

r - 为什么在使用 plotstyle ="ggplot"时 qqcomp 函数中没有显示任何点?

R 绘图限制和轴标签/标题之间的通用距离

MATLAB 旋转 xtick 标签

r - 更改组合 ggplot 中的条形颜色