본문 바로가기
R

(R) Ways to make Bar graph (continuous variable --> Discrete variable)/qplot, ggplot,barplot

by jangpiano 2020. 8. 11.
반응형

 continuous variable _x 

 discrete variable_x

 

 



<Three ways to make a Bar graph>


> table(mpg$cyl)

 4  5  6  8 

81  4 79 70 


>barplot(table(mpg$cyl))



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



> qplot(mpg$cyl,binwidth=0.5)




When the variable x is a numeric value, the variable is regarded as continuous variable  using ggplot()-geom_bar(),geom_col()  and the graph becomes containing the vacant element. (ex) cyl=7 above 

You can make the variable discrete by using factor(). 


<Make a Discrete bar graph with an originally continuous variable>


Instead of having a blank in the bar graphs above, we can delete the case when x=7 by making the variable mpg$cyl be discrete. There are three ways to make a bar graph of a discrete variable from a continuous variable. 



> qplot(factor(mpg$cyl))



> ggplot(mpg,aes(x=factor(cyl)))+geom_bar()




> barplot(table(mpg$cyl))






반응형