在 R 中创建气泡图
Creating bubble plot in R
我在下面的 Excel 中导入了数据以在 R 中创建气泡图:
# A tibble: 6 x 3
Country Series `2019`
<chr> <chr> <dbl>
1 United Kingdom GDP per capita 42354.
2 United Kingdom Life Expectancy 81
3 United Kingdom Population (M) 67
4 United States GDP per capita 65298.
5 United States Life Expectancy 78.8
6 United States Population (M) 328
我写了一个代码,但它没有绘制任何东西。如何制作气泡图?
bubble2 <- mutate_all(bubble, function(x) as.numeric(as.character(x))) %>%
pivot_longer(cols=-c("Country","Series"),names_to="year") %>%
mutate(Year=as.numeric(year)) %>%
select(-year) %>%
ggplot(aes(x="GDP per capita", y="Life Expectancy", size="Population (M)", fill=Country)) +
geom_point(alpha=0.5, shape=21, color="black") +
scale_size(range = c(10, 1400), name="Population (M)") +
scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
ylab("Life Expectancy") +
xlab("Gdp per capita")
编辑
我又添加了 10 个国家并调整了代码:
bubble2 <- bubble %>%
pivot_wider(names_from = "Series", values_from = `2019`)
ggplot(bubble2, aes(x = `GDP per capita`, y = `Life Expectancy`, size = `Population (M)`, fill = Country)) +
geom_point(alpha = 0.5, shape = 21, color = "black") +
geom_text(aes(label = Country), size = 8 / .pt) +
#ggrepel::geom_text_repel(aes(label = Country), size = 8 / .pt) +
scale_size(range = c(.1, 24), name="Population (M)") +
ylab("Life Expectancy") +
xlab("Gdp per capita") +
theme(axis.title = element_text(size=8), plot.title = element_text(hjust = 0.5))
但是关于人口的传说发生了变化。如何显示正确的人口图例并删除国家图例?
首先。如果您的数据集包含带字符串的列,则将所有内容都转换为数字不是一个好主意。第二。您需要 pivot_wider
而不是 pivot_longer
。第三。在非标准列名称周围使用反引号 ` 而不是引号:
library(ggplot2)
library(dplyr)
library(tidyr)
library(viridis)
bubble2 <- bubble %>%
pivot_wider(names_from = "Series", values_from = `2019`)
ggplot(bubble2, aes(x = `GDP per capita`, y = `Life Expectancy`, size = `Population (M)`, fill = Country)) +
geom_point(alpha = 0.5, shape = 21, color = "black") +
# scale_size(range = c(10, 1400), name = "Population (M)") +
scale_fill_viridis(discrete = TRUE, guide = "none", option = "A") +
ylab("Life Expectancy") +
xlab("Gdp per capita")
编辑:通过geom_text
添加标签:
ggplot(bubble2, aes(x = `GDP per capita`, y = `Life Expectancy`, size = `Population (M)`, fill = Country)) +
geom_point(alpha = 0.5, shape = 21, color = "black") +
geom_text(aes(label = Country), size = 8 / .pt) +
#ggrepel::geom_text_repel(aes(label = Country), size = 8 / .pt) +
scale_fill_viridis(discrete = TRUE, guide = "none", option = "A") +
ylab("Life Expectancy") +
xlab("Gdp per capita")
数据
bubble <- structure(list(Country = c(
"United Kingdom", "United Kingdom",
"United Kingdom", "United States", "United States", "United States"
), Series = c(
"GDP per capita", "Life Expectancy", "Population (M)",
"GDP per capita", "Life Expectancy", "Population (M)"
), `2019` = c(
42354,
81, 67, 65298, 78.8, 328
)), row.names = c(NA, -6L), class = c(
"tbl_df",
"tbl", "data.frame"
))
``
加上@stefan 的回答,尺寸范围太大了。另外添加,限制可能会有所帮助。
bubble %>% pivot_wider(names_from = "Series", values_from = `2019`) %>%
ggplot(aes(x=`GDP per capita`, y=`Life Expectancy`, size=`Population (M)`, fill=Country)) +
geom_point(alpha=0.5, shape=21, color="black") +
scale_size(range = c(10, 40), name="Population (M)") +
xlim(c(30000, 80000)) + ylim(c(77, 82)) +
scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
ylab("Life Expectancy") +
xlab("Gdp per capita") +
guides(size = FALSE)
您可以通过使用 reshape2::dcast
将数据重塑为宽幅格式,然后使用基础图形的另一种方法以更少的努力完成此操作。
library(reshape2)
plot(`Life Expectancy` ~ `GDP per capita`, dcast(bubble, Country ~ Series),
cex=`Population (M)`/120, ylim=c(78.5, 81.5), main='Here might be your title')
pseq <- seq(100, 300, 50)
legend('topright', legend=pseq, pch=1, pt.cex=pseq/120, title='Population (M)')
这些颜色实际上不是必需的,是吗?
数据:
bubble <- structure(list(Country = c("United Kingdom", "United Kingdom",
"United Kingdom", "United States", "United States", "United States"
), Series = c("GDP per capita", "Life Expectancy", "Population (M)",
"GDP per capita", "Life Expectancy", "Population (M)"), `2019` = c(42354,
81, 67, 65298, 78.8, 328)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
我在下面的 Excel 中导入了数据以在 R 中创建气泡图:
# A tibble: 6 x 3
Country Series `2019`
<chr> <chr> <dbl>
1 United Kingdom GDP per capita 42354.
2 United Kingdom Life Expectancy 81
3 United Kingdom Population (M) 67
4 United States GDP per capita 65298.
5 United States Life Expectancy 78.8
6 United States Population (M) 328
我写了一个代码,但它没有绘制任何东西。如何制作气泡图?
bubble2 <- mutate_all(bubble, function(x) as.numeric(as.character(x))) %>%
pivot_longer(cols=-c("Country","Series"),names_to="year") %>%
mutate(Year=as.numeric(year)) %>%
select(-year) %>%
ggplot(aes(x="GDP per capita", y="Life Expectancy", size="Population (M)", fill=Country)) +
geom_point(alpha=0.5, shape=21, color="black") +
scale_size(range = c(10, 1400), name="Population (M)") +
scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
ylab("Life Expectancy") +
xlab("Gdp per capita")
编辑 我又添加了 10 个国家并调整了代码:
bubble2 <- bubble %>%
pivot_wider(names_from = "Series", values_from = `2019`)
ggplot(bubble2, aes(x = `GDP per capita`, y = `Life Expectancy`, size = `Population (M)`, fill = Country)) +
geom_point(alpha = 0.5, shape = 21, color = "black") +
geom_text(aes(label = Country), size = 8 / .pt) +
#ggrepel::geom_text_repel(aes(label = Country), size = 8 / .pt) +
scale_size(range = c(.1, 24), name="Population (M)") +
ylab("Life Expectancy") +
xlab("Gdp per capita") +
theme(axis.title = element_text(size=8), plot.title = element_text(hjust = 0.5))
但是关于人口的传说发生了变化。如何显示正确的人口图例并删除国家图例?
首先。如果您的数据集包含带字符串的列,则将所有内容都转换为数字不是一个好主意。第二。您需要 pivot_wider
而不是 pivot_longer
。第三。在非标准列名称周围使用反引号 ` 而不是引号:
library(ggplot2)
library(dplyr)
library(tidyr)
library(viridis)
bubble2 <- bubble %>%
pivot_wider(names_from = "Series", values_from = `2019`)
ggplot(bubble2, aes(x = `GDP per capita`, y = `Life Expectancy`, size = `Population (M)`, fill = Country)) +
geom_point(alpha = 0.5, shape = 21, color = "black") +
# scale_size(range = c(10, 1400), name = "Population (M)") +
scale_fill_viridis(discrete = TRUE, guide = "none", option = "A") +
ylab("Life Expectancy") +
xlab("Gdp per capita")
编辑:通过geom_text
添加标签:
ggplot(bubble2, aes(x = `GDP per capita`, y = `Life Expectancy`, size = `Population (M)`, fill = Country)) +
geom_point(alpha = 0.5, shape = 21, color = "black") +
geom_text(aes(label = Country), size = 8 / .pt) +
#ggrepel::geom_text_repel(aes(label = Country), size = 8 / .pt) +
scale_fill_viridis(discrete = TRUE, guide = "none", option = "A") +
ylab("Life Expectancy") +
xlab("Gdp per capita")
数据
bubble <- structure(list(Country = c(
"United Kingdom", "United Kingdom",
"United Kingdom", "United States", "United States", "United States"
), Series = c(
"GDP per capita", "Life Expectancy", "Population (M)",
"GDP per capita", "Life Expectancy", "Population (M)"
), `2019` = c(
42354,
81, 67, 65298, 78.8, 328
)), row.names = c(NA, -6L), class = c(
"tbl_df",
"tbl", "data.frame"
))
``
加上@stefan 的回答,尺寸范围太大了。另外添加,限制可能会有所帮助。
bubble %>% pivot_wider(names_from = "Series", values_from = `2019`) %>%
ggplot(aes(x=`GDP per capita`, y=`Life Expectancy`, size=`Population (M)`, fill=Country)) +
geom_point(alpha=0.5, shape=21, color="black") +
scale_size(range = c(10, 40), name="Population (M)") +
xlim(c(30000, 80000)) + ylim(c(77, 82)) +
scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
ylab("Life Expectancy") +
xlab("Gdp per capita") +
guides(size = FALSE)
您可以通过使用 reshape2::dcast
将数据重塑为宽幅格式,然后使用基础图形的另一种方法以更少的努力完成此操作。
library(reshape2)
plot(`Life Expectancy` ~ `GDP per capita`, dcast(bubble, Country ~ Series),
cex=`Population (M)`/120, ylim=c(78.5, 81.5), main='Here might be your title')
pseq <- seq(100, 300, 50)
legend('topright', legend=pseq, pch=1, pt.cex=pseq/120, title='Population (M)')
这些颜色实际上不是必需的,是吗?
数据:
bubble <- structure(list(Country = c("United Kingdom", "United Kingdom",
"United Kingdom", "United States", "United States", "United States"
), Series = c("GDP per capita", "Life Expectancy", "Population (M)",
"GDP per capita", "Life Expectancy", "Population (M)"), `2019` = c(42354,
81, 67, 65298, 78.8, 328)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))