반응형
Reorder Columns
<How to make columns in R>
1. make summary (using dplyr)
2. decide x-axis, y-axis
3. draw columns (using ggplot2)
<How to make reordered columns in R>
During step3, you should use reorder() function in x-axis
<How to make columns in R>
1. Make Summary
> library(ggplot2)
> library(dplyr)
> drv_cty<-mpg%>%group_by(drv)%>%summarise(mean_cty=mean(cty))
> drv_cty
# A tibble: 3 x 2
drv mean_cty
<chr> <dbll>
1 4 14.3
2 f 20.0
3 r 14.1
2. Decide X,Y axis
x-axis: drv
Y-axis: mean_cty
3. Draws columns
> ggplot(data=drv_cty, aes(x=drv, y=mean_cty))+geom_col()
<How to make reordered columns in R>STEP 1,2 is same with <How to make columns in R>3. Draw Reordered Columns1. Reorder X-axis in ascending order> ggplot(data=drv_cty,aes(x=reorder(drv,mean_cty),y=mean_cty))+geom_col()
2. Reorder X-axis in descending order
> ggplot(data=drv_cty,aes(x=reorder(drv,-mean_cty),y=mean_cty))+geom_col()
반응형
'R' 카테고리의 다른 글
(R) ifelse() (0) | 2020.07.23 |
---|---|
(R) make a summary - group_by(),summarise() (0) | 2020.07.22 |
(R) geom_col() VS geom_bar() (0) | 2020.07.20 |
Interpret data frame/ summary(), typeof(), str(), class(),View(),names(),ncol(),length(),nrow(),dim() (0) | 2020.07.16 |
(R) data.frame function / Way to make data frame 데이터 프레임 만들기 R (0) | 2020.07.16 |