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

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

Bubble Plot

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)