본문 바로가기
공부/R Programming

R 기초 - ggplot2 그래픽3 - 집단별 그래프

by 혼밥맨 2021. 1. 24.
반응형

R 기초 - ggplot2 그래픽3 - 집단별 그래프

 

library(ggplot2)

 

library(car)

str(Salaries)

 

# 투명도 50% 분포도

ggplot(Salaries, aes(x=salary)) +

         geom_density(alpha=0.5)

 

# 산점도

ggplot(Salaries, aes(x=yrs.since.phd, y=salary, color=rank, shape=sex)) +

         geom_point()

 

# 막대그래프

ggplot(Salaries, aes(x=rank, fill=sex)) +

         geom_bar(position="stack")

 

ggplot(Salaries, aes(x=rank, fill=sex)) +

         geom_bar(position="dodge")

 

ggplot(Salaries, aes(x=rank, fill=sex)) +

         geom_bar(position="fill")

 

 

presummed <- data.frame(Grade=c("A", "B", "C", "D", "F"),

                                    Frequency=c(20, 45, 20, 10, 5))

 

presummed

 

# 막대 그래프

ggplot(presummed, aes(x=Grade, y=Frequency)) +

        geom_bar(stat="identity")

 

?geom_bar

 

facet_wrap()

facet_grid()

 

library(lattice)

head(singer)

 

 

# 히스토그램

ggplot(singer, aes(x=height)) + 

      geom_histogram() + 

      facet_wrap(~ voice.part, nrow=4)

 

ggplot(singer, aes(x=height)) +

      geom_density() + 

      facet_grid(voice.part ~ .)

 

ggplot(Salaries, aes(x=yrs.since.phd, y=salary)) +

         geom_point() + 

         facet_grid(sex ~ rank)

 

ggplot(Salaries, aes(x=yrs.since.phd, y=salary, color=rank, shape=rank)) +

         geom_point() + 

         facet_grid(. ~ sex)

 

반응형

댓글