使用R中的ggplot2制作不同大小的多个饼图的散点图 [英] Making a scatter plot of multiple pie charts of differing sizes, using ggplot2 in R

查看:3157
本文介绍了使用R中的ggplot2制作不同大小的多个饼图的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的数据框:

 > data_graph 
#一个tibble:12 x 4
#组:ATTPRO,ATTMAR [?]
x y组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轴 x y li>
  • nb ,定量变量,表示饼图大小

  • ,定性变量,表示饼图部分



使用 ggplot2 包来处理这个问题的最佳结果只能让我看到这个代码。我找不到一个解决方案把它放在它内:

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



使用 scatterpie 包没有那么多帮助。这次馅饼绘制得很好,但我找不到使用 nb 定义饼图大小的方法。另外, x y 被视为定量变量(我没有任何机会尝试过 factor())而不是定性变量。结果非常难看,没有完整的图例。

 > tmp 
xy ABC
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()


I have a data frame containing the following data:

> 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

My objective is to have the following chart:

  • Both x and y, qualitative variables, to be put as X/Y axis
  • nb, quantitative variable, representing pie size
  • group, qualitative variable, representing pie parts

The best result approching this using ggplot2 package is only giving me bubbles, with this code. I can't find a solution to put pies within it:

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")

Using scatterpie package did not help that much. This time pies are well drawn, but I can't find a way to use nb to define pie size. Also, x and y are treated as quantitative variables (I tried factor() without any chance) instead of qualitative ones. The result is pretty ugly, without a full legend.

> 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()

How can this code be altered in order to have the 1st chart with the 2nd one's pies?

解决方案

This seems to be a case for geom_arc_bar() from ggforce, with some dplyr magic. This treats x and y as continuous variables, but that's not a problem, you can pretend they are discrete by setting the right axis settings.

The data:

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)

The code:

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())

这篇关于使用R中的ggplot2制作不同大小的多个饼图的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆