R 그래픽 - ggplot2패키지
R 그래픽 - ggplot2패키지
참고: https://statkclee.github.io/R-ecology-lesson/kr/05-visualization-ggplot2.html
http://ggplot2.tidyverse.org/reference/
http://www.cookbook-r.com/Graphs/
1. Bar Graph
1) bar graph of identity
# 텍스트
dat <- data.frame(time = factor(c("Lunch","Dinner"),
levels = c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
dat
ggplot(data=dat, aes(x=time,y=total_bill,fill=time)) +
geom_bar(colour="black", stat="identity", width=0.8, fill = "#DD8888") +
guides(fill=FALSE) + xlab("time of the day") + ylab("total_bill") +
ggtitle("Average bill for 2 people")
- stat = "identity" 통계량 설정
- width = 0.8 너비 조정
- fill = " " 안쪽 색상
- colour = " " 바깥쪽 색상, 선 색상
- guides(fill= F/T) 범례 추가 or not
- xlab, ylab, ggtitle, colour 등 모두 문자를 사용할 때는 " " 쓰기
2) bar graph of counts
# 텍스트
data(tips)
ggplot(data=tips, aes(x=day)) +
geom_bar(stat="count")
3) 변수 많을 때
# 텍스트
dat1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
dat1
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +
geom_bar(stat="identity", position=position_dodge(), colour="black") +
scale_fill_manual(values=c("#999999", "#E69F00"))
- fill = sex # 색깔 구분 기준
- position=position_dodge() # beside= TRUE 랑 같은 옵션
없으면 stacked 형식으로 그려짐
- scale_fill_manual # 나만의 색상 선택/만들기
2. Line Graph
1) 변수 하나
# 텍스트
ggplot(data=dat, aes(x=time, y=total_bill, group=1))+
geom_line(colour="red", linetype="dashed", size=1.5) +
geom_point(colour="red", size=4, shape=21, fill="white") +
xlab("time of the day") + ylab("Total Bill") +
ggtitle("Average bill for 2 people")
- group = 1 # 모든 점 연결
- linetype, size, shape # 모양 설정
# y축 시작/한계 값 변경
expand_limits(y=0)
2) 변수 많을 때
# Basic line graph with points ggplot(data=dat1, aes(x=time, y=total_bill, group=sex)) + geom_line() + geom_point() # Map sex to color ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, colour=sex)) + geom_line() + geom_point() # Map sex to different point shape, and use larger points ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line() + geom_point() # Use thicker lines and larger points, and hollow white-filled points ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line(size=1.5) + geom_point(size=3, fill="white") + scale_shape_manual(values=c(22,21))
3. Bar Graph / Line Graph 완성본
# A bar graph ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(colour="black", stat="identity", position=position_dodge(), size=.3) + # Thinner lines scale_fill_hue(name="Sex of payer") + # Set legend title xlab("Time of day") + ylab("Total bill") + # Set axis labels ggtitle("Average bill for 2 people") + # Set title theme_bw()
xlab("Time of day") + ylab("Total bill") + ggtitle("Average bill for 2 people") + theme_bw() + theme(legend.position=c(.7, .4)) # Position legend inside # This must go after theme_bw
4.
G <- ggplot(data=k, aes(x=School, y=Grade, fill=Sex))+
geom_bar(stat="identity", position="dodge", color="black") +
geom_point(size = 3, fill= "white") +
guides(fill = guide_legend(reverse=FALSE) ) +
theme(legend.position = "top",
legend.title = element_text(color="blue", size = 15, face = "bold"))+
scale_fill_brewer(palette="Spectral")
10. Cleveland Dot plot
#텍스트
data(tophitters2001)
to <- tophitters2001[1:25 , ]
To <- to[, c("name", "lg", "avg")]
To1 <- arrange(To, lg, name, avg)
nameorder <- To1$name[order(To1$lg, To1$avg)]
To1$name <- factor(To1$name, levels = nameorder)
GP <-
ggplot(To1, aes(x=avg, y=name)) +
geom_point(size = 3, aes(color=lg) ) +
geom_segment( aes(yend=name), xend=0, color="grey50" ) +
scale_color_brewer(palette="Set1", limits = c("NL","AL")) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(color="grey", linetype=2),
legend.position = c(1, 0.55),
legend.justification = c(1, 0.5) )
# reorder( ) 함수로는 한 가지 변수의 요인 수준밖에 정렬이 안되기 때문에,
변수 2개에 담겨 있는 요인 수준을 가지고 정렬하는 경우에는 직접
데이터를 재가공해야 한다. (중간 nameorder 과정)
- lg, avg로 정렬한 이름을 가져오고
- name을 요인으로 변환하여 nameorder순으로 수준을 정렬함
댓글
댓글 쓰기