ggplot(iris, aes(x=Sepal.Width, fill=Species, color=Species)) +
geom_histogram(binwidth=0.5, position="dodge") +
theme(legend.position="top")
설명
x=Sepal.Width
- 히스토그램을 작성할 대상 열
fill=Species
- 히스토그램의 막대 내부를 채울 색을 지정
- Species는 팩터 타입이기 때문에 숫자 1,2,3으로 변환되어 품종별로 막대의 색이 다르게 채워지게 됨
color=Species
- 막대의 윤곽선 색
binwidth=0.5
- 데이터 구간은 0.5 간격으로 나누어 히스토그램 작성
position="dodge"
- 동일 구간의 막대들을 어떻게 그릴지 결정
- "dodge"는 막대들을 겹치지 않고 병렬로 그림
ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width)) +
geom_point()
ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width, color=Species)) +
geom_point(size=3) +
ggtitle("꽃잎의 길이와 폭") +
theme(plot.title=element_text(size=25, face="bold", colour="steelblue"))
설명
- 점 모양도 품종별로 다르게 하려면
aes()
에shape=Species
를 추가
ggplot(data=iris, aes(y=Petal.Length)) +
geom_boxplot(fill="yellow")
ggplot(data=iris, aes(y=Petal.Length, fill=Species)) +
geom_boxplot()
year <- 1937:1960
cnt <- as.vector(airmiles)
df <- data.frame(year, cnt)
head(df)
ggplot(data=df, aes(x=year, y=cnt)) +
geom_line(col="red")
> head(df)
year cnt
1 1937 412
2 1938 480
3 1939 683
4 1940 1052
5 1941 1385
6 1942 1418
- 고차원 데이터를 2, 3차원 데이터로 축소하는 기법
- 어쩔 수 없이 정보의 손실 발생
- 하지만 고차원상의 데이터는 눈으로 분포를 관찰할 수 없기 때문에 정보의 손실을 감수하고 저차원상으로 축소하여 분포를 관찰하는 것이 의미있음
ds <- iris[,-5]
# 중복 데이터 제거
dup <- which(duplicated(ds))
dup
ds <- ds[-dup,]
ds.y <- iris$Species[-dup] # 중복을 제외한 품종 정보
# t-SNE 실행
tsne <- Rtsne(ds, dims=2, perplexity=10)
# 축소결과 시각화
df.tsne <- data.frame(tsne$Y)
head(df.tsne)
ggplot(df.tsne, aes(x=X1,y=X2, color=ds.y)) +
geom_point(size=2)
> head(df.tsne)
X1 X2
1 -7.644435 15.024222
2 -13.986896 9.925678
3 -13.875295 7.365891
4 -12.765561 7.440136
5 -6.697645 14.244768
6 -5.903073 20.539361
설명
- t-sne를 사용하려면 중복된 데이터가 존재하면 안 됨
Rtsne()
의dims
는 몇 차원으로 축소할 것인지 결정perplexity=10
은 차원 축소 과정에서 데이터를 샘플링하는데 샘플의 개수를 결정, (대상 데이터의 행의 수)/3 보다 작게 지정
사고
library("rgl")
을 하면 R studio가 중단되는 문제가 발생options(rgl.debug=T)
로 해결 가능
tsne <- Rtsne(ds, dims=3, perplexity=10)
df.tsne <- data.frame(tsne$Y)
head(df.tsne)
# 회귀면이 포함된 3차원 산점도
scatter3d(x=df.tsne$X1, y=df.tsne$X2, z=df.tsne$X3)
# 회귀면이 없는 3차원 산점도
points <- as.integer(ds.y)
color <- c("red","green","blue")
scatter3d(x=df.tsne$X1, y=df.tsne$X2, z=df.tsne$X3,
point.col=color[points], # 점의 색을 품종별로 다르게게
surface=F) # 회귀면을 표시하지 않음
> head(df.tsne)
X1 X2 X3
1 -2.19823150 -16.67887 22.53799
2 -3.84217693 -27.85199 19.26846
3 -1.98813180 -29.20379 23.45456
4 -4.01074219 -29.00402 23.22839
5 -3.99623650 -16.11633 22.46584
6 -0.08025083 -9.30155 24.56214
library(ggmap)
register_google(key='키') # API키 등록
gc <- geocode(enc2utf8("종로구")) # 지점의 경도 위도
gc
cen <- as.numeric(gc) # 경도 위도를 숫자로
cen
map <- get_googlemap(center=cen) # 지도 생성
ggmap(map) # 지도 출력
> gc
# A tibble: 1 × 2
lon lat
<dbl> <dbl>
1 127. 37.6
> cen
[1] 126.97936 37.57295
get_googlemap()
의 매개변수
center
: 지도 중심의 좌표값 지정- 경도, 위도
zoom
: 지도의 확대 크기를 지정- 3(대륙) ~ 21(빌딩), 기본값은 10(도시)
size
: 지도의 가로와 세로의 픽셀 크기를 입력- 최댓값은 640x640
maptype
: 출력될 지도 유형 지정- "terrain"(기본값), "roadmap", "satellite", "hybrid"
gc <- geocode(enc2utf8("설악산"))
cen <- as.numeric(gc)
map <- get_googlemap(center=cen,
zoom=9,
size=c(640,640),
maptype="roadmap")
ggmap(map)
cen <- c(-118.233248,34.085015)
map <- get_googlemap(center=cen)
ggmap(map)
gc <- geocode(enc2utf8("용인"))
cen <- as.numeric(gc)
map <- get_googlemap(center=cen,
maptype="roadmap",
markers=gc) # 마커의 위치
ggmap(map)
names <- c("용두암","성산일출봉","정방폭포","중문관광단지","한라산1100고지","차귀도")
addr <- c("제주시 용두암길 15",
"서귀포시 성산읍 성산리",
"서귀포시 동홍동 299-3",
"서귀포시 중문동 2624-1",
"서귀포시 색달동 산1-2",
"제주시 한경면 고산리 125")
gc <- geocode(enc2utf8(addr))
gc
# 관광지 명칭과 좌표값으로 데이터프레임 생성
df <- data.frame(name=names,
lon=gc$lon,
lat=gc$lat)
df
cen <- c(mean(df$lon), mean(df$lat))
map <- get_googlemap(center=cen,
maptype="roadmap",
zoom=10,
size=c(640,640),
markers=gc)
ggmap(map)
gmap <- ggmap(map)
gmap+geom_text(data=df,
aes(x=lon, y=lat),
size=5,
label=df$name)
> gc
# A tibble: 6 × 2
lon lat
<dbl> <dbl>
1 127. 33.5
2 127. 33.5
3 127. 33.2
4 126. 33.2
5 126. 33.3
6 126. 33.3
> df
name lon lat
1 용두암 126.5117 33.51496
2 성산일출봉 126.9324 33.46993
3 정방폭포 126.5717 33.24502
4 중문관광단지 126.4206 33.24575
5 한라산1100고지 126.3865 33.25698
6 차귀도 126.2057 33.30110
sp <- sample(1:nrow(wind),50)
df <- wind[sp,]
head(df)
cen <- c(mean(df$lon), mean(df$lat))
gc <- data.frame(lon=df$lon, lat=df$lat)
head(gc)
# 측정 위치에 마터 표시
map <- get_googlemap(center=cen,
maptype= "roadmap",
zoom=6,
marker=gc)
ggmap(map)
# 풍속을 원의 크기로 표시
map <- get_googlemap(center=cen, # 마커 없는 지도 가져오기
maptype="roadmap",
zoom=6)
gmap <- ggmap(map) # 지도를 저장
gmap+geom_point(data=df, # 풍속을 원의 크기로 표시
aes(x=lon,y=lat,size=spd),
alpha=0.5,
col="blue")+
scale_size_continuous(range=c(1,14)) # 원의 크기 조절절
> head(df)
lon lat dir dur std spd delta_lat delta_lon
36566 -99.103 31.424 5.23282871 0 0.9218 9.2783 -0.17352012 0.09945234
21378 -98.174 28.459 0.08623672 0 0.7477 12.0805 0.01722597 0.19925678
47585 -93.661 33.548 2.68679476 0 0.6771 30.4304 0.08785615 -0.17966997
17954 -99.015 27.796 4.30740278 0 0.9745 8.2020 -0.18382157 -0.07880122
8026 -96.360 25.849 5.21858682 0 0.7504 25.1228 -0.17491886 0.09697108
29303 -99.059 30.008 4.84520854 0 0.9056 9.4863 -0.19823849 0.02648588
> head(gc)
lon lat
1 -99.103 31.424
2 -98.174 28.459
3 -93.661 33.548
4 -99.015 27.796
5 -96.360 25.849
6 -99.059 30.008