- Converta quadros de dados para gráficos usando ggplot2
ggplot()
+
' cada layer/atributo do gráficogeom
spread
ou gather
para definir uma unidade/linha apropriadageom()
geom(aes())
flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay))
flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay), color="blue")
flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay, color=origin))
flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay, shape=origin))
x
, y
, group
color
, fill
, alpha
shape
, size
See https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf
x
geom_density
geom_histogram
geom_bar
(com o count de dados discretos)x
flights %>% ggplot() + geom_histogram(aes(x=dep_time))
x
e y
geom_point
(scatterplot)geom_col
(um discreto, um contÃinuo)x
e y
flights %>% group_by(origin) %>% summarize(avg_delay=mean(dep_delay,na.rm=TRUE)) %>% ggplot() + geom_col(aes(x=origin,y=avg_delay))
x
e y
flights %>% filter(carrier %in% c("UA","AA","DL")) %>% group_by(origin,carrier) %>% summarize(total_delay=sum(dep_delay,na.rm=TRUE)) %>% ggplot() + geom_col(aes(x=origin,y=total_delay,fill=carrier), position="fill")
x
e y
e mais umageom_line
(define color
)geom_text
(define label
)geom_tile
(define fill
)x
e y
e mais umaflights %>% ggplot() + geom_text(aes(x=dep_time,y=distance,label=dest))
x
e y
e mais umaflights %>% group_by(origin,month) %>% summarize(avg_delay=mean(dep_delay,na.rm=TRUE)) %>% ggplot() + geom_line(aes(x=month,y=avg_delay,color=origin))
x
e y
e mais umaflights %>% group_by(origin,hour) %>% summarize(flights=n()) %>% ggplot() + geom_tile(aes(x=hour,y=origin,fill=flights))
geom_...
facet_grid
com o variável que muda entre gráficosflights %>% group_by(origin,month) %>% summarize(avg_delay=mean(dep_delay,na.rm=TRUE)) %>% ggplot() + geom_line(aes(x=month,y=avg_delay)) + facet_grid(.~origin)
flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay)) + geom_smooth(aes(x=dep_time,y=dep_delay), method="lm")
flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay)) + geom_smooth(aes(x=dep_time,y=dep_delay), method="loess")
flights %>% zelig(dep_delay~dep_time + day + minute, data=., model="ls") %>% from_zelig_model() %>% summary() %>% coefficients() %>% as.data.frame() %>% rownames_to_column() %>% as.tibble(rownames=NA) %>% filter(rowname!="(Intercept)") %>% mutate(Conf.lo=Estimate-1.96*`Std. Error`,Conf.hi=Estimate+1.96*`Std. Error`) %>% ggplot() + geom_point(aes(x=rowname,y=Estimate)) + geom_errorbar(aes(x=rowname,ymin=Conf.lo,ymax=Conf.hi)) + geom_hline(yintercept=0,color="red",lty=2)
Tipo de dados | Color (point, line) | Fill (area) |
---|---|---|
Continuo | scale_color_gradient(low="color1",high="color2") | scale_fill_gradient(low="color1",high="color2") |
Discreto | scale_color_brewer(palette="pre-definido") | scale_fill_brewer(palette="pre-definido") |
flights %>% group_by(origin,month) %>% summarize(avg_delay=mean(dep_delay,na.rm=TRUE)) %>% ggplot() + geom_line(aes(x=month,y=avg_delay,color=origin)) + scale_color_brewer(palette="Dark2")
flights %>% ggplot() + geom_text(aes(x=dep_time,y=arr_time,label=dest)) + scale_color_gradient(low="yellow",high="red")
+ theme_classic()
- remova as linhas de grade e o fundo+ xlab()
, + ylab()
- labelas para os eixos+ ggtitle()
- o tÃtuloflights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay, color=origin)) + theme_classic() + xlab("Departure Time") + ylab("Departure Delay") + ggtitle("Delays by Departure Time and Airport Origin")
library(plotly) plot <- flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay, color=origin)) + theme_classic() plot %>% ggplotly()
library(plotly) plot <- flights %>% ggplot() + geom_point(aes(x=dep_time,y=dep_delay, color=origin, frame=month)) + theme_classic() %>% plot %>% ggplotly()