본문 바로가기
R

(R) list colors of columns in order / character type to factor type

by jangpiano 2020. 7. 30.
반응형

list colors of columns in order. 

*character type to factor type* 

>library(ggplot2)
>library(dplyr)

> Ozone_temp_rate<-airquality_%>%count(Ozone_rank,temp_rank)%>%group_by(Ozone_rank)%>%mutate(per=n/sum(n)*100)

> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_col()

> class(Ozone_temp_rate$temp_rank)
[1] "character"

> levels(Ozone_temp_rate$temp_rank) ------------------no levels because the variable 'temp_rank' is a 'character' type.
NULL

> Ozone_temp_rate$temp_order<-factor(Ozone_temp_rate$temp_rank, level=c("warm","hot","very hot")
      # switch the variable 'temp_rank' to the 'factor' type.


> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_order))+geom_col()

> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_order))+geom_col()+scale_x_discrete(limits=c("much","middle","little"))

[R] - (R) Reorder columns in any orders / scale_x_discrete()


반응형