Secondary y axis in ggplot2

Sangbuem Cho·2023년 2월 10일
0

Rgraphics

목록 보기
1/2

scale이 다른 두 종속변수

mtcars %>% 
  ggplot() +
  geom_line(aes(mpg, disp), color ="grey20") +
  geom_line(aes(mpg, wt), color ="red")

wt value를 secondary axis로 보내기

scaleFactor <- max(mtcars$disp, na.rm = T) / max(mtcars$wt, na.rm = T)

mtcars %>% 
  ggplot() +
  geom_line(aes(mpg, disp), color ="grey20") +
  geom_line(aes(mpg, wt * scaleFactor), color ="red") +
  scale_y_continuous(
    sec.axis = sec_axis(~./scaleFactor, name="wt")
  )
profile
data scientist

0개의 댓글