๋ฐ์ดํฐ ๊ณผํ์ ์ํ R ์ฐ์ต๋ฌธ์
3.Data visualisation (3.1~3.5)
[3.1.1 Prerequisites]
install.packages('tidyverse')
library("tidyverse")
str(mpg)
?mpg
[3.2.2 Creating a ggplot]
ggplot(data=mpg)
str(mpg)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
#geom_point() ์ฐ์ ๋ ์ถ๋ ฅ
#mapping ์๊ฐ์ ์์ฑ ์ค์ aes(x= y=)
[3.2.4 Exercises]
Q. ์คํ ggplot(data = mpg). ๋ฌด์์ ๋ณผ ์ ์์ต๋๊น?
ggplot(data = mpg)
A. ์๋ฌด๊ฒ๋ ์๋ ๋น ๋ค๋ชจ ์์ฑ
Q. mpg์ ๋ช ๊ฐ์ ํ์ด ์์ต๋๊น? ์ด์ ๋ช ๊ฐ์
๋๊น?
mpg #234X11
nrow(mpg) #234
ncol(mpg) #11
A. 123๊ฐ, 11๊ฐ
Q. drv๋ณ์์ ๋ํด ์ค๋ช
ํ์ธ์. ์์๋ณด๋ ค๋ฉด ๋์๋ง์ ์ฝ์ด๋ณด์ธ์.
?mpg
A. the type of drive train, where f = front-wheel drive, r = rear wheel drive, 4 = 4wd
Q. hwy์ ์ฐ์ ๋๋ฅผ ๋ง๋์ญ์์ค. (cy1)
ggplot (data = mpg) +
geom_point(mapping = aes(x=cyl, y=hwy))
ggplot(mpg, aes(x = cyl, y = hwy)) +
geom_point()
Q1. class์ ์ฐ์ ๋๋ฅผ ๊ทธ๋ฆฌ๋ฉด drv์ด๋ป๊ฒ ๋ ๊น์?
Q2. ํ๋กฏ์ด ์ ์ฉํ์ง ์์ ์ด์ ๋ ๋ฌด์์
๋๊น?
ggplot(data=mpg)+
geom_point(mapping = aes(x=class, y=drv))
A. drv์ class๋ ๋ชจ๋ ๋ฒ์ฃผํ ๋ณ์๋ก ์ฐ์ ๋์ ์ ํฉํ์ง ์์ ๋ฐ์ดํฐ์ด๋ค.
x,y๋ก ์กฐํฉํด ํ์ํ๋ฏ๋ก (drv๋ 3๊ฐ์ ๊ฐ, class๋ 7๊ฐ์ ๊ฐ ์ฌ์ฉ) 21๊ฐ๋ง ๊ด์ฐฐ ๊ฐ๋ฅํ๋ค.
count(mpg, drv, class)
[3.3 Aesthetic mappings]
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
Left
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
* alpha : ์ ์ ํฌ๋ช ๋ ์กฐ์
Right
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
* color = "blue" : ๋ชจ๋ ์ ์ด ํ๋์
[3.3.1 Exercises]
Q. ์ด ์ฝ๋์ ๋ฌด์์ด ๋ฌธ์ ์์ต๋๊น? ํฌ์ธํธ๊ฐ ํ๋์์ด ์๋ ์ด์ ๋ ๋ฌด์์
๋๊น?
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
A. ๊ดํธ์ค์ ๋๋ฌธ. aes๋ ๋ณ์์ ๋งคํ ๊ฐ๋ฅํ ์๊ฐ์ ์์ฑ์ ๋งคํ์ธ์์ ์ ๋ฌ.
Q. mpg์ ์ด๋ค ๋ณ์๊ฐ ๋ฒ์ฃผํ์
๋๊น? ์ด๋ค ๋ณ์๊ฐ ์ฐ์์ ์
๋๊น?
(ํํธ: mpg๋ฐ์ดํฐ ์ธํธ์ ๋ํ ๋ฌธ์๋ฅผ ์ฝ์ผ๋ ค๋ฉด ์
๋ ฅ ํ์ญ์์ค). ์คํํ ๋ ์ด ์ ๋ณด๋ฅผ ์ด๋ป๊ฒ ๋ณผ ์ ์์ต๋๊น?
str(mpg)
?mpg
A. ๋ฒ์ฃผํ:manufacturer, model, trans, drv, fl, class
A. ์ฐ์ํ:displ, year, cyl, cty, hwy
Q. ์ฐ์ ๋ณ์๋ฅผ color, size, shape์ ๋งคํํ์ธ์.
Q. ์ด๋ฌํ ๋ฏธํ์ ๋ฒ์ฃผํ ๋ณ์์ ์ฐ์ํ ๋ณ์์ ๋ํด ์ด๋ป๊ฒ ๋ค๋ฅด๊ฒ ์๋ํฉ๋๊น?
ggplot(data=mpg)+
geom_point(mapping = aes(x=displ, y=hwy, color=cty)) #cty:๊ฐค๋ฐ ๋น ๋์ ๋ง์ผ
A. color์ mappingํ ๋, ์ ์ ์์ด ์ ์ ์ฐํด์ง๋ค.
ggplot(data=mpg)+
geom_point(mapping = aes(x=displ, y=hwy, size=cty))
A. size์ mappingํ ๋ ์ ์ ํฌ๊ธฐ๊ฐ ์ง์์ ์ผ๋ก ์ปค์ง๋ค.
ggplot(data=mpg)+
geom_point(mapping = aes(x=displ, y=hwy, shape=cty))
A. error. ์ฐ์ ๋ณ์๋ shape์ mappingํ ์ ์์
Q. ๋์ผํ ๋ณ์๋ฅผ ์ฌ๋ฌ aes์ ๋งคํํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น์?
ggplot(data=mpg)+
geom_point(mapping = aes(x=displ, y=hwy, color=hwy, size=displ))
A. hwy๋ y์ถ, ์์ ๋๋๋ก mapping. displ์ x์ถ, ์ ์ ํฌ๊ธฐ๋ก mapping.
๋จ์ผ ๋ณ์๊ฐ ์ฌ๋ฌ aes์ ์ค๋ณตmapping๋ ์ ์์ผ๋ฏ๋ก, ํผํ๋ค.
Q. stroke aesthetic๊ฐ ๋ฌด์์ธ๊ฐ์? ์ด๋ค ๋ชจ์์ผ๋ก ์๋ํฉ๋๊น? (ํํธ: geom_point ์ฌ์ฉ)
ggplot(data=mpg)+
geom_point(mapping = aes(x=displ, y=hwy), shape=21, color="yellow", fill="blue", size=3, stroke=3)
Q. aes์ ๋ณ์ ์ด๋ฆ์ด ์๋ ๋ค๋ฅธ ๊ฒ์ผ๋ก ๋งคํํ๋ฉด ex) aes(colour = displ < 5)์ด๋ป๊ฒ ๋ ๊น์? ์ฐธ๊ณ ๋ก x์ y๋ ์ง์ ํด์ผ ํฉ๋๋ค.
ggplot(data=mpg)+
geom_point(mapping=aes(x=displ, y=hwy, color=displ<5))
A. aes๋ ์ฐ์ฐ์์๋ mapping๊ฐ๋ฅํ๋ค.
์์ ๋ณ์๊ฐ ์ถ๊ฐ๋ ๊ฒ์ฒ๋ผ ์๋ํ๊ณ , ์ด ๊ฒฝ์ฐ displ<5์ ๊ฒฐ๊ณผ๋ T/F๊ฐ์ ์ทจํ๋ ๋
ผ๋ฆฌ ๋ณ์๊ฐ ๋๋ค.
'๊ฐ์ธ๊ณต๋ถ > R' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
19. R EDA, ๋ฐ์ดํฐ์ ์ฒ๋ฆฌ ์ฐ์ต๋ฌธ์ (0) | 2021.10.03 |
---|---|
16. ํต๊ณ๊ธฐ๋ณธ๊ฐ๋ (์ด์ฐํ๋ฅ ๋ถํฌ~๋ชจํ๊ท ์ถ์ ) (0) | 2021.09.30 |
14. R๊ธฐ์ด์ฐ์ต๋ฌธ์ (0) | 2021.09.28 |
13. R์ ๋ฆฌ (๋ฐ์ดํฐ ์ ์ , ๊ทธ๋ํ ๋ง๋ค๊ธฐ) (0) | 2021.09.27 |
12. R์ ๋ฆฌ (๋ฐ์ดํฐ ์์ /๊ฐ๊ณต) (0) | 2021.09.26 |