I am currently attempting to use ggplot to create a bar chart with a single bar that is partially transparent.
I have the following code:
dt1 <- data
First you put the alpha
inside the aes
as suggested by @jazzurro. However, you should use factor
for this to get a discrete scale. Then you can manually adjust the alpha scale.
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val, fill=x, alpha=factor(alphayr)), stat="identity") +
scale_x_continuous(breaks=dt1$yr) +
scale_alpha_manual(values = c("0.5"=0.5, "1"=1), guide='none')
An instructive question and answer. Other readers may not use data.table
syntax and may want to see the result, so I simply revised @shadow's answer to create a factor with a data frame, and display the plot below.
dt1 <- data.frame(yr=c(2010,2010,2011,2011), val=c(1500,3000,2000,1100), x=c("a","b","a","b"))
create the factor
dt1$alphayr <- as.factor(ifelse(dt1$yr == "2011", 0.5, 1))
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val, fill=x, alpha=factor(alphayr)), stat="identity") +
scale_x_continuous(breaks=dt1$yr) +
scale_alpha_manual(values = c("0.5"=0.5, "1"=1), guide='none')