2017-12-02 10 views
1

でggplot2を使用して、異なるサイズの複数の円グラフの散布図を作る:は、私は次のようなデータを含むデータフレームを持つR

> data_graph 
# A tibble: 12 x 4 
# Groups: ATTPRO, ATTMAR [?] 
     x  y group nb 
    <dbl> <dbl> <chr> <int> 
1  0  0  1 1060 
2  0  0  2 361 
3  0  0  3 267 
4  0  1  1 788 
5  0  1  2 215 
6  0  1  3 80 
7  1  0  1 485 
8  1  0  2 168 
9  1  0  3 101 
10  1  1  1 6306 
11  1  1  2 1501 
12  1  1  3 379 

私の目的は以下のチャートを持っていることです。

  • 両方のX/Y軸と置くべきXY、質的変数、
  • NB、quantita

この使用ggplot2パッケージは、これだけのコードで、泡を私に与えているapproching最良の結果をパイの部分を表すパイの大きさを表す

  • グループ、質的変数、TIVE変数、。私はその中にパイを置くための解決策を見つけることができません:scatterpieパッケージを使用

    library(ggplot2) 
        ggplot(data_graph, aes(y = factor(y),x = factor(x))) + 
        geom_point(aes(colour = group, size = nb)) + 
        theme_bw() + 
        cale_size(range = c(1, 20)) + 
        labs(x = "x", y = "y", color = "group", size = "nb") 
    

    enter image description here

    はそれほど助けにはなりませんでした。今回はパイがよく描かれていますが、nbを使用して円のサイズを定義する方法が見つかりません。また、xyは、質的変数ではなく、factor()としました。結果は完全な伝説なしでかなり醜いです。このコードは、2番目の1のパイを持つ第一のチャートを持つために変更することができますどのように

    > tmp 
        x y A B C 
    1 0 0 1060 361 267 
    2 0 1 788 215 80 
    3 1 0 485 168 101 
    4 1 1 6306 1501 379 
    
    library(scatterpie) 
    ggplot() + 
        geom_scatterpie(aes(x = x, y = y), data = tmp, cols = c("A", "B", "C")) + 
        coord_fixed() 
    

    enter image description here

  • +0

    少ない画像を使用して、増加を希望の場合は特に、問題の一部として使用されるデータサンプルを追加してください。答えを得る確率。 – Heikki

    +0

    @Heikkiヒントをありがとう。私はデータフレームのコードを修正しました。 – MarieT

    +1

    一般に、データを 'dput()'の出力として貼り付けたいので、コードにコピーしてデータフレームを直接作り直すことができます。 –

    答えて

    5

    これはggforceのgeom_arc_bar()のケースと思われますが、いくつかのdplyrの魔法です。これは、連続変数としてxyを扱いますが、それは問題ではありません。右軸設定を設定することで、離散しているふりをすることができます。

    データ:

    data_graph <- read.table(text = "x  y group nb 
    1  0  0  1 1060 
    2  0  0  2 361 
    3  0  0  3 267 
    4  0  1  1 788 
    5  0  1  2 215 
    6  0  1  3 80 
    7  1  0  1 485 
    8  1  0  2 168 
    9  1  0  3 101 
    10  1  1  1 6306 
    11  1  1  2 1501 
    12  1  1  3 379", header = TRUE) 
    

    コード:

    library(ggforce) 
    library(dplyr) 
    
    # make group a factor 
    data_graph$group <- factor(data_graph$group) 
    
    # add case variable that separates the four pies 
    data_graph <- cbind(data_graph, case = rep(c("Aaaa", "Bbbb", "Cccc", "Dddd"), each = 3)) 
    
    # calculate the start and end angles for each pie 
    data_graph <- left_join(data_graph, 
        data_graph %>% 
        group_by(case) %>% 
        summarize(nb_total = sum(nb))) %>% 
        group_by(case) %>% 
        mutate(nb_frac = 2*pi*cumsum(nb)/nb_total, 
         start = lag(nb_frac, default = 0)) 
    
    # position of the labels 
    data_labels <- data_graph %>% 
        group_by(case) %>% 
        summarize(x = x[1], y = y[1], nb_total = nb_total[1]) 
    
    # overall scaling for pie size 
    scale = .5/sqrt(max(data_graph$nb_total)) 
    
    # draw the pies 
    ggplot(data_graph) + 
        geom_arc_bar(aes(x0 = x, y0 = y, r0 = 0, r = sqrt(nb_total)*scale, 
            start = start, end = nb_frac, fill = group)) + 
        geom_text(data = data_labels, 
          aes(label = case, x = x, y = y + scale*sqrt(nb_total) + .05), 
          size =11/.pt, vjust = 0) + 
        coord_fixed() + 
        scale_x_continuous(breaks = c(0, 1), labels = c("X0", "X1"), name = "x axis") + 
        scale_y_continuous(breaks = c(0, 1), labels = c("Y0", "Y1"), name = "y axis") + 
        theme_minimal() + 
        theme(panel.grid.minor = element_blank()) 
    

    enter image description here

    関連する問題