Ggplot: how to change the color of vertical lines in accordance with the group identifier (in the polar plot)

I have the following dataframe (dput (df2) output):

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

I run the following code to make the following graph:

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

I get the following picture:

enter image description here

Almost there, but I would like to get two more things:

  • The radial lines that should be colored according to the last column (true.id) df2 (which has the same color as the points in the third concentric circle are the same as for id == "True").
  • I would also like to get a radial scale marked with an interval of 30 (for example, at angles 0, 30, 60, 90, ... 330. 0). However, I do not need the scale on the left (from y).
  • 30 , . , 9 , .. 27 . (, - 2.077 2.105) , , , ?

, , , - .

!

+4
1

, , , , ( , ...). , , , - . , (.. ) , ( , re degrees v. Radians, ).

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

Source: https://habr.com/ru/post/1612104/


All Articles