2015-04-04 96 views
3

我正在使用和ggplot2(或R中的任何其他軟件包)在附件image中製作雷達圖。 This談論這個,但我的情況是不同的,因爲我試圖創建一個具有一定範圍的響應數據的蜘蛛情節。 我使用下面的代碼做了一個情節,但我無法弄清楚如何在圖像中做出這種情況。請幫助我。如何繪製ggplot2或R中的雷達圖表

Impcts <- c("system","supply","security","well-being") 
present <- c(5,5,3,5) 
past <- c(6,6,4,5) 
group.names <- c("present", "past") 
ddf.pre <- data.frame(matrix(c(rep(group.names[1], 4), Impcts), nrow = 4,  ncol = 2), var.order = seq(1:4), value = present) 
ddf.pas <- data.frame(matrix(c(rep(group.names[2], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = past) 
ddf <- rbind(ddf.pre, ddf.pas) 
colnames(ddf) <- c("Group", "Impcts", "var.order", "var.value") 
library(ggplot2) 
ggplot(ddf, aes(y = var.value, x = reorder(Impcts, var.order), 
group = Group, colour = Group))+ 
coord_polar() + 
geom_path() + 
geom_point()+ 
labs(title = "Impacts Analysis"). 

enter image description here

回答

4

下面是使用geom_path()我attempt.First我畫正方形。然後,我使用geom_polygon()在正方形的頂部繪製了兩個多邊形。最後我添加了註釋。

### Draw squares 
mydf <- data.frame(id = rep(1:6, each = 5), 
        x = c(0, 6, 0, -6, 0, 
         0, 5, 0, -5, 0, 
         0, 4, 0, -4, 0, 
         0, 3, 0, -3, 0, 
         0, 2, 0, -2, 0, 
         0, 1, 0, -1, 0), 
        y = c(6, 0, -6, 0, 6, 
         5, 0, -5, 0, 5, 
         4, 0, -4, 0, 4, 
         3, 0, -3, 0, 3, 
         2, 0, -2, 0, 2, 
         1, 0, -1, 0, 1)) 

g <- ggplot(data = mydf, aes(x = x, y = y, group = factor(id)) + 
    geom_path() 

### Draw polygons 
mydf2 <- data.frame(id = rep(7:8, each = 5), 
        x = c(0, 6, 0, -5, 0, 
          0, 5, 0, -5, 0), 
        y = c(6, 0, -4, 0, 6, 
          5, 0, -3, 0, 5)) 

gg <- g + 
     geom_polygon(data = mydf2, aes(x = x, y = y, group = factor(id), fill = factor(id))) + 
     scale_fill_manual(name = "Time", values = c("darkolivegreen4", "brown4"), 
         labels = c("Past", "Present")) 


### Add annotation 

mydf3 <- data.frame(x = c(0, 6.5, 0, -6.5), 
        y = c(6.5, 0, -6.5, 0), 
        label = c("system", "supply", "security", "well-being")) 


ggg <- gg + 
     annotate("text", x = mydf3$x, y = mydf3$y, label = mydf3$label, size = 3) 

ggsave(ggg, file = "name.png", width = 10, height = 9) 

enter image description here

+0

親愛@jazzurro,謝謝你不錯的鍛鍊,並提出解決方案。在我的情況下,我必須製作超過30個不同的雷達圖表。在每種情況下,當前和過去的變化值基於響應(範圍從0-10)。因此,我會很好地建議我對代碼進行可能的修改,以便它可以用於當前和過去的不同值。過去和現在的價值變化會改變多邊形的形狀。謝謝。 – Cirrus 2015-04-05 02:42:49

+1

@Cirrus感謝您的評論。 1)我的評分等級爲1-10,你想在第一步中畫10格。在這裏我有一個1-6的比例。您可以輕鬆修改此部分。 2)如果你有超過30個圖表,你可能要考慮在ggplot函數中使用'lapply()'。您可以創建包含所有點數據的大數據框。你使用特定的位置名稱或類似的東西來對數據進行分組。只要您像我一樣指定x和y值,則可以爲圖表繪製多邊形。希望這可以在一定程度上幫助你。 – jazzurro 2015-04-05 03:14:18

+0

謝謝@jazzuuro。我會嘗試使用多個圖的方面。謝謝。 – Cirrus 2015-04-05 05:52:02