Data Analysis/깨R지식
[R] 데이터 시각화: Bar Plot
[지현]
2020. 6. 29. 17:12
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
libraray(dplyr); library(ggplot2);library(forcats)
df = data%>%filter(categorical1=="aa")
temp = aggregate(df$numerical, by = list(df$categorical2), FUN = sum)
colnames(temp) = c('categorical2', 'numerical')
temp%>%
mutate(name = fct_reorder(categorical2, numerical)) %>%
ggplot( aes(x=categorical2, y=numerical)) +
labs(x="") +
geom_bar(stat="identity", fill="#68C8CB", alpha=.6, width=0.8) +
coord_flip() +
xlab("") +
theme_bw()
|
카테고리1에 속해있는 데이터에 한해, 카테고리 2별로 numerical을 function 넣어 보고 싶을 때 사용할 수 있다.
예를 들자면, 각 반 별로 어느 반이 성적 점수 합이 가장 높은 지 보고 싶다. (FUN = sum), 혹은,
각 반별로 어느 반이 성적 점수 평균이 높은지 보고싶다. ( FUN = mean) 등등.
이러면 복잡하게 나오지 않고, 간단한 bar plot으로 한 눈에 들어오며 기회서에 넣어도 눈에 잘 달라붙는다.
