반응형
-
세로 막대 차트
1
2
3
4
|
ggplot(df, aes(x = month, y=rain)) + # aes 함수는 ggplot() 안에 써도, 또는 geom_bar()안에 써도 상관 없음
geom_bar(stat = "identity", #데이터셋 안에 포함되지 않은 변수를 쓸때는 stat = “bin”을 입력
width = 0.7,
fill = "steelblue")
|
-
가로 막대 차트
1
2
3
4
5
6
7
8
9
10
|
ggplot(df, aes(x = month, y = rain)) +
geom_bar(stat = "identity",
width = 0.7,
fill = "steelblue") +
ggtitle ("월별 강수량") + # 차트 제목
theme(plot.title = element_text(size = 25,
face = "bold",
color = 'red')) +
labs(x = '월', y = '강수량') +
coord_flip() #가로-세로 flip
|

-
히스토그램 Histogram
1
2
3
4
|
ggplot(iris, aes(x = Sepal.Width, fill = Species, # fill은 막대를 채우는 색
color = Species)) + #color는 경계선의 색
geom_histogram(binwidth = 0.1, position = "dodge") + # dodge : 막대 이어 붙이기
theme(legend.position = 'bottom') # legend.position :범례 위치
|
-
산점도 Scatter Chart
1
2
3
4
5
6
7
8
9
|
ggplot ( data = iris, mapping = aes (x = Petal.Length,
y = Petal.Width,
color = Species,
shape = Species )) +
geom_point(size = 3) +
ggtitle("꽃잎의 길이와 폭") +
theme(plot.title = element_text (size = 25,
face = 'bold',
color = rgb( 128,0,128,maxColorValue = 255)))
|
-
상자 그래프 Box Plot
1234567ggplot (data = iris, mapping = aes(y=Petal.Length)) +geom_boxplot(fill = 'navy',color = 'red')ggplot (data = iris, mapping = aes(y=Petal.Length,fill = Species)) +geom_boxplot()
-
선 그래프
1
2
3
4
5
|
year <- 1937:1960
cnt <- as.vector(airmiles)
df <- data.frame(year, cnt)
ggplot(df, aes(x = year, y = cnt)) +
geom_line(col = "navy")
|
+ 사선 & 평행선
1
2
3
4
5
|
ggplot(economics, aes(x = date, y = psavert)) +
geom_line() +
geom_abline(intercept = 12.18671, # 사선, intercept : y 절편값
slope = -0.0005333) + # slope : 기울기
geom_hline(yintercept = mean(economics$psavert)) # 평행선
|
+ 수직선
1
2
3
|
ggplot(economics, aes(x = date, y=psavert)) +
geom_line() +
geom_vline(xintercept = x_inter) # 수직선 vertical line
|
+ 텍스트 추가 (산점도 배경)
1
2
3
|
ggplot(economics, aes(x = date, y=psavert)) +
geom_line() +
geom_vline(xintercept = x_inter) # 수직선 vertical line
|
+ 강조 영역 및 화살표 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
annotate('rect',
xmin = 3, # 좌표 지정
xmax = 4,
ymin = 12,
ymax = 21,
alpha = 0.5, #투명도 지정
fill = 'red') +
annotate('segment', x= 2.5, xend = 3.7, # 화살표 넣어 강조하기
y = 10, yend=17, color = 'blue',
arrow = arrow(60) ) + # 화살표 각도
annotate('text', x= 2.5, y = 10, # 화살표 이름
label = 'point')
|
반응형
'빅데이터 > R' 카테고리의 다른 글
[R] treemap() 함수 사용 예시 (0) | 2019.12.10 |
---|---|
[R] RStudio에서 read.csv 후 한글 깨짐 현상 해결 방법 (영문 OS) (0) | 2019.12.10 |
[R] 데이터 조작 - dplyr 패키지 활용하기 (0) | 2019.12.08 |
[R] 데이터 전처리 - 코딩 변경 (0) | 2019.12.08 |
[R] 데이터 전처리 - 결측치 처리 (제거 및 대체) (0) | 2019.12.08 |