library(ggplot2) ## 3.13 ggplot(mtcars,aes(x=mpg))+ geom_histogram(bins=10,fill="blue")+ theme_classic() ggplot(mtcars,aes(x=cyl))+ geom_bar()+ theme_classic() ggplot(mtcars,aes(x=disp,y=mpg))+ geom_point()+ theme_classic() ## 3.14 prob <- data.frame(label=c(rep("red",3),rep("blue",2),rep("green",4),rep("black",1))) ggplot(prob,aes(x=as.factor(label)))+ geom_bar() prob <- data.frame(label=c("red","blue","green","black")) ggplot(prob,aes(x=as.factor(label),weights=c(0.3,0.2,0.4,0.1)))+ geom_bar() ## 3.15 X <- seq(-2*pi,2*pi,by=0.1) n <- length(X) D <- data.frame(x=rep(X,2), type=c(rep("sin",n),rep("cos",n)),f=c(sin(X),cos(X))) p1 <- ggplot(D,aes(x=x,y=f,colour=type))+ geom_line()+ geom_hline(yintercept=-1,size=2)+ geom_hline(yintercept=1,size=2)+ labs(y="",x="x",title="Trigonométrie",color="Fonctions")+ theme_classic()+ theme(plot.title=element_text(hjust=0.5,face="bold"),legend.position="top") p1 ggplot(D,aes(x=x,y=f,colour=type))+ geom_line()+ geom_hline(yintercept=-1,size=2)+ geom_hline(yintercept=1,size=2)+ theme_classic()+ theme(legend.position="none")+ facet_wrap(~type) ## 3.16 n <- 100 x <- runif(n) y <- 3+x+rnorm(n,sd=0.2) D <- data.frame(x=x,y=y) model.reg <- lm(y~x,data=D) ggplot(D,aes(x=x,y=y))+ geom_point()+ geom_abline(intercept=model.reg$coefficients[1],slope=model.reg$coefficients[2])+ geom_abline(intercept=3,slope=1)+ theme_classic() ggplot(D,aes(x=x,y=y))+ geom_point()+ geom_smooth(method="lm")+ theme_classic() D <- data.frame(x=x,y=y,yhat=model.reg$fitted.values) ggplot(D,aes(x=x,y=y))+ geom_point()+ geom_segment(data=D,aes(x=x,y=y,xend=x,yend=yhat))+ geom_abline(intercept=model.reg$coefficients[1],slope=model.reg$coefficients[2])+ theme_classic() ## 3.17 data(state) states <- data.frame(state.x77,state.name=rownames(state.x77), state.region=state.region) # 1 q1 <- quantile(states$Income,1/3,names=FALSE) q2 <- quantile(states$Income,2/3,names=FALSE) states$revenu1 <- cut(states$Income,breaks=c(min(states$Income),q1,q2,max(states$Income)),labels=c("faible","moyen","fort"),include.lowest=TRUE) summary(states$revenu1) # 2 ggplot(states,aes(x=Population,y=Murder))+ geom_point()+ facet_wrap(~revenu1)+ theme_classic() # 3 ggplot(states,aes(x=Population,y=Murder))+ geom_point()+ geom_smooth(method="lm",se=FALSE)+ geom_point(aes(colour=state.region))+ facet_wrap(~revenu1)+ theme_classic() ## 3.18 # 1 library(mlbench) data(Ozone) help(Ozone) # 2 Ozone$date <- as.Date(paste(Ozone$V1,Ozone$V2,"76",sep="/"),"%m/%d/%y") # 3 ggplot(Ozone,aes(x=date,y=V4))+ geom_line()+ labs(y=expression(paste("Concentration en ",O[3],sep="")),x="Date")+ theme_classic() # 4 ggplot(Ozone,aes(x=V4,y=V8))+ geom_point()+ labs(x=expression(paste("Concentration en ",O[3],sep="")),y="Temperature")+ theme_classic() # 5 Ozone$month <- as.factor(months(Ozone$date, abbreviate = TRUE)) # 6 levels(Ozone$month) <- c("jan","fév","mar","avr","mai","jui","jul","aoû","sep","oct","nov","déc") ggplot(Ozone,aes(x=month,y=V4))+ geom_boxplot()+ labs(y=expression(paste("Concentration en ",O[3],sep="")),x="Mois")+ theme_classic() # 7 q1 <- quantile(Ozone$V6,1/3,names=FALSE) q2 <- quantile(Ozone$V6,2/3,names=FALSE) Ozone$vent <- cut(Ozone$V6,breaks=c(min(Ozone$V6),q1,q2,max(Ozone$V6)),labels=c("faible","moyen","fort"),include.lowest=TRUE) summary(Ozone$vent) # 8 ggplot(Ozone,aes(x=V8,y=V4,colour=vent))+ geom_point()+ labs(y=expression(paste("Concentration en ",O[3],sep="")),x="Temperature")+ facet_wrap(~month)+ theme_classic() library(gganimate) library(gapminder) p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + scale_colour_manual(values = country_colors) + scale_size(range = c(2, 12)) + scale_x_log10() + facet_wrap(~continent) + labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + transition_time(year) + ease_aes('linear') animate(p, duration = 5, fps = 20, width = 200, height = 200, renderer = gifski_renderer()) anim_save("test.gif")