-
ggplot() 함수로 산점도 작성하기R 2017. 10. 21. 12:55
ggplot(data = diamonds, aes(x = carat, y = price)) +
geom_point(aes(col = cut), size = 2) +
geom_smooth(method = "lm", col = "red", size = 1) +
coord_cartesian(x = c(0, 6), y = c(0, 25000)) +
labs(title = "Scatter Plot",
subtitle = "Carat and Price",
x = "Carat",
y = "Price",
caption = "diamonds data") +
scale_color_brewer(palette = "Set1") +
facet_grid( ~ cut)# 1. 레이어 준비 : x축에 carat, y축에 price 배치
ggplot(data = diamonds, aes(x = carat, y = price))# 2. 레이어 위에 산점도 작성
geom_point()
geom_point(aes(col = cut), size = 2) # 점의 색깔을 cut별로 지정하고, 점의 크기는 2로 설정함# 3. 산점도 위에 회귀직선을 표현하기
geom_smooth(method = "lm", col = "red", size = 1) # 회귀선의 색을 red, 선의 굵기를 1로 지정# 4. x축눈금과 y축눈금을 조정하기
coord_cartesian(x = c(0, 6), y = c(0, 25000)) # x축눈금의 최소값을 0, 최대값을 6으로 지정하고, y축눈금의 최소값을 0, 최대값을 25000으로 지정# 5. 제목 / 부제목 / x축제목 / y축제목 / 캡션 달기
labs(title = "Scatter Plot",
subtitle = "Carat and Price",
x = "Carat",
y = "Price",
caption = "diamonds data")# 6. 전체적인 색을 palette = "Set1"를 지정
scale_color_brewer(palette = "Set1")# 7. 집단별로 산점도 작성하기
facet_grid( ~ cut) # cut별로 산점도 작성하기'R' 카테고리의 다른 글
Marginal Histogram / Boxplot (0) 2017.10.22 ggplot2 패키지로 산점도 작성하기 (0) 2017.10.22 R데이터를 엑셀(excel) 데이터로 저장하기 (0) 2017.09.25 RStudio에서 할당 연산자인 <-의 단축키 (0) 2017.09.20 Resources on R and Data Mining Free Datasets (0) 2017.09.20