
library(dplyr)
library(ggplot2)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
library(dplyr)
library(ggplot2)
no_sports_cars <- filter(mpg, as.character(class) != "2seater")
ggplot(data = no_sports_cars) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
이 R 코드 예제는 ggplot2와 dplyr 패키지를 사용하여 데이터를 시각화하고 필터링하는 과정을 보여줍니다. 아래는 각 코드 부분에 대한 설명입니다.
library(dplyr)
library(ggplot2)
dplyr: 데이터를 조작하고 필터링하기 위한 패키지.ggplot2: R에서 데이터 시각화를 위한 강력한 패키지.ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
mpg 데이터셋: ggplot2에 내장된 자동차 연비 데이터셋.displ: 엔진 배기량 (리터 단위).hwy: 고속도로 연비 (마일/갤런).geom_point(): 산점도를 그리는 함수.aes(x = displ, y = hwy):displ (배기량).hwy (고속도로 연비).ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
color = class: 자동차 클래스(class)에 따라 점의 색상을 다르게 지정.compact, suv, pickup 등.no_sports_cars <- filter(mpg, as.character(class) != "2seater")
filter(): 데이터셋에서 조건에 맞는 행만 선택.class가 "2seater"(2인승 스포츠카)가 아닌 행만 선택.as.character(): class 변수를 문자형으로 변환.class는 팩터형 변수인데, 이를 문자열로 명시적으로 변환하여 비교.ggplot(data = no_sports_cars) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
no_sports_cars)로 시각화.geom_point():displ).hwy).color = class: 클래스별 색상 유지.displ)과 고속도로 연비(hwy)의 관계를 보여줌.이 예제는 데이터 필터링(filter())과 시각화(ggplot2)를 활용한 분석 과정을 보여주는 간단한 사례로, 데이터 전처리와 시각화의 기초를 익히는 데 적합합니다.