1

I am working with the library gapminder and I have generated the following graph to show the relationship between life expentancy, continent and gdp

library(gapminder)
library(dplyr)
library(ggplot2)
df <-gapminder

ggplot(db1 %>% filter(year==2007), aes(x=pop, y=gdpPercap, color=continent))+
  geom_point()+scale_size(df$lifeExp)

However, the resulting dots are all the same size and I don't know why. Can someone help me?

slow_learner
  • 337
  • 1
  • 2
  • 15

1 Answers1

1

You need to add the size aesthetic before you can manipulate it using scale_size(), e.g.

library(gapminder)
library(dplyr)
library(ggplot2)

gapminder %>%
  filter(year == 2007) %>%
  ggplot(aes(x = pop, y = gdpPercap, color = continent, size = lifeExp)) +
  geom_point() +
  scale_size(name = "Life Expectancy (years)")

You can't really see the different size dots, but it's clearer if you log transform the x axis:


gapminder %>%
  filter(year == 2007) %>%
  ggplot(aes(x = pop, y = gdpPercap, color = continent, size = lifeExp)) +
  geom_point() +
  scale_size(name = "Life Expectancy (years)") +
  scale_x_log10()

Created on 2022-11-10 by the reprex package (v2.0.1)

Does that answer your question, or have I misunderstood something?

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Thanks for the answer. Is there a way for me to remove the life expectancy legend? – slow_learner Nov 10 '22 at 10:06
  • You can substitute `guides(size = "none") +` for the line `scale_size(name = "Life Expectancy (years)") +` to remove the life expectancy legend completely – jared_mamrot Nov 10 '22 at 10:14