2017-11-20 3 views
1

ggplot의 컬러 스케일 그라디언트 적용에 대한 질문이 있습니다. 나는 응답 변수가 양수와 음수를 포함하는 연속 변수이고 독립 변수가 다수의 독립적 인 사이트 인 데이터 셋을 가지고있다. 나는 모든 데이터를 배경으로 플롯 할 수 있고 데이터의 음의 범위를 다루는 응답 데이터에 컬러 스케일 그라디언트를 적용 할 수있는 방식으로 데이터를 플롯하려합니다. 이것은 지금까지 실제 데이터 세트의 구조를 모방 한 예제 데이터 세트로 내가 가진 것입니다.데이터 부분에 ggplot 컬러 스케일 그라디언트 적용

tr_sim <- data.frame(site_id = seq(1,100,1), estimated_impact = 
    rnorm(100,18,200), impact_group = rep(c(1,2),each = 50)) 

    rng_full <- range(tr_sim$estimated_impact) 
    #produce plot showing the full range of impacts across all sites and then 
    over the subsetted sites 

    impact_plot_full <- ggplot(data = tr_sim, aes(x = factor(site_id, levels = 
    site_id[order(estimated_impact)]), y = estimated_impact)) + 
    geom_bar(stat = "identity",width = 1, fill = "grey80") 

    impact_plot_full 

    impact_plot_full + 
    geom_bar(stat = "identity", width = 1, position = "stack", aes(y = 
    estimated_impact[impact_group == 1])) + 
    scale_fill_gradient2(low="firebrick", mid="yellow", high = "green4") + 
    labs(y = "Estimated Impact ($/week)", x = "Total number of sites with estimate 
    is 100", title = "Sites with the greatest impact after coverage loss") + 
    theme(axis.text.x = element_blank()) + 
    scale_y_continuous(breaks = 
    round(seq(rng_full[1],rng_full[2],by=100),digits=0)) 

나는 회색 배경에 모든 데이터를 플롯 할 수 있습니다 그리고이 위에 응답 데이터의 부정적인 범위를 플롯을 시도하고있다. '미학은 길이가 1이거나 데이터 (100), y, x'와 동일해야한다는 오류가 발생합니다. 음수 데이터가 전체 데이터 집합과 길이가 같지 않기 때문에 발생하는 것으로 알고 있지만이를 수행 할 방법을 찾을 수는 없습니다. 어떤 제안이라도 대단히 감사하겠습니다.

당신은 데이터를 부분 집합 및 geom_baraes()fill을 사용할 필요가 커티스

답변

0

, 감사합니다.

impact_plot_full + 
geom_bar(data = subset(tr_sim, estimated_impact < 0), 
     stat = "identity", 
     aes(y = estimated_impact, fill = estimated_impact)) + 
scale_fill_gradient2(low = "firebrick", mid = "yellow", high = 
"green4") + 
theme(axis.text.x = element_blank()) + 
xlab("site_id") 

enter image description here

희망이 당신을 위해 무엇을 찾고 있습니다.

+0

감사합니다 @ M.A., 훌륭한 작품입니다. 나는 당신의 대답에 +1을 주려고했으나 내 rep 점수가 너무 낮습니다. – Curtis

관련 문제