2015-10-18 75 views
4

我有以下的數據框(dput的輸出(DF2)):ggplot:如何根據組ID(以極座標圖)改變垂直線顏色

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°,330°,0°)。但是,我不希望左邊的(Y's)的比例。
  3. 上面有30個點,每個角度的每個方法有三個重複。但是,這些數字似乎只繪製了9次重複,即總共27個點。 (可能有兩個角度--2.077和2.105的角度)非常接近,所以他們確實可能都在那裏,但我不能說因爲那兩點彼此接近?

我已經嘗試了一整天,但無法讓這兩個工作,所以我想知道如果任何人都可以幫助。

在此先感謝!

回答

0

我想這可能是你想要的(我知道,很久以前現在...)。其中一些錯誤我實際上不太清楚,但嘗試使用座標網格來顯示數據幾乎總是一個錯誤 - 對數據使用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())