4.facet_wrap(~varname)将图形按var类别分割
ggplot展示summarize的数据
5.expend_limits()tomakesuretheplot ’syaxisincludeszero
library(gapminder)
library(dplyr)
library(ggplot2)
by_year_continent<gapminder%>%
group_by(continent,year)%>%
summarize(medianGdpPercap=median(gdpPercap))
ggplot(by_year_continent,aes(x=year,y=medianGdpPercap,color=continent))+
geom_point()+expand_limits(y=0)
6.barplotx为分类变量
library(gapminder)
library(dplyr)
library(ggplot2)
by_continent<gapminder%>%
filter(year==1952)%>%
group_by(continent)%>%
summarize(medianGdpPercap=median(gdpPercap))
ggplot(by_continent,aes(x=continent,y=medianGdpPercap))+geom_col()
7.histogramx为连续型变量
library(gapminder)
library(dplyr)
library(ggplot2)
gapminder_1952<gapminder%>%
filter(year==1952)
Createahistogramofpopulation(pop)
ggplot(gapminder_1952,aes(x=pop))+geom_histogram()+scale_x_log10()
8.boxplot同barplot
function
1.function运行首先找function中的元素,如果环境中没有,则找环境中的元素
2.[和[[不同:Thatis,my_list[[1]]extractsthefirstelementofthelistmy_list,and
my_list[[“name”]]extractstheelementinmy_listthatiscalledname.Ifthelistisnestedyou
cantraveldownthehierarchybyrecursivesubsetting.Forexample,mylist[[1]][[“name”]]is
theelementcallednameinsidethefirstelementofmy_list.