2014-09-14 2 views
1

나는 ggplot에서 scale_fill_manual으로 가져 오는 모순되는 결과에 당황합니다.대수롭지 않은 결과가있는 scale_fill_manual

변수가 heat 인 경우 빨간색 타일을 플롯하고, 0이면 회색 타일을 지정하고 1이면 녹색 타일을 만듭니다.이 코드는 루프에 중첩되어 예상되는 그래프를 압도적으로 생성합니다 . 몇 가지 알려지지 않은 이유로 (최소한 나에게), 데이터와 모순되는 색으로 그래프를 만드는 반복이 있습니다.

아래의 데이터 1은 두 개의 녹색 타일 (나머지 회색)이있는 그래프를 생성해야하지만 압도적 인 빨간색 타일 (회색이어야 함)과 두 개의 회색 타일 (녹색이어야 함)을 얻습니다. 열 -1 값의 데이터에는 관측이 없습니다. 그러나 거의 모든 타일은 빨간색 (값이 0 인 타일)입니다.

대조적으로 정확한 결과를 생성하는 예제도 제공합니다 (데이터 2 & 그래프 2 맨 아래).

이것은 https://github.com/hadley/ggplot2/issues/384과 관련된 버그입니까? 코드에 문제가 있습니까? 또는 나는 무엇인가 놓치고 있냐?

코드

comp.plot <- ggplot(df, aes(y=variable, x=as.factor(as.character(year)), fill=as.factor(heat)))+ 
    geom_tile()+ 
    ggtitle(paste("Difference"))+ 
    theme(plot.title=element_text(face="bold"), 
     legend.position="bottom", 
     legend.title=element_text(size=7), 
     legend.text=element_text(size=5), 
     legend.box="vertical", 
     axis.title.x = element_blank(), 
     axis.text.x = element_text(angle=90, size=6), 
     axis.title.y = element_blank(), 
     axis.text.y = element_blank(), 
     axis.ticks.y = element_blank()) + 

    guides(fill=guide_legend(title.position="top", 
          keywidth=0.5, keyheight=0.5))+ 

    scale_fill_manual(name="Promise vs Practice", 
        breaks=c(-1,0,1), 
        labels=c("No Practice","No Promise","Practice"), 
        drop=FALSE, 
        values=c("darkred","lightgrey","darkgreen")) 

데이터 1

df <– as.data.frame(structure(list(variable = structure(c(11L, 14L, 12L, 13L, 4L, 
3L, 2L, 1L, 16L, 15L, 8L, 6L, 7L, 9L, 5L, 10L), .Label = c("eps_commission", 
"eps_company", "mps_armyint", "mps_milcmd", "other_constitution", 
"other_parlelect", "other_preselect", "other_proprep", "other_referendum", 
"other_unresolved", "pps_cabinet", "pps_nsencabinet", "pps_parlquota", 
"pps_sencabinet", "tps_autonomy", "tps_devolution"), class = "factor"), 
    year = c(2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
    2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006), heat = c(0, 
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0)), .Names = c("variable", 
"year", "heat"), class = "data.frame", row.names = c(7L, 86L, 
165L, 244L, 323L, 402L, 481L, 560L, 639L, 718L, 797L, 876L, 955L, 
1034L, 1113L, 1192L))) 

그래프 1

enter image description here

데이터 2

df2 <– as.data.frame(structure(list(variable = structure(c(11L, 14L, 12L, 13L, 4L, 
3L, 2L, 1L, 16L, 15L, 8L, 6L, 7L, 9L, 5L, 10L), .Label = c("eps_commission", 
"eps_company", "mps_armyint", "mps_milcmd", "other_constitution", 
"other_parlelect", "other_preselect", "other_proprep", "other_referendum", 
"other_unresolved", "pps_cabinet", "pps_nsencabinet", "pps_parlquota", 
"pps_sencabinet", "tps_autonomy", "tps_devolution"), class = "factor"), 
    year = c(1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 
    1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999), heat = c(1, 
    1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 1)), .Names = c("variable", 
"year", "heat"), class = "data.frame", row.names = c(11L, 90L, 
169L, 248L, 327L, 406L, 485L, 564L, 643L, 722L, 801L, 880L, 959L, 
1038L, 1117L, 1196L))) 

그래프 2

enter image description here

답변

1

data.frame 나를 위해 작동하지 않습니다,하지만 난 문제를 볼 수 있습니다 생각 만들기위한 귀하의 코드입니다. data.frame df에서 변수 "열"의 클래스를 검사 할 때 숫자 또는 요소입니까? 숫자라고 생각합니다. 내가 열거 한 것과 비슷한 샘플 data.frame을 만들고 "열"변수를 숫자 데이터로 남겨 두었을 때 그래프는 게시 한 것과 같습니다. 나는이 한 때 :

df$heat <- factor(df$heat, levels = c(-1, 0, 1)) 

를 다음 그래프를 만들기위한 조각을 실행, 값은 올바르게 매핑하는 것, 즉 "0"값은 회색이었고, "1"값은 녹색이었다. 나는 ggplot2가 만나는 가장 낮은 값을 scale_fill_manual 값에 나열된 첫 번째 색으로 매핑한다고 생각합니다.

+0

우수. 단순히 as.factor (열)를 ggplot 명령에 넣는 것만으로는 충분하지 않았습니다. 많은 thx. – zoowalker

관련 문제