library(ggplot2)
library(dplyr)
df = read.csv('student_info.csv')
๋ฐ์ดํฐํ๋ ์์ ํํ, ์ปฌ๋ผ ํ์ ํ์ธ
str(df)
ggplot(df, aes(x=weight)) + geom_histogram(binwidth = 1)
ggplot(df, aes(x=weight)) + geom_histogram(binwidth = 5)
ggplot(df, aes(x=weight)) + geom_histogram(binwidth = 1, color = 'black, fill = 'gray')
ggplot(df, aes(x=weight)) + geom_histogram(binwidth = 1, color = 'black', fill = 'gray') + geom_vline(xintercept = mean(weight), color = 'red', linetype = 'dashed', size = 1)
# ํ์กํ ๋ณ๋ก ์ถ๋ ฅ
ggplot(df, aes(x=weight, fill = bt)) + geom_histogram(binwidth = 5)
ggplot(df.student.info, aes(x=weight, fill = bt)) + geom_histogram(binwidth = 5, position = "dodge") + theme(legend.position = "top")
df.student.info = read.csv("student_info.csv")
str(df.student.info)
ggplot(df.student.info, aes(x=weight, y=height)) + geom_point()
ggplot(df.student.info, aes(x=weight, y=height, color=bt)) + geom_point(size = 4) + ggtitle("๋ชธ๋ฌด๊ฒ์ ์ฒด์ค")
df.student.info = read.csv("student_info.csv")
str(df.student.info)
ggplot(df.student.info, aes(y=weight)) + geom_boxplot(fill = 'steelblue')
ggplot(df.student.info, aes(y=weight, fill = bt)) + geom_boxplot()
library(ggplot2)
help(Nile)
year <- 1871:1970
flow.river <- data.frame(Nile)
df.flow.river <- data.frame(year, Nile)
str(df.flow.river)
ggplot(df.flow.river, aes(x=year, y=Nile)) + geom_line(col = 'red')
library(ggplot2)
ggplot(data = diamonds)
ggplot(data = diamonds) + geom_histogram(aes(x=carat))
ggplot(data = diamonds, aes(x=carat)) + geom_histogram()
ggplot(data = diamonds) + geom_density(aes(x=carat), fill = 'grey50')
ggplot(data = diamonds) + geom_density(aes(x=carat), fill = 'red')
ggplot(data = diamonds, aes(x=carat, y=price)) + geom_point()
ggplot(data = diamonds) + geom_point(aes(x=carat, y=price))
g1 <- ggplot(data = diamonds, aes(x=carat, y=price))
g2 <- geom_point(aes(color = color))
g1 + g2
g3 <- ggplot(data = diamonds)
g4 <- geom_point(aes(x=carat, y=price, color=color))
g3+g4
์ ๊ตญ์ธ๊ตฌ์กฐ์ฌ ์๋ฃ(example_population_f.csv )๋ฅผ ์ด์ฉํ์ฌ ๋ค์ํ ๋ฐ์ดํฐ ๋ถ์์ ์งํํ์ธ
์.
library(ggplot2)
library(dplyr)
library(ggthemes)
df <- read.csv('example_population_f.csv', header = T, fileEncoding = 'cp949', encoding = 'UTF-8')
df
str(df)
# ์ฒซ ์ด ์ ๊ฑฐ
df <- df[,-1]
df
# df ์ปฌ๋ผ์์ Provinces ๊ฐ ์ถฉ์ฒญ๋ถ๋, ์ถฉ์ฒญ๋จ๋์ธ ํ ์ถ์ถ
df2 <- filter(df, Provinces == '์ถฉ์ฒญ๋ถ๋' | Provinces == '์ถฉ์ฒญ๋จ๋')
# x์ถ์ city, y์ถ์ ์ธ๊ตฌ๋ก barplot()
graph <- ggplot(df, aes(x=City, y=Population, fill=Provinces)) + geom_bar(stat = 'identity') +theme_wsj()
# ๋ณด๊ธฐ ์ข๊ฒ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
graph_order <- ggplot(df2, aes(x=reorder(City, Population), y=Population, fill = Provinces)) + geom_bar(stat = 'identity') + theme_wsj()
graph_order
df3 <-filter(df, SexRatio > 1, PersInHou < 2)
df
graph2 <- ggplot(df3, aes(x=City, y=SexRatio, fill = Provinces)) + geom_bar(stat='identity') + theme_wsj()
graph2
df <- read.csv('example_population_f.csv', header = T, fileEncoding = 'cp949', encoding = 'UTF-8')
df <- df[, -1]
df <- mutate(df, SexF = ifelse(SexRatio < 1, '์ฌ์๋น์จ์ด ๋์', ifelse(SexRatio > 1, '๋จ์๋น์จ์ด ๋์', '๋จ๋
๋น์จ์ด ๊ฐ์')))
df$SexF <- factor(df$SexF)
df2 <- filter(df, Provinces == '๊ฒฝ๊ธฐ๋')
graph <- ggplot(df2, x=City, y = SexRatio-1, fill=SexF) + geom_bar(stat = 'identity', position = 'identity') + theme_wsj()
graph
< ๊ฒฝ๊ธฐ๋ ์ฑ๋น >
df4 <-filter(df, Provinces == '์์ธํน๋ณ์')
graph2 <- ggplot(df4, aes(x=City, y = SexRatio - 1, fill=SexF)) + geom_bar(stat = 'identity', position = 'identity')+ theme_whj
< ์์ธํน๋ณ์ ์ฑ๋น >
mpg ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ์ฌ ๋ค์ ์ฐจํธ๋ฅผ ๋ง๋ค์ด ๋ณด์ธ์.
1. mpg๋ฐ์ดํฐ์ cty์ hwy ๊ฐ์ ์ด๋ค ๊ด๊ณ๊ฐ ์๋์ง ์์๋ณด๋ ค๊ณ ํฉ๋๋ค. x์ถ์ cty, y์ถ์
hwy๋ก ๋ ์ฐ์ ๋๋ฅผ ๋ง๋ค์ด ๋ณด์ธ์.
- midwest ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ์ฌ ๋ค์์ ๋ถ์ํ์ธ์
- midwest ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด ์ ์ฒด ์ธ๊ตฌ์ ์์์์ธ ์ธ๊ตฌ ๊ฐ์ ์ด๋ค ๊ด๊ณ๊ฐ ์๋์ง ์์๋ณด
๋ ค๊ณ ํฉ๋๋ค. x์ถ์ poptotal, y์ถ์ popasian์ผ๋ก ์ฐ์ ๋๋ฅผ ๋ง๋ค์ด ๋ณด์ธ์.- ๊ทธ๋ฆฌ๊ณ , ์ ์ฒด ์ธ๊ตฌ๋ 50๋ง ์ดํ, ์์์์ธ ์ธ๊ตฌ๋ 1๋ง๋ช ์ดํ์ธ ์ง์ญ๋ง ์ฐ์ ๋์ ํ์๋
๊ฒ ํ์ธ์.
- ์ฐธ๊ณ : ์ง์ ํ์๋ฅผ ์์ฐ์๋ก ํ๋ ค๋ฉด options(scipen = 99) vs options(scipen = 0)
mpg <- as.data.frame(ggplot2::mpg)
1. ggplot(data = mpg, aes(x=cty, y=hwy)) + geom_point()
midwest <- as.data.frame(ggplot2::midwest)
2. ggplot(data = midwest, aes(x=poptotal, y=popasian) + geom_point()
3.
# ์ ์ ํํ๋ก ํํ
options(scipen = 99)
# ์ง์ ํํ๋ก ํํ ex) 2e + 6
options(scipen = 0)
ggplot(data = midwest, aes(x=poptotal, y=popasian) + geom_point() + xlim(0. 500000) + ylim(0,10000)
1.mpg ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด์ drv๋ณ ํ๊ท hwy๋ฅผ ๋ง๋๊ทธ๋ํ๋ก ํํ
2. ๋ง๋ ๊ทธ๋ํ์ x์ถ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌ
3. reorder()๋ฅผ ์ด์ฉํ์ฌ ํฌ๊ธฐ์์ผ๋ก ์ ๋ ฌ ๊ฐ๋ฅ
mpg <- as.data.frame(ggplot2::mpg)
1.
mpg_plot <- mpg %>% group_by(drv) %>% summarise(mean_hwy = mean(hwy))
3. ggplot(data = df_mpg, aes(x=reorder(drv, mean_hwy), y = mean_hwy)) + geom_col()
์ฝ๋๋ฅผ ์
๋ ฅํ์ธ์
mpg ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด์ ๋ถ์ํ์ธ์
1. ์ด๋ค ํ์ฌ์์ ์์ฐํ๋ โsuvโ ์ฐจ์ข ์ ๋์ ์ฐ๋น๊ฐ ๋์์ง ์์๋ณด๋ ค๊ณ ํฉ๋๋ค.
โsuvโ ์ฐจ์ข ์ ๋์์ผ๋ก ํ๊ท cty๊ฐ ๊ฐ์ฅ ๋์ ํ์ฌ ๋ค์ฏ ๊ณณ์ ๋ง๋ ๊ทธ๋ํ๋ก ํํ
ํ์ธ์. ๋ง๋๋ ์ฐ๋น๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌํ์ธ์.
2. ์๋์ฐจ ์ค์์ ์ด๋ค class๊ฐ ๊ฐ์ฅ ๋ง์์ง ์์๋ณด๋ ค๊ณ ํฉ๋๋ค. ์๋์ฐจ ์ข ๋ฅ๋ณ ๋น๋
๋ฅผ ํํํ ๋ง๋ ๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด ๋ณด์ธ์.
1.
mpg2 <- mpg %>% filter(class = 'suv) %>% group_by(manufacturer) %>% summarise(mean_cty = mean(cty)) %>% arrange(desc(mean_cty)) %>% head(5)
ggplot(data = mpg2, aes(x=reorder(manufacturer, mean_cty), y=mean_cty)) + geom_col()
2. ggplot(data = mpg, aes(x=class)) geom_bar()
mpg ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํด์ ๋ถ์ํ์ธ์
1. class๊ฐ โcompactโ, โsubcompactโ, โsuvโ์ธ ์๋์ฐจ์ cty๊ฐ ์ด๋ป๊ฒ ๋ค๋ฅธ์ง ๋น๊ตํด ๋ณด๋ ค๊ณ
ํฉ๋๋ค. ์์ ๊ทธ๋ํ๋ก ๋ง๋ค์ด ๋ณด์ธ์.mpg_plot <- mpg %>% filter(class %in% c('compact', 'subcompact', 'suv') ggplot(data = mpg_plot, aes(x=class, y=cty) + geom_boxplot()
install.packages('treemap')
library(treemap)
tree_data <- data.frame(name = c('KIM', 'LEE', 'CHOI', 'HAN'),
value = c(200, 300, 50, 600))
tree_data
treemap(tree_data, index = 'name', vSize = 'value', type = 'index')
tree_mpg <- as.data.frame(ggplot2::mpg)
tree_mpg <- tree_mpg[, c('manufacturer', 'model', 'hwy')]
tree_mpg
# FUNํจ์ ? :
tree_mpg = aggregate(hwy ~ manufacturer + model, data = tree_mpg, FUN = mean)
tree_mpg
treemap(tree_mpg, index = c('manufacturer', 'model'), vSize = 'hwy', type = 'index')
treemap(tree_mpg, index = c('manufacturer', 'model'), vSize = 'hwy', type = 'index', palatte = 'Dark2')