2016-10-31 3 views
4

ggplot 막대 그림의 불투명도를 변경하려고합니다. 다음의 예를 생각해geom_bar 객체의 일부에서 불투명도 변경

df <- data.frame(period = rep(c("current", "past"), 3), 
       type = c("so_far", "so_far", "source_x", "source_x", "source_y", "source_y"), 
       income = c(12, 10, 7, 9, 4, 7)) 
ggplot(df, aes(x = period, y = income, fill = type)) + geom_bar(stat = "identity") 

이 다음과 같은 플롯을 제공합니다 현실에서 enter image description here

을 과거에 그들이 진정한 가치있는 동안 현재 기간 source_x 및 source_y에 대한 추정치하지만. 그래서 왼쪽 사이드 바의 파란색과 녹색 부분의 불투명도를 변경하고 싶습니다. 이 작업을 수행 할 수 있습니까? 방법?

답변

6

견적이 포함 된 막대 섹션을 강조 표시 할 때 점선 테두리가 더 효과적 일 수 있습니다. 아래 코드에서 선 종류와 알파 미학을 모두 사용하여 예상되는 두 개의 막대 부분을 표시했습니다. 전설을 뒤집어 색상 순서가 누적 막대의 색상 순서와 일치하도록했습니다.

library(ggplot2) 

ggplot(df, aes(x = period, y = income, fill = type, 
       linetype=ifelse(grepl("current", period) & grepl("source", type), "B", "A"), 
       alpha=ifelse(grepl("current", period) & grepl("source", type), 0, 1))) + 
    geom_bar(stat = "identity", lwd=0.5, colour="grey40") + 
    scale_alpha(range=c(0.5,1)) + 
    theme_bw() + 
    guides(alpha=FALSE, linetype=FALSE, 
     fill=guide_legend(reverse=TRUE)) 

enter image description here