본문 바로가기
R

(R) geom_col() VS geom_bar()

by jangpiano 2020. 7. 20.
반응형

This posting is about the difference between geom_col() and geom_bar() which look similar. 


geom_col() 

 geom_bar()

need both x-axis and y-axis designated

 only need x-axis or y-axis designated

 need summary about x and y 

only need raw material



<geom_col()>


<EXAMPLE1>


> library(ggplot2)

> library(dplyr)

> mpg2<-mpg%>%group_by(manufacturer)%>%summarise(mean_hwy=mean(hwy))        # make summarise 

> ggplot(data=mpg2,aes(x=manufacturer,y=mean_hwy))+geom_col()             # x:manufacturer y:mean_hwy  



1. designate two variables (manufacturer, mean_hwy)

2. group_by(manufacturer) -------separating data by a variable (manufacturer) 

3. summarise(mean_hwy=mean(hwy))-------calculating mean of another variable (hwy) for each manufacturer 

4. mpg2------- summary is successfully made 

5. ggplot(data=mpg2, aes(x=manufacturer, y=mean_hwy))+geom_col()---------make geom_col() graph <using ggplot2> 




x: manufacturer        y: mean_hwy  


<EXAMPLE2>


>library(ggplot2)

>library(dplyr)

> midwest2<-midwest%>%group_by(state)%>%summarise(mean_popdensity=mean(popdensity))

> ggplot(data=midwest2, aes(x=state,y=mean_popdensity))+geom_col()


1. designate two variables (state, mean_popdensity)

2. group_by(state) -------separating data by a variable (state) 

3. summarise(mean_popdensity=mean(popdensity))-------calculating mean of another variable (popdensity) for each state.

4. midwest2------ summary is successfully made 

5. ggplot(data=midwest2, aes(x=state, y=mean_popdensity))+geom_col()---------make geom_col() graph <using ggplot2> 


X: state                   Y: mean_popdensity




<geom_bar()>


<EXAMPLE1>


>library(ggplot2)

>ggplot(data=mpg,aes(x=manufacturer))+geom_bar()


no need to make a summary. (no need 'dplyr' package)

we need only x or y-axis. (only designate x in aes())

no relationship (variable: only x)



x: manufacturer              y: frequencies of each 'manufacturer' in 'mpg'



It looks same with qplot(mpg$manufacturer)


>library(ggplot2)

> qplot(mpg$manufacturer)


x: mpg$manufacturer           y: frequencies of each 'manufacturer' in 'mpg'



<EXAMPLE2>


>library(ggplot2) 

> ggplot(data=midwest,aes(x=state))+geom_bar()



It looks same with plot(midwest$state)

> qplot(midwest$state)





반응형