【问题标题】:Creating bubble plot in R在 R 中创建气泡图
【发布时间】:2021-09-20 19:34:01
【问题描述】:

我在下面的 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))

但是关于人口的传说改变了。如何显示正确的人口图例并取出国家图例?

【问题讨论】:

  • 能否添加一个可重现的样本 dput(head(bubble) ? aes 参数应该是数据集中不带引号的列名。
  • 好的,添加了。

标签: r data-wrangling bubble-chart


【解决方案1】:

首先。如果您的数据集包含带有字符串的列,则将所有内容转换为数字并不是一个好主意。第二。您需要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"
))
``

【讨论】:

  • 如何才能为每个圈子指定国家?我在 fill=country 之后尝试了 label=country,但没有成功。
  • 添加标签,你必须添加一个文本层,例如尝试使用geom_text(aes(label = Country), size = 8 / .pt) 或使用ggrepel 包,您可以使用ggrepel::geom_text_repel(aes(label = Country), size = 8 / .pt)
  • 我使用了您的第一个选项,它可以在侧面创建一个图例。如何在图表上标记每个圆圈?谢谢。
  • 不确定你做了什么。但是这两个选项中的任何一个都会或应该将带有国家名称的标签添加到圆圈中。查看我的编辑。
  • (: 你可以通过 scale_size 的breaks参数设置图例的中断,例如scale_size(breaks = c(100, 200, 300, 500, 600, ....)
【解决方案2】:

您可以使用 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"))

【讨论】:

    【解决方案3】:

    除了@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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2018-06-28
      • 2023-03-12
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多