Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot transparency on individual bar

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.table(yr=c(2010,2010,2011,2011),
                  val=c(1500,3000,2000,1100),
                  x=c("a","b","a","b"))


ggplot() + geom_bar(data=dt1, aes(x=yr, y=val,fill=x),stat="identity") +
  scale_x_continuous(breaks=dt1$yr)

This will create a simple chart with 2 columns with stacked data. I have tried the following code to adjust the 2011 value to have transparency, however I am not having much luck. Any pointers?

dt1[,alphayr:=ifelse(yr==2011,.5,1)]
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val,fill=x),stat="identity", alpha=dt1$alphayr) +
scale_x_continuous(breaks=dt1$yr)
like image 464
Dan Avatar asked Mar 11 '15 06:03

Dan


People also ask

How to create transparent barplot using Ggplot2 in R?

To create transparent barplot using ggplot2, we can use alpha argument inside geom_bar function. For example, if we have a data frame called df that contains a categorical column say x and a numerical column say count then the bar plot with transparency can be created by using the command ggplot (df,aes (x,y))+geom_bar (alpha=0.1,stat="identity")

How to improve your ggplot bar charts?

Ordering your bar charts make sense in case the categorical value has no internal order and helps focusing on the largest and smallest groups. In addition, one can highlight specific bars with use of custom colors. It is pretty easy to improve your ggplot with a few lines of code.

What are the aesthetic mappings for a ggplot bar graph?

I know this can sound a bit theoretical, so let’s review the specific aesthetic mappings you’ve already seen as well as the other mappings available within geom_bar. The main aesthetic mappings for a ggplot bar graph include: aesthetic mappings. We’ve also seen applied as a parameter to change the outline of the bars in the prior example.

What is �ggplot in R?

ggplot is a package for creating graphs in R, but it’s also a method of thinking about and decomposing complex graphs into logical subunits. ggplot takes each component of a graph–axes, scales, colors, objects, etc–and allows you to build graphs up sequentially one component at a time.


2 Answers

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')
like image 200
shadow Avatar answered Oct 18 '22 05:10

shadow


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')

enter image description here

like image 30
lawyeR Avatar answered Oct 18 '22 04:10

lawyeR