library(gapminder)
library(dplyr)
##
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(gapminder)
library(dplyr)
library(ggplot2)
gapminder
## # A tibble: 1,704 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # … with 1,694 more rows
gapminder_1957<- gapminder %>%
filter(year==1957)
gapminder_1957
## # A tibble: 142 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1957 30.3 9240934 821.
## 2 Albania Europe 1957 59.3 1476505 1942.
## 3 Algeria Africa 1957 45.7 10270856 3014.
## 4 Angola Africa 1957 32.0 4561361 3828.
## 5 Argentina Americas 1957 64.4 19610538 6857.
## 6 Australia Oceania 1957 70.3 9712569 10950.
## 7 Austria Europe 1957 67.5 6965860 8843.
## 8 Bahrain Asia 1957 53.8 138655 11636.
## 9 Bangladesh Asia 1957 39.3 51365468 662.
## 10 Belgium Europe 1957 69.2 8989111 9715.
## # … with 132 more rows
ggplot(gapminder_1957,aes(x=pop,y=lifeExp))+geom_point()
ggplot(gapminder_1957,aes(x=pop,y=gdpPercap))+geom_point()
ggplot(gapminder_1957,aes(x=gdpPercap,y=lifeExp))+geom_point()
ggplot(gapminder_1957,aes(x=gdpPercap,y=lifeExp))+geom_point()+scale_x_log10()
ggplot(gapminder,aes(x=pop,y=lifeExp, colour=continent,size=gdpPercap))+
geom_point()+scale_x_log10()+scale_y_log10()+facet_wrap(~year)
ggplot(gapminder,aes(x=pop,y=lifeExp, colour=continent))+
geom_point()+scale_x_log10()+scale_y_log10()
ggplot(gapminder,aes(x=pop,y=lifeExp, colour=continent,size=gdpPercap))+
geom_point()+scale_x_log10()+scale_y_log10()
ggplot(gapminder,aes(x=pop,y=lifeExp))+
geom_point()+scale_x_log10()+scale_y_log10()+facet_wrap(~continent)
ggplot(gapminder,aes(x=gdpPercap,y=lifeExp, colour=continent,size=pop))+
geom_point()+scale_x_log10()+scale_y_log10()+facet_wrap(~year)
gapminder by_year<- gapminder %>% group_by(year) %>% summarise(MedianLifexp=median(lifeExp)) by_year ## 5.2: Create a scatter plot showing the change in medianLifeExp over time
gapminder
## # A tibble: 1,704 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # … with 1,694 more rows
by_year<- gapminder %>%
group_by(year) %>%
summarise(MedianLifexp=median(lifeExp))
ggplot(by_year,aes(x=year,y=MedianLifexp))+geom_point()+expand_limits(y=0)
## 5.3: Summarize medianGdpPercap within each continent within each year: ## ggplot(by_year,aes(x=year,y=MedianLifexp))+geom_point()+expand_limits(y=0)
by_year_continent <- gapminder %>% group_by(year, continent) %>% summarize(medianGdpPercap = median(gdpPercap))
by_year_continent
by_year_continent <- gapminder %>%
group_by(year, continent) %>%
summarize(medianGdpPercap = median(gdpPercap))
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
ggplot(by_year_continent,aes(x=year,y=medianGdpPercap,colour=continent))+geom_point()
by_continent_2007 <- gapminder %>% filter(year == 2007) %>% group_by(continent) %>% summarize(medianLifeExp = median(lifeExp), medianGdpPercap = median(gdpPercap))
by_continent_2007 <- gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarize(medianLifeExp = median(lifeExp),
medianGdpPercap = median(gdpPercap))
ggplot(by_continent_2007,aes(x=medianLifeExp,y=medianGdpPercap,colour=continent))+geom_point()
by_year <- gapminder %>% group_by(year) %>% summarize(medianGdpPercap = median(gdpPercap))
by_year
by_year <- gapminder %>%
group_by(year) %>%
summarize(medianGdpPercap = median(gdpPercap))
ggplot(by_year,aes(x=year,y=medianGdpPercap))+geom_line()+expand_limits(y=0)
by_year_continent <- gapminder %>% group_by(year, continent) %>% summarize(medianGdpPercap = median(gdpPercap))
by_year_continent <- gapminder %>%
group_by(year, continent) %>%
summarize(medianGdpPercap = median(gdpPercap))
## `summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
ggplot(by_year_continent,aes(x=year,y=medianGdpPercap, color=continent))+geom_line()+expand_limits(y=0)
by_continent <- gapminder %>% filter(year == 1957) %>% group_by(continent) %>% summarize(medianGdpPercap = median(gdpPercap))
by_continent <- gapminder %>%
filter(year == 1957) %>%
group_by(continent) %>%
summarize(medianGdpPercap = median(gdpPercap))
ggplot(by_continent,aes(x=continent,y=medianGdpPercap))+geom_col()
oceania_1957 <- gapminder %>% filter(continent == “Oceania”, year == 1957)
oceania_1957 <- gapminder %>%
filter(continent == "Oceania", year == 1957)
ggplot(oceania_1957,aes(x=country,y=gdpPercap))+geom_col()
oceania_1957
gapminder_1957<- gapminder %>% filter(year==1957) %>% mutate(pop_by_mil=pop/1000000)
gapminder_1957<- gapminder %>%
filter(year==1957) %>%
mutate(pop_by_mil=pop/1000000)
ggplot(gapminder_1957,aes(x=pop_by_mil))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
gapminder_1957 <- gapminder %>% filter(year==1957)
gapminder_1957 <- gapminder %>%
filter(year==1957)
ggplot(gapminder_1957,aes(x=pop))+geom_histogram()+scale_x_log10()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
gapminder_1957 <- gapminder %>%
filter(year == 1957)
gapminder_1957
## # A tibble: 142 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1957 30.3 9240934 821.
## 2 Albania Europe 1957 59.3 1476505 1942.
## 3 Algeria Africa 1957 45.7 10270856 3014.
## 4 Angola Africa 1957 32.0 4561361 3828.
## 5 Argentina Americas 1957 64.4 19610538 6857.
## 6 Australia Oceania 1957 70.3 9712569 10950.
## 7 Austria Europe 1957 67.5 6965860 8843.
## 8 Bahrain Asia 1957 53.8 138655 11636.
## 9 Bangladesh Asia 1957 39.3 51365468 662.
## 10 Belgium Europe 1957 69.2 8989111 9715.
## # … with 132 more rows
ggplot(gapminder_1957,aes(x=gdpPercap,y=lifeExp))+geom_point()+
scale_x_log10()+
scale_y_log10()
ggplot(gapminder_1957,aes(x=continent,y=gdpPercap))+geom_boxplot()
ggplot(gapminder_1957,aes(x=continent,y=gdpPercap))+geom_boxplot()+
scale_y_log10()+
ggtitle("Comparing GDP per capita across continents")