2017-12-15 4 views
1

헤더에서 말했듯이 히트 맵에 1 개의 시각적 레이어를 추가하고 싶습니다. Input히트 맵에 막대 그래프 레이어 추가 (geom_tile)

샘플 데이터는 mpg입니다.

library(tidyverse) # for dplyr and ggplot2 

data <- mpg %>% 
    mutate(quantity = 1, isnew = ifelse(year == 2008, 1, 0)) %>% 
    group_by(drv, class) %>% 
    summarise(quantity = sum(quantity), isnew = round(sum(isnew)/quantity, 2)) 

주축 - 클래스 및 drv의 두 가지 범주. 수량은 클래스와 drv의 각 교차로에 대한 자동차 수입니다. Isnew는 신차 (2008 년 대 1999 년)의 수량을 나타냅니다.

 drv  class quantity isnew 
    <chr>  <chr> <dbl> <dbl> 
1  4 compact  12 0.67 
2  4 midsize  3 0.67 
3  4  pickup  33 0.52 
4  4 subcompact  4 0.00 
5  4  suv  51 0.53 
6  f compact  35 0.40 
7  f midsize  38 0.50 
8  f minivan  11 0.45 
9  f subcompact  22 0.50 
10  r 2seater  5 0.60 
11  r subcompact  9 0.56 
12  r  suv  11 0.55 

히트 맵은

ggplot(data, aes(class, drv)) + 
    geom_tile(aes(fill = quantity)) + 
    geom_text(aes(label = isnew)) 

무엇 내가 달성하고자하는 것은 스택 바 각 섹션에 텍스트 값을 변경하는 것입니다. 히트 맵의 초기 색상을 저장하려면 모든 막대의 맨 덩어리가 투명해야합니다. 하단 히트 맵 섹터의 값은 isnew이어야합니다.

는 일부 그리기 (단지 몇 바,하지만 난 그들 모두 원하는) 페인트 아마 ggsubpolot 패키지를 통해 수행 할 수 in here 찾을 수 8P

Output

에서 원하는 출력을. 그러나 Grolemund github에서 언급 한 것처럼 "ggplot2 그래픽에 포함 시키십시오"는 더 이상 사용되지 않습니다.

답변

1

이 플롯은 나에게 혼란스러워 보였지만, 제작시 균열에 견딜 수 없었습니다. geom_segment을 사용하여 막대를 만들고 우리는 밑에있는 drv 요소 코딩을 사용하여 막대와 텍스트 레이블을 y 축의 적절한 위치에 배치합니다.

ggplot(data, aes(class, drv)) + 
    geom_tile(aes(fill = quantity), colour="white", size=0.5) + 
    geom_segment(aes(y=as.numeric(as.factor(drv)) - 0.5, 
        yend=as.numeric(as.factor(drv)) - 0.5 + isnew, 
        x=class, xend=class), 
       colour="yellow", size=10) + 
    geom_text(aes(label = isnew, y=(2*as.numeric(as.factor(drv)) + isnew)/2 - 0.5), 
      colour="grey30", size=3) + 
    theme_classic() 

enter image description here

아마 더 좋을 거라 크기 미적으로 geom_point를 사용하여 :

ggplot(data, aes(class, drv)) + 
    geom_tile(aes(fill = quantity), colour="white", size=0.5) + 
    geom_point(aes(size=isnew), shape=21, fill="white") + 
    scale_size_continuous(range=c(1,10)) + 
    theme_classic() 

enter image description here

관련 문제