Home > OS >  Problem using ggplot 2 in R to display country data vs gdp
Problem using ggplot 2 in R to display country data vs gdp

Time:11-11

I am working with the library gapminder as follows:

library(gapminder)
library(dplyr)
db1 <-gapminder
db1Asia<-db1 %>% filter(year==1962 | year==1967) %>% filter(country=="China" | country=="India")
ggplot(data=db1Asia %>% filter(year==1962), aes(x=country, y=pop, color=country))  geom_bar()

to study the evolution of the population of the two nations between 1962 and 1967. However, I get the following error:

Error in `f()`:
! stat_count() can only have an x or y aesthetic.

I am following this tutorial and I cannot see what I am doing wrong. Can someone please help me?

CodePudding user response:

Quinten's comment works

library(gapminder)
library(dplyr)
db1 <-gapminder
db1Asia<-db1 %>% filter(year==1962 | year==1967) %>% filter(country=="China" | country=="India")
ggplot(data=db1Asia %>% filter(year==1962), aes(x=country, y=pop, color=country))  geom_col()

CodePudding user response:

I think you must add geom_bar(stat = 'identity'):

    library(gapminder)
library(dplyr)
db1 <-gapminder
db1Asia<-db1 %>% filter(year==1962 | year==1967) %>% filter(country=="China" | country=="India")
ggplot(data=db1Asia %>% filter(year==1962), aes(x=country, y=pop, color=country))   geom_bar(stat = 'identity')
  • Related