2016-05-09 109 views
0

ggplot2でレーダーチャートを作ろうとしています。ggplot2を使ったレーダーチャート/蜘蛛図

> Test1 
    Country fertility gengap LEB 
1 Colombia  1.92 0.29 79.30 
2  Peru  1.94 0.37 78.40 
3  Min  9.23 3.83 0.00 
4  Max  1.59 23.70 78.43 

#normalizing the data: 

Test1_norm<- data.frame(sapply(Test1[1:4, 2:4], scales::rescale)) 

    > Test1_norm 
    fertility  gengap  LEB 
1 0.04319372 0.000000000 1.0000000 
2 0.04581152 0.003417343 0.9886507 
3 1.00000000 0.151217428 0.0000000 
4 0.00000000 1.000000000 0.9890290 

私は、このコードは、レーダーチャートにプロットすることが分かっ:mtcars-例えば http://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.html を:

ggplot(mtcarsmelted, aes(x = variable, y = value)) + 
    geom_path(aes(group = model, color = model), size = 2) + 
    theme(strip.text.x = element_text(size = rel(0.8)), 
     axis.text.x = element_text(size = rel(0.8)), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_blank()) + 
    xlab("") + ylab("") + 
    guides(color = guide_legend(ncol=2)) + 
    coord_polar() 

すべては私のために正常に動作しますが、私は書き換えしようとすると、mtcarsの例を使用します私のデータのためのコード私はちょうどエラーを取得します。私は数時間試していましたが、私はどの変数を交換する必要があるか、この例のようにとどまる必要があるかどうかはわかりません。

私は少し助けていただければ幸いです!

おかげで皆:)

+0

http://stackoverflow.com/questions/9614433/creating-radar-chart-a-k-a-star-plot-spider-plot-using-ggplot2-in-r?rq=1 – bVa

答えて

0

まず、通常のデータを使用して、私はあなたが私に表示する方法に従ってレーダーチャートをプロットしようとすると)、私はまだRに非常に新しいです。 reshape2パッケージが必要です。ここでは、コードがあります:

幸い
Test1_norm <- data.frame(group=c("Colombia","Peru","Min","Max"), 
       fertility=c(0.04319372,0.04581152,1,0), 
       gengap=c(0,0.003417343,0.151217428,1), 
       LEB=c(1,0.9886507,0,0.9890290)) 
Test1_norm$model <- Test1_norm$group 
library(reshape2) 
Test1_plot<- melt(Test1_norm) 

##Define a radar coordinate system 
coord_radar <- function (theta = "x", start = 0, direction = 1) 
{ 
    theta <- match.arg(theta, c("x", "y")) 
    r <- if (theta == "x") 
     "y" 
    else "x" 
    ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
     direction = sign(direction), 
     is_linear = function(coord) TRUE) 
} 

#Plot 
library(ggplot2) 
ggplot(Test1_plot, aes(x = variable, y = value)) + 
geom_polygon(aes(group = model, color = model), fill = NA, size = 2, show.legend = FALSE) + 
    geom_line(aes(group = model, color = model), size = 2) + 
    theme(strip.text.x = element_text(size = rel(0.8)), 
     axis.text.x = element_text(size = rel(0.8)), 
     axis.ticks.y = element_blank(), 
     axis.text.y = element_blank()) + 
    xlab("") + ylab("") + 
    guides(color = guide_legend(ncol=2)) + 
    coord_radar() 

、リカルドバイオンはあなたに役に立つかもしれggplot2.Thisでレーダーチャートを作成するための効率的な方法を提供する:https://github.com/ricardo-bion/ggradar をあなたはパッケージをインストールし、手動で導入されggradar()関数を呼び出すことができます。元のデータを持つ私のコードです。

Test1 <- data.frame(fertility=c(1.92,1.94,9.23,1.59), 
        gengap=c(0.29,0.37,3.83,23.70), 
        LEB=c(79.30,78.40,0.00,78.43)) 
rownames(Test1)<-c("Colombia","Peru","Min","Max") 

source("ggradar.R") 
library(dplyr) 
library(ggplot2) 
library(scales) 
Test1 %>% 
     add_rownames(var = "group") %>% 
     mutate_each(funs(rescale), -group) -> Test1_radar 
ggradar(Test1_radar) 
ggsave("Test1_radar.tiff",width = 8, height = 8) 
関連する問題