简体   繁体   English

使用 r 中的 ggplot2 沿 y 轴订购气泡图气泡

[英]Order Bubble Chart bubbles along y-axis using ggplot2 in r

I want to make a bubble chart using the following data:我想使用以下数据制作气泡图:

> head(data1)
# A tibble: 6 x 4
  Group                        Group_Strength Group_N Group_Quality
  <chr>                                 <dbl>   <dbl>         <dbl>
1 Young Controls                         1          1          18.5
2 Healthy Age-Matched Controls          59         59          20  
3 Neurodegenerative Disease             36        178          19.1
4 Right Hemisphere Stroke                7.86     159          20.1
5 Left Hemisphere Stroke               -19         19          15  
6 Bilateral Stroke                      26         26          17  

I would like the size of each bubble to indicate the Group_N column, the color of each bubble to indicate the Group_Strength column, and the y-axis to indicate the Group_Quality column.我希望每个气泡的大小表示 Group_N 列,每个气泡的颜色表示 Group_Strength 列,y 轴表示 Group_Quality 列。

SO far I have been able to represent the Group_N column (bubble size) and the Group_strength column (color) using the following code.到目前为止,我已经能够使用以下代码表示 Group_N 列(气泡大小)和 Group_strength 列(颜色)。 But I cannot figure out how to change the order of the bubbles along the y-axis to indicate Group_Quality:但我无法弄清楚如何更改气泡沿 y 轴的顺序以指示 Group_Quality:

library(packcircles)
library(ggplot2)
library(tidyverse)

data1$Group_N <- as.numeric(data1$Group_N)

# Generate the layout. sizetype can be area or radius, following your preference on what to be proportional to value.
packing1 <- circleProgressiveLayout(data1$Group_N, sizetype='area')
data1 <- cbind(data1, packing1)
dat.gg1 <- circleLayoutVertices(packing1, npoints=50)


dat.gg1$Group_Strength <- rep(data1$Group_Strength, each=51)
# Plot
ggplot() + 
  
  # bubbles
  geom_polygon(data = dat.gg1, aes(x, y, group = id, fill=Group_Strength), colour = "black", alpha = 0.6) +
  scale_fill_distiller(palette = "BuPu", direction = 1 ) +
  
  # text for each bubble
  geom_text(data = data1, aes(x, y, size=Group_N, label = Group)) +
  scale_size_continuous(range = c(1,4)) +
  
  # Theme:
  theme_void()  + 
  theme(legend.position="none") + 
  coord_equal()

Giving me the following figure:给我下图:

气泡图

Can anyone suggest a way to represent Group_Quality along the y-axis, or in some other way?任何人都可以提出一种沿 y 轴或其他方式表示 Group_Quality 的方法吗? I am at a loss.我很茫然。


Data数据

data1 <- structure(list(Group = c("Young Controls", 
"Healthy Age-Matched Controls", 
"Neurodegenerative Disease", "Right Hemisphere Stroke", "Left Hemisphere Stroke", 
"Bilateral Stroke"), Group_Strength = c(1, 59, 36, 7.86, -19, 
26), Group_N = c(1L, 59L, 178L, 159L, 19L, 26L), Group_Quality = c(18.5, 
20, 19.1, 20.1, 15, 17)), class = "data.frame", row.names = c(NA, 
-6L))

Here is one possible way using ggplot .这是使用ggplot的一种可能方法。 adding the y-axis kind of destroyes the bubble-plot ides though.但是,添加 y 轴会破坏气泡图 ide。

The nicest way is probably to add the Groups as x as well:最好的方法可能是将 Groups 添加为 x :

library(tidyverse)

data1 <- structure(
  list(
    Group = c(
      "Young Controls",
      "Healthy Age-Matched Controls",
      "Neurodegenerative Disease",
      "Right Hemisphere Stroke",
      "Left Hemisphere Stroke",
      "Bilateral Stroke"
    ),
    Group_Strength = c(1, 59, 36, 7.86,-19,
                       26),
    Group_N = c(1L, 59L, 178L, 159L, 19L, 26L),
    Group_Quality = c(18.5,
                      20, 19.1, 20.1, 15, 17)
  ),
  class = "data.frame",
  row.names = c(NA,-6L)
)

data1 %>%
  ggplot(aes(x = Group, 
             y = Group_Quality
             )) +
  geom_point(aes(
    size = Group_N,
    color = Group_Strength)) +
  theme_minimal() +
  scale_size_area(max_size = 20) +
  geom_label(aes(label = Group),
             nudge_y = - .25 * IQR(data1$Group_Quality)) +
  ylim(min(data1$Group_Quality) * .9,
       max(data1$Group_Quality) * 1.05)

Or you could just set them as a constant:或者您可以将它们设置为常量:

library(tidyverse)

data1 <- structure(
  list(
    Group = c(
      "Young Controls",
      "Healthy Age-Matched Controls",
      "Neurodegenerative Disease",
      "Right Hemisphere Stroke",
      "Left Hemisphere Stroke",
      "Bilateral Stroke"
    ),
    Group_Strength = c(1, 59, 36, 7.86,-19,
                       26),
    Group_N = c(1L, 59L, 178L, 159L, 19L, 26L),
    Group_Quality = c(18.5,
                      20, 19.1, 20.1, 15, 17)
  ),
  class = "data.frame",
  row.names = c(NA,-6L)
)

data1 %>%
  ggplot(aes(x = 1, 
             y = Group_Quality
             )) +
  geom_point(aes(
    size = Group_N,
    color = Group_Strength)) +
  theme_minimal() +
  scale_size_area(max_size = 20) +
  geom_label(aes(label = Group),
             nudge_y = - .25 * IQR(data1$Group_Quality)) +
  ylim(min(data1$Group_Quality) * .9,
       max(data1$Group_Quality) * 1.05) +
  scale_x_continuous(breaks = NULL,name = '')

Created on 2020-12-17 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 12 月 17 日创建


EDIT编辑

Here's an added linebreak in the labels:这是标签中添加的换行符:

library(tidyverse)

data1 <- structure(
  list(
    Group = c(
      "Young Controls",
      "Healthy Age-Matched Controls",
      "Neurodegenerative Disease",
      "Right Hemisphere Stroke",
      "Left Hemisphere Stroke",
      "Bilateral Stroke"
    ),
    Group_Strength = c(1, 59, 36, 7.86,-19,
                       26),
    Group_N = c(1L, 59L, 178L, 159L, 19L, 26L),
    Group_Quality = c(18.5,
                      20, 19.1, 20.1, 15, 17)
  ),
  class = "data.frame",
  row.names = c(NA,-6L)
)

data1 %>%
  mutate(Group = str_replace_all(Group, '\\s', '\n')) %>% 
  ggplot(aes(x = Group, 
             y = Group_Quality
  )) +
  geom_point(aes(
    size = Group_N,
    color = Group_Strength)) +
  theme_minimal() +
  scale_size_area(max_size = 20) +
  geom_label(aes(label = Group),
             nudge_y = - .35 * IQR(data1$Group_Quality)) +
  ylim(min(data1$Group_Quality) * .9,
       max(data1$Group_Quality) * 1.05)

Created on 2020-12-19 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 12 月 19 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM