Закажите пузыри на пузырьковой диаграмме вдоль оси y, используя ggplot2 в r

Я хочу создать пузырьковую диаграмму, используя следующие данные:

> 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  

Я хотел бы, чтобы размер каждого пузыря указывал столбец Group_N, цвет каждого пузыря указывал столбец Group_Strength, а ось Y - столбец Group_Quality.

До сих пор мне удалось представить столбец Group_N (размер пузыря) и столбец Group_strength (цвет), используя следующий код. Но я не могу понять, как изменить порядок пузырьков по оси 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()

Дает мне следующую цифру:

BubblePlot

Может ли кто-нибудь предложить способ представления Group_Quality по оси Y или каким-либо другим способом? Я в недоумении.


Данные

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

person novicecoder    schedule 17.12.2020    source источник


Ответы (1)


Вот один из возможных способов использования ggplot. добавление оси Y разрушает идеи пузырькового графика.

Самый лучший способ - это, вероятно, также добавить группы как 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)

Или вы можете просто установить их как константу:

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 = '')

Создано 17 декабря 2020 г. пакетом REPEX (v0.3.0)


ИЗМЕНИТЬ

Вот добавленный разрыв строки в ярлыках:

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)

Создано 19 декабря 2020 г. пакетом REPEX (v0.3.0)

person Max Teflon    schedule 17.12.2020
comment
Я думаю, что это отличные варианты, поскольку не представляется возможным добавить значимую ось с пакетом packcircles. @Max знаете ли вы, можно ли создавать метки (внутри графика или по оси x), где метка разделена на две строки, чтобы улучшить читаемость? - person novicecoder; 19.12.2020
comment
Вы имеете в виду разрыв строки в ярлыках? Я добавлю решение для этого. - person Max Teflon; 19.12.2020