## 패키지 로드
library(dplyr)
library(plotly)
library(RColorBrewer)
### 한글 폰트 설정
library(showtext)
font_add_google("Nanum Gothic", "nanumgothic")
df <- read.csv("https://vincentarelbundock.github.io/Rdatasets/csv/DAAG/edcCO2.csv")
data.2 <- df[-1]
head(data.2)
## age co2
## 1 137 280.4
## 2 268 274.9
## 3 279 277.9
## 4 395 279.1
## 5 404 281.9
## 6 485 277.7
plot_ly(data.2,x=~age,y=~co2,
type='scatter') %>%
layout(title = "age에 따른 남극 얼음의 co2 변화",
font=list(family ='nanumgothic'),
xaxis = list(title="age", zeroline = F),
yaxis = list(title="co2", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
## 라인 그래프
plot_ly(data.2,x=~age,y=~co2,
type='scatter',
mode = 'lines') %>%
layout(title = "age에 따른 남극 얼음의 co2 변화",
font=list(family ='nanumgothic'),
xaxis = list(title="age", zeroline = F),
yaxis = list(title="co2", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
## 라인 그래프 + 산점도
plot_ly(data.2,x=~age,y=~co2,
type='scatter',
mode = 'lines+markers') %>%
layout(title = "age에 따른 남극 얼음의 co2 변화",
font=list(family ='nanumgothic'),
xaxis = list(title="age", zeroline = F),
yaxis = list(title="co2", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
airquality %>% head()
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
airquality %>% summary()
## Ozone Solar.R Wind Temp
## Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00
## 1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00
## Median : 31.50 Median :205.0 Median : 9.700 Median :79.00
## Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88
## 3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00
## Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00
## NA's :37 NA's :7
## Month Day
## Min. :5.000 Min. : 1.0
## 1st Qu.:6.000 1st Qu.: 8.0
## Median :7.000 Median :16.0
## Mean :6.993 Mean :15.8
## 3rd Qu.:8.000 3rd Qu.:23.0
## Max. :9.000 Max. :31.0
##
plot_ly(airquality,y=~Ozone,
type='scatter',
mode = 'lines',
name = 'Ozone') %>%
add_trace(airquality,y=~Temp,
type='scatter',
mode = 'lines',
name = 'Temp') %>%
add_trace(airquality,y=~Wind,
type='scatter',
mode = 'lines',
name = 'Wind') %>%
layout(title = "",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
library(reshape2)
air.use <- airquality %>% select(Ozone, Temp, Wind) %>% melt()
air.use %>% summary()
## variable value
## Ozone:153 Min. : 1.00
## Temp :153 1st Qu.: 10.90
## Wind :153 Median : 31.50
## Mean : 43.43
## 3rd Qu.: 77.00
## Max. :168.00
## NA's :37
air.use %>% head()
## variable value
## 1 Ozone 41
## 2 Ozone 36
## 3 Ozone 12
## 4 Ozone 18
## 5 Ozone NA
## 6 Ozone 28
plot_ly(air.use,y=~value,
type='scatter',
mode = 'lines',
split = ~variable) %>%
layout(title = "split = ~variable",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
plot_ly(
data = airquality,y=~Ozone,
type='scatter',
mode = 'lines',
name = 'Blue Gray',
line = list(color = "#98AFC7")) %>%
add_trace(data = airquality,y=~Temp,
type='scatter',
mode = 'lines',
name = 'Blue Turquoise',
line = list(color = "#43C6DB")) %>%
add_trace(data = airquality,y=~Wind,
type='scatter',
mode = 'lines',
name = 'Coral Peach',
line = list(color = "#FBD5AB")) %>%
layout(title = "color = '컬러코드'",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
plot_ly(air.use,y=~value,
type='scatter',
mode = 'lines',
split = ~variable,
color = ~variable,
colors = "Set1") %>%
layout(title = "color = ~variable ",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
plot_ly(air.use,y=~value,
type='scatter',
mode = 'lines',
color = ~variable,
colors = c("#FFCE44", "#F87431", "#E4287C")) %>%
layout(title = "color = ~variable",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
plot_ly(air.use,y=~value,
type='scatter',
mode = 'lines',
split = ~variable,
line = list(width = 3),
linetype = ~variable) %>%
layout(title = "linetype = ~variable ",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
x <- c(1:5)
y <- c(1, 3, 2, 3, 1)
fig <- plot_ly(x = ~x)
fig <- fig %>% add_lines(y = ~y, name = "solid", line = list(dash = "solid", width = 4))
fig <- fig %>% add_lines(y = y + 5, name = "dot", line = list(dash = "dot", width = 4))
fig <- fig %>% add_lines(y = y + 10, name = "dash", line = list(dash = "dash", width = 4))
fig <- fig %>% add_lines(y = y + 15, name = "longdash", line = list(dash = "longdash", width = 4))
fig <- fig %>% add_lines(y = y + 20, name = "dashdot", line = list(dash = "dashdot", width = 4))
fig <- fig %>% add_lines(y = y + 25, name = "longdashdot", line = list(dash = "longdashdot", width = 4))
fig %>% layout(title = "dash 종류",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
fig <- plot_ly(x = ~x)
fig <- fig %>% add_lines(y = ~y, name = "linear", line = list(shape = "linear", width = 4))
fig <- fig %>% add_lines(y = y + 5, name = "spline", line = list(shape = "spline", width = 4))
fig <- fig %>% add_lines(y = y + 10, name = "vhv", line = list(shape = "vhv", width = 4))
fig <- fig %>% add_lines(y = y + 15, name = "hvh", line = list(shape = "hvh", width = 4))
fig <- fig %>% add_lines(y = y + 20, name = "vh", line = list(shape = "vh", width = 4))
fig <- fig %>% add_lines(y = y + 25, name = "hv", line = list(shape = "hv", width = 4))
fig %>% layout(title = "shape 종류",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
fig <- plot_ly(x = ~x)
fig <- fig %>% add_lines(y = ~y, name = "2", line = list( width = 2))
fig <- fig %>% add_lines(y = y + 5, name = "4", line = list(width = 4))
fig <- fig %>% add_lines(y = y + 10, name = "6", line = list(width = 6))
fig <- fig %>% add_lines(y = y + 15, name = "8", line = list(width = 8))
fig %>% layout(title = "width = n",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))
library(reshape2)
air.use2 <- airquality %>% filter(Month == "5") %>%
select(Day, Ozone,Solar.R, Temp, Wind) %>%
melt(id.vars = 'Day')
air.use2 %>% summary()
## Day variable value
## Min. : 1 Ozone :31 Min. : 1.00
## 1st Qu.: 8 Solar.R:31 1st Qu.: 12.00
## Median :16 Temp :31 Median : 37.00
## Mean :16 Wind :31 Mean : 68.71
## 3rd Qu.:24 3rd Qu.: 69.00
## Max. :31 Max. :334.00
## NA's :9
air.use2 %>% head()
## Day variable value
## 1 1 Ozone 41
## 2 2 Ozone 36
## 3 3 Ozone 12
## 4 4 Ozone 18
## 5 5 Ozone NA
## 6 6 Ozone 28
plot_ly(air.use2,x = ~Day, y=~value,
type='scatter',
mode = 'lines+text',
split = ~variable,
connectgaps = T, # NA 값이 존재하기 때문에 NA 무시하고 연결
showlegend = F) %>%
## Ozone trace 마지막 위치에 주석 추가
add_annotations(text = 'Ozone',
x = air.use2 %>% filter(variable == 'Ozone' & Day ==max(Day)) %>%
select(Day) %>% pull(),
y = air.use2 %>% filter(variable == 'Ozone' & Day ==max(Day)) %>%
select(value) %>% pull(),
xanchor = 'left', showarrow = F,
font = list(family = "nanumgothic", size =16)) %>%
## Solar.R trace 마지막 위치에 주석 추가
add_annotations(text = 'Solar.R',
x = air.use2 %>% filter(variable == 'Solar.R' & Day ==max(Day)) %>%
select(Day) %>% pull(),
y = air.use2 %>% filter(variable == 'Solar.R' & Day ==max(Day)) %>%
select(value) %>% pull(),
xanchor = 'left', showarrow = F,
font = list(family = "nanumgothic", size =16)) %>%
## Temp trace 마지막 위치에 주석 추가
add_annotations(text = 'Temp',
x = air.use2 %>% filter(variable == 'Temp' & Day ==max(Day)) %>%
select(Day) %>% pull(),
y = air.use2 %>% filter(variable == 'Temp' & Day ==max(Day)) %>%
select(value) %>% pull(),
xanchor = 'left', showarrow = F,
font = list(family = "nanumgothic", size =16)) %>%
## Wind trace 마지막 위치에 주석 추가
add_annotations(text = 'Wind',
x = air.use2 %>% filter(variable == 'Wind' & Day ==max(Day)) %>%
select(Day) %>% pull(),
y = air.use2 %>% filter(variable == 'Wind' & Day ==max(Day)) %>%
select(value) %>% pull(),
xanchor = 'left', showarrow = F,
font = list(family = "nanumgothic", size =16)) %>%
layout(title = "Label Lines with Annotations",
font=list(family ='nanumgothic'),
xaxis = list(title="", zeroline = F),
yaxis = list(title="", zeroline = F),
margin = list(l=10, r=20, b=10, t=30, pad=0))