Home > database >  Problem using scale_size in R from library ggplot2
Problem using scale_size in R from library ggplot2

Time:11-10

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?

CodePudding user response:

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?

  • Related