[英]Order Bubble Chart bubbles along y-axis using ggplot2 in 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()
给我下图:
任何人都可以提出一种沿 y 轴或其他方式表示 Group_Quality 的方法吗? 我很茫然。
数据
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))
这是使用ggplot
的一种可能方法。 但是,添加 y 轴会破坏气泡图 ide。
最好的方法可能是将 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)
或者您可以将它们设置为常量:
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 = '')
由代表 package (v0.3.0) 于 2020 年 12 月 17 日创建
编辑
这是标签中添加的换行符:
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)
由代表 package (v0.3.0) 于 2020 年 12 月 19 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.