library(tidyverse)
library(gridExtra)
library(nycflights13)
# 기존 제공하는 함수가 덮어졌을 경우 Class로 접근
median = function(x){x[0]}
median(c(1,2,3))
stats::median(c(1,2,3))
head(mpg)
str(mpg) # tibble format 이다. Rstudio에서는 화면에 크기게 맞게 데이터를 보여주고 각 컬럼의 Type의 알려준다.
options(repr.plot.width=5,repr.plot.height=3)
ggplot(data=mpg) +
geom_point(mapping = aes(x = displ, y =hwy))
##displ 엔진 사이즈, hwy 연비, 효율
ggplot(data=mpg) +
geom_point(mapping = aes(x = displ, y =hwy), color='pink')
ggplot(data=mpg) + geom_point(aes(x=hwy,y=cyl)) # 명목형이라 Scatter가 의미가 없는듯.
ggplot(data=mpg) + geom_point(aes(x=class,y=drv)) # 둘다 명목이라 의미가 없는 듯.
ggplot(data=mpg) + geom_point(aes(displ, hwy, color=class))
ggplot(data=mpg) +
geom_point(mapping = aes(x = displ, y =hwy, color=drv)) # 색
ggplot(data=mpg) +
geom_point(mapping = aes(x = displ, y =hwy, shape=class)) # 모양
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy),alpha = .2,size=3) # 투명도(20%) -> 5개가 겹쳐야 100%가 된다.
ggplot(data = mpg) +
geom_jitter(mapping = aes(x = displ, y = hwy),alpha = .2,size=3)
options(repr.plot.width = 10)
gg1 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue")) # color 가 aes안에 있다면 데이터에 대해서 조건이 들어가야됨.
## Answer
gg2 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
grid.arrange(gg1,gg2,ncol=2)
options(repr.plot.width = 4)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), shape=21,
color = "white",
fill='black', size=3,
stroke=2) # stroke 테두리 굵기
era <- function(year){
if(year < 2001){
return('pre')
}else if(year < 2004){
return('mid')
}else{
return('post')
}
}
era(2001)
purrr::map(mpg$year,era)[1:3] # List로 나온다.
purrr::map_chr(mpg$year,era)[1:4] # 어떤 데이터의 모든 원소에 함수를 적용. 문자 Vector로 변환하여 출력.
options(repr.plot.width = 6)
ggplot(data=mpg) +
geom_point(aes(displ,hwy,color=purrr::map_chr(year,era)))
map_dbl(c(1,2,3), function(x){x+1})
cut(mpg$year,c(1998,2001,2004,2009))
ggplot(data=mpg) +
geom_point(aes(displ,hwy,color=cut(year,c(1998,2001,2004,2009))))
## 최대 row = 2
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy)) + facet_wrap(~class,nrow=2) # Class별로 따로그려라
## 최대 Col = 2
options(repr.plot.height=6)
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy)) + facet_wrap(~class,ncol=2) # Class별로 따로그려라
options(repr.plot.height=4)
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy)) + facet_grid(~class) # 격자처럼 쪼개서 그린다. 가로를 쪼갠다
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy)) + facet_grid(class~.) # 격자처럼 쪼개서 그린다. 세로를 쪼갠다.
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy)) + facet_grid(class~drv) # 세로로는 Class, 가로로는 drv로 쪼갠다.
options(repr.plot.height=4)
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy, color = class))
ggplot(data=mpg) +
geom_point(aes(x=displ, y=hwy, color = class)) + facet_grid(~class)
options(repr.plot.height=3)
gg1 <- ggplot(data=mpg) +
geom_smooth(aes(displ,hwy)) +
geom_point(aes(displ,hwy,color=class))
gg2 <- ggplot(data=mpg) +
geom_smooth(aes(displ,hwy), se=F) +
geom_point(aes(displ,hwy,color=class))
grid.arrange(gg1,gg2)
ggplot(data=mpg) +
geom_smooth(aes(displ,hwy),method = 'lm',se=F)
ggplot(data=mpg, aes(displ,hwy)) + geom_smooth() + geom_point(aes(color=class))
ggplot(data=mpg, aes(displ,hwy,color=drv)) + geom_smooth(se=F) + geom_point()
ggplot(data=mpg, aes(displ,hwy)) +
geom_smooth(aes(.=drv),se=F) + # . 대신에 group=drv
geom_point() #선만 따로 그린다.
ggplot(data=mpg, aes(displ,hwy)) +
geom_smooth(aes(linetype=drv,color=drv), se=F) +
geom_point()
ggplot(data=mpg, aes(displ,hwy)) +
geom_point(aes(color=class)) +
geom_smooth(data=filter(mpg,class=='subcompact'), se=F)
options(repr.plot.height=6,repr.plot.width=8)
gg <- ggplot(data=mpg, aes(displ,hwy))
gg1 <- gg + geom_point(size=2) + geom_smooth(se=F)
gg2 <- gg + geom_point(size=2) + geom_smooth(aes(group=drv), se=F)
gg3 <- gg + geom_point(aes(color=drv)) + geom_smooth(aes(color=drv),se=F)
gg4 <- gg + geom_point(aes(color=drv)) + geom_smooth(se=F)
gg5 <- gg + geom_point(aes(color=drv)) + geom_smooth(aes(linetype=drv),se=F)
gg6 <- gg +
geom_point(aes(fill=drv),color="white",shape=21, size=2,
stroke=1)
grid.arrange(gg1,gg2,gg3,gg4,gg5,gg6)
options(repr.plot.height=3,repr.plot.width=5)
head(diamonds)
ggplot(diamonds) + geom_bar(aes(x=cut,fill=cut))
a <- data.frame(x=c(1,4,7), y=c(2,3,4)) # 열방식으로 들어간다.
tibble(x=c(1,2,3), y=c(2,3,4)) # 열형태로도 되고 행 형태로도 된다.
#tribble(
# ~x, ~y,
# 1,5,
# 2,3,
# 4,7
# ) # 왜 안되지? 낼 물어봐야지...
str(as_data_frame(a)) # tibble
str(as_tibble(a))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
options(repr.plot.height=5)
gg1 <- ggplot(data=diamonds) + geom_bar(aes(cut,fill=color), position='dodge')
gg2 <- ggplot(data=diamonds) + geom_bar(aes(cut,fill=color), position='fill') # 100%로 꽉채운 후 비율을 나타낸다.
grid.arrange(gg1,gg2)
options(repr.plot.height=3,repr.plot.width=9)
gg1 <- ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
gg2 <- ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
grid.arrange(gg1,gg2,ncol=2)
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
gg1 <- bar + coord_flip()
gg2 <- bar + coord_polar()
grid.arrange(gg1,gg2,ncol=2)
options(repr.plot.height=3)
ggplot(mpg) + geom_line(aes(hwy,displ))
jan = flights %>% filter(month == 1) %>%
group_by(day) %>% mutate(count = n())
ggplot(jan,aes(day,count)) + geom_line()
library(lubridate)
daily <- flights %>% group_by(month, day) %>% mutate(date=make_date(year,month,day), count=n())
ggplot(daily,aes(date,count)) + geom_line() + geom_smooth() # smooth를 사용하면 추세를 볼 수 있다.