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

4

我有以下数据框(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())

我得到了以下图像:

enter image description here

几乎完成,但我想要的是两件事:
  1. 根据df2的最后一列(true.id),将放射线染色(与第三个同心圆中的点相同的颜色--与id ==“True”相同的颜色)。
  2. 我还想要一个径向刻度标尺,间隔为30(如在0、30、60、90等角度处)。但是,我不希望在y轴的左侧有刻度标尺。
  3. 上面有30个点,每个角度的每种方法都有三个重复。然而,图只显示了9个重复,即总共27个点。 (可能有两个角度--一个为2.077,另一个为2.105--非常接近,因此它们可能真的都在那里,但我无法确定,因为那么接近的两个点是什么?)
我已经尝试了一整天,但没有得到任何一个工作,所以我想知道是否有人可以帮忙。
提前感谢!
1个回答

1
我认为这可能是您想要的(我知道,现在已经很久了...)。您错误的部分我有些无法确定,但几乎总是错误的尝试使用坐标网格线来显示数据 - 使用geom_line或geom_segment来显示数据,并将网格线用于显示坐标。这解决了您需要对线条进行着色的问题,并使x网格线(即径向网格线)更容易具有所需的标签(无论我是否正确理解您关于度数与弧度的问题,我不确定)。

enter image description here

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接