2017-01-13 2 views
1

트리 맵의 레이블을 아이콘으로 바꾸고 싶습니다. 예를 들어, 아래의 더미 데이터에서 상태 플래그로 레이블을 바꾸려고합니다. treemap 패키지 또는 다른 패키지를 사용하여 R로 구현할 수 있습니까? 단지 부분적인 대답 만R : 트리 맵의 레이블 대신 아이콘 사용

library(treemap) 

my_data=rbind(data.frame(state="CA",Car=c(6,8,5)), 
      data.frame(state="FL",Car=c(5,6,4)), 
      data.frame(state="NY",Car=c(10,5,0)), 
      data.frame(state="CT",Car=c(10,5,5)), 
      data.frame(state="MD",Car=c(7,7,8))) 


treemap(my_data, 
    index="state", 
    vSize="Car", 
    vColor = "Car", 
    type="value", 
    border.col ="white", 
    title="", 
    title.legend="", 
    ) 

enter image description here

답변

0

일부 사용 될 수 있습니다.

당신이 개체로 트리 맵 함수의 출력을 저장하면 사각형의 좌표, 너비와 높이에 액세스 할 수 있으며 다음과 같이 ggplot 그들에게 자신을 다시 사용

library(tidyverse) 
library(ggplot2) 
library(treemap) 

my_data=rbind(data.frame(state="CA",Car=c(6,8,5)), 
       data.frame(state="FL",Car=c(5,6,4)), 
       data.frame(state="NY",Car=c(10,5,0)), 
       data.frame(state="CT",Car=c(10,5,5)), 
       data.frame(state="MD",Car=c(7,7,8))) 


obj <- treemap(my_data, 
     index="state", 
     vSize="Car", 
     vColor = "Car", 
     type="value", 
     border.col ="white", 
     title="", 
     title.legend="", 
) 


obj$tm %>% 
    mutate(x1 = x0 + w, 
     y1 = y0 + h, 
     xcentre = (x0 + x1)/2, 
     ycentre = (y0 + y1)/2) %>% 
    ggplot(aes(xmin = x0, ymin = y0, xmax = x1, ymax = y1, fill = vColor, x = xcentre, y = ycentre, label = state)) + 
    geom_rect(colour = "white") + 
    geom_text(colour = "white") + 
    coord_equal() + 
    theme_void() 

enter image description here

이제 geom_text()(xcentre, ycentre)에 플래그를 배치하는 것으로 바꾸는 것이 문제입니다. baptiste; s ggflags package이 작업을 수행하지만 현재는 ggplot2과 동기화되지 않았습니다. 따라서 귀하의 옵션은 ggflags (또는 baptiste에 매우 잘 수행하도록 요구하는) 또는 다양한 어색한 해결 방법을 업데이트하고 있습니다.

편집 - 가로 세로 비율을 확인하고 coord_equal을 사용하는 것이 올바른지 여부를 확인해야합니다. 이것은 독자를위한 운동으로 남아 있습니다.

관련 문제