ggplot:如何根据组ID更改垂直线的颜色(在极坐标图中)

我有以下数据帧(输出dput(df2)):

structure(list(angles = c(-0.701916320805404, 2.33367948606366, 
0.364313791379516, -0.228918909875176, -2.77064550417737, 2.97776037032614, 
-3.03604124258522, 2.10507549390108, 2.07708771915781, -0.0646656487453258, 
-0.701916320805404, 2.33367948606366, 0.364313791379516, -0.228918909875176, 
-2.77064550417737, 2.97776037032614, -3.03604124258522, 2.10507549390108, 
2.07708771915781, -0.0646656487453258, -0.701916320805404, 2.33367948606366, 
0.364313791379516, -0.228918909875176, -2.77064550417737, 2.97776037032614, 
-3.03604124258522, 2.10507549390108, 2.07708771915781, -0.0646656487453258
), id = c(9L, 4L, 5L, 6L, 3L, 10L, 3L, 4L, 4L, 6L, 1L, 4L, 5L, 
6L, 2L, 1L, 3L, 4L, 4L, 6L, 1L, 7L, 5L, 6L, 2L, 1L, 3L, 4L, 4L, 
6L), method = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("kd-clips", "QT-Clust", "True"
), class = "factor"), truid = structure(c(1L, 4L, 5L, 6L, 2L, 
1L, 3L, 4L, 4L, 6L, 1L, 4L, 5L, 6L, 2L, 1L, 3L, 4L, 4L, 6L, 1L, 
4L, 5L, 6L, 2L, 1L, 3L, 4L, 4L, 6L), .Label = c("1", "2", "3", 
"4", "5", "6"), class = "factor")), .Names = c("angles", "id", 
"method", "truid"), row.names = c(940L, 474L, 889L, 298L, 222L, 
932L, 87L, 695L, 261L, 832L, 1940L, 1474L, 1889L, 1298L, 1222L, 
1932L, 1087L, 1695L, 1261L, 1832L, 2940L, 2474L, 2889L, 2298L, 
2222L, 2932L, 2087L, 2695L, 2261L, 2832L), class = "data.frame")

我运行以下代码来制作下面的图:

df2$y <- as.numeric(as.factor(df2$method))  + 3
df2$yend <- df2$y + 1

library(ggplot2)
library(RColorBrewer)
cx <- ggplot(df2, aes(y = y, x = angles))
cx + geom_point(aes(color = as.factor(id))) + ylim(0,6)  + theme_light() + scale_colour_brewer(palette = "Paired") +
  scale_x_continuous(labels = NULL, breaks = df2$angles)+coord_polar() + 
  theme(legend.position="none",  panel.border=element_blank(), axis.title =
        element_blank(), axis.text = element_blank())

我得到下图:

《ggplot:如何根据组ID更改垂直线的颜色(在极坐标图中)》

几乎在那里,但我想要得到的还有两件事:

>根据df2的最后一列(true.id)进行着色的径向线(与第三个同心圆中的点颜色相同 – 与id ==“True”相同).
>我也想要一个径向刻度,以30的间隔标记(如0°,30°,60°,90°,… 330°0的角度).但是,我不希望左边的比例(y的).
>以上有30个点,每个方法在每个角度重复三次.然而,这些数字似乎只绘制了9个重复,即总共27个点. (有可能两个角度 – 一个是2.077,一个是2.105)非常接近,所以它们实际上可能都存在,但是我无法分辨,因为那两个点彼此接近的是什么?

我整天都在尝试,但是无法让这两种方法起作用,所以我想知道是否有人可以提供帮助.

提前致谢!

最佳答案 我想这可能是你想要的(我知道,很久以前现在……).有些你错了我实际上无法说清楚,但尝试使用坐标网格线来显示数据几乎总是错误的 – 使用geom_line或geom_segment来显示数据,并留下网格线来显示坐标.这解决了你需要将线条着色,并使x网格线(即径向线条)更容易拥有你想要的标签(无论我是否正确理解你,重新度数弧度,我不是当然).

《ggplot:如何根据组ID更改垂直线的颜色(在极坐标图中)》

library(ggplot2)
library(RColorBrewer)

# your "angle" looks to be in radians, not sure how you want these converted to degrees?
# but let's set where we want the axis marks to be
br <- seq(from = -pi, to = pi, length.out = 7)

ggplot(df2, aes(y = y, x = angles)) +
  geom_point(aes(color = as.factor(id))) + 
  theme_light() + 
  scale_colour_brewer(palette = "Paired") +
  geom_vline(aes(xintercept = angles, colour = truid)) +
  coord_polar() +
  scale_x_continuous(lim = c(-pi, pi), breaks = br, labels = 0:6 * 60)  +
  theme(legend.position="none",  
        panel.border=element_blank(), 
        axis.title = element_blank(), 
        axis.text.y = element_blank())
点赞