시작하기무료로 시작하기

colorFactor로 색상 팔레트 만들기

지금까지는 지도 스타일을 꾸밀 때 색상만 사용했어요. colorFactor()를 사용하면 범주형 변수의 각 수준(level)에 색을 매핑하는 색상 팔레트를 만들 수 있어요.

pal <- 
   colorFactor(palette = c("blue", "red", "green"), 
               levels = c("Public", "Private", "For-Profit"))

m %>% 
    addCircleMarkers(color = ~pal(sector_label))

왜 이 특정 색상 팔레트는 사용하지 않는 편이 좋을까요?

연속형 변수를 사용해 지도의 색을 지정하고 싶다면 colorNumeric()을 참고하세요.

pal <- colorNumeric(palette = "RdBu", domain = c(25:50))

ipeds %>% 
    leaflet() %>% 
        addProviderTiles("CartoDB")  %>% 
        addCircleMarkers(radius = 2, color = ~pal(lat))

이 연습은 강의의 일부입니다

R로 만드는 leaflet 인터랙티브 지도

강의 보기

연습 안내

  • colorFactor()를 사용해 sector_label 값에 대한 색상 팔레트 pal을 만드세요. 색상은 "red", "blue", "#9b4a11"를 사용하세요.
  • pal()sector_label 값을 사용해 대학에 색을 입히는 원형 마커를 추가하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Make a color palette called pal for the values of `sector_label` using `colorFactor()`  
# Colors should be: "red", "blue", and "#9b4a11" for "Public", "Private", and "For-Profit" colleges, respectively
pal <- ___(palette = c("red", "blue", ___), 
                   levels = c("Public", "Private", "For-Profit"))

# Add circle markers that color colleges using pal() and the values of sector_label
map2 <- 
    map %>% 
        addCircleMarkers(data = ca, radius = 2, 
                         color = ~pal(sector_label), 
                         label = ~paste0(name, " (", sector_label, ")"))

# Print map2
map2
코드 편집 및 실행