gpt4 book ai didi

r - ggplot : how to change colors of vertical lines according to group id (in polar plot)

转载 作者:行者123 更新时间:2023-12-04 06:14:56 27 4
gpt4 key购买 nike

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

差不多了,但我还想得到两件事:
  • 根据 df2 的最后一列 (true.id) 着色的径向线(与第三个同心圆中的点颜色相同——与 id == "True"相同)。
  • 我还想要一个径向刻度,以 30 的间隔标记(例如以 0、30、60、90、... 330. 0 的角度)。但是,我不想要左边的比例(y 的)。
  • 上面有 30 个点,每个方法在每个角度重复 3 次。然而,这些数字似乎只绘制了 9 个重复,即总共 27 个点。 (有可能两个角度——一个是 2.077,另一个是 2.105)非常接近,所以它们真的可能都在那里,但我不知道因为那么彼此接近的两个点是什么?

  • 我已经尝试了一整天,但无法让其中任何一个工作,所以我想知道是否有人可以提供帮助。

    提前致谢!

    最佳答案

    我想这可能是你想要的(我知道,很久以前现在......)。你犯的一些错误我实际上不太清楚,但尝试使用坐标网格线来显示数据几乎总是一个错误 - 使用 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())

    关于r - ggplot : how to change colors of vertical lines according to group id (in polar plot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194667/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com