본문 바로가기

R

[R] 시각화 - 꺾은선 그래프, 산점도, 분할보기

꺾은선 그래프

 geom_line()

x <- data.frame(day=1:5,weight=c(50,53,52,60,48))

ggplot(data=x,aes(x=day,y=weight))+
  geom_line(linetype=4) #linetype=1~6

# 0 : blank, 1 : solid, 2 : dashed, 3 : dotted, 4 : dotdash, 5 : longdash, 6 : twodash

+) dplyr로 꺾은선 그래프 생성하기

library(dplyr)
df%>%
  filter(전염병=='A형간염')%>%
  select(년도,건수)%>%
  ggplot(aes(x=as.integer(년도),y=건수))+ #factor형은 int로 변경
  geom_line()+
  scale_x_continuous(breaks = 1:5,labels = 2015:2019) #breaks= 만들고 labels= 표현!

 


[문제 176] ggplot을 이용해서 line plot을 생성해주세요.

data <- read.csv("c:/data/감염병군별발생현황.csv",header=T)

data <- data[data$법정감염병군별.1.=='제1군',]
data <- data[-1,-1]
names(data) <- c('전염병','2015','2016','2017','2018','2019')
data[,2:6] <- lapply(data[,2:6],as.integer)

library(reshape2)
df <- melt(data,id='전염병')
names(df)[2:3] <- c('년도','건수')

ggplot(data=df[df$전염병!='A형간염',],aes(x=년도,y=건수,group=전염병,color=전염병))+
  geom_line(aes(linetype=전염병))+ #축에 있는 컬럼 기준으로 linetype을 다르게 설정
  scale_linetype_manual(values = c('dashed','dotted','dotdash','longdash','twodash'))+
  scale_color_manual(values=c('red','blue','green','yellow','pink'))+
  scale_size_manual(values=c(seq(1,1.8,by=0.2)))

 


산점도

geom_point()

ggplot(data=df[df$전염병!='A형간염',],aes(x=년도,y=건수,group=전염병,color=전염병))+
  geom_point(aes(shape=전염병,size=전염병))+ #shape=0~25
  scale_shape_manual(values = c(10:14))+
  scale_color_manual(values=c('red','blue','green','yellow','pink'))+
  scale_size_manual(values = c(seq(1,1.8,by=0.2)))


그래프 분할해서 보기

facet_grid(.~분할 기준)

ggplot(data=subset(employees,DEPARTMENT_ID %in% c(30,50)),aes(x=SALARY))+
  geom_histogram(binwidth = 2000)+
  facet_grid(.~DEPARTMENT_ID)

가로형

ggplot(data=subset(employees,DEPARTMENT_ID %in% c(30,50)),aes(x=SALARY))+
  geom_histogram(binwidth = 2000)+
  facet_grid(DEPARTMENT_ID~.)

 

'R' 카테고리의 다른 글

[R] 시각화 문제 -barplot,ggplot  (0) 2022.01.28
[R] 시각화 - ggplot histogram,boxplot  (0) 2022.01.28
[R] 시각화 - 원형차트, 양방향 barplot  (0) 2022.01.27
[R] 시각화 - ggplot  (0) 2022.01.27
[R] 시각화 - histogram, box plot  (0) 2022.01.27
Recent Posts
Popular Posts
Recent Comments