2017-12-02 261 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ÿ,定性變量,被投入作爲X/Y軸
  • NB,quantita略去變量,代表餡餅大小
  • ,定性變量,代表餡餅份

最好結果approching此使用GGPLOT2包只給我氣泡,與此代碼。我無法找到一個解決方案,把在它的餡餅:

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

使用scatterpie包沒有幫助那麼多。這次餡餅畫得很好,但我找不到一種方法來使用nb來定義餅圖大小。另外,xy被視爲定量變量(我嘗試因子()沒有任何機會),而不是定性變量。結果很醜,沒有完整的圖例。

> 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