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()
'R' 카테고리의 다른 글
(R) Ways to make Bar graph (continuous variable --> Discrete variable)/qplot, ggplot,barplot (0) | 2020.08.11 |
---|---|
(R) two lines in a graph, lines and points in a graph. (0) | 2020.08.10 |
(R) Reorder columns in any orders / scale_x_discrete() (0) | 2020.07.30 |
(R) coord_flip() (0) | 2020.07.30 |
(R) count the number of elements/ count() (0) | 2020.07.29 |