transparency in a barchart plot / names are still opaque

86 views
Skip to first unread message

igreg

unread,
Oct 15, 2010, 10:09:29 AM10/15/10
to ggplot2
dear list members...


my example:


library(ggplot2)


d.frame <- data.frame(city=rep(c("Geneve","Basel","Luzern"),each=3),

variable=rep(c("read.books","watch.tv","shopping"),3),

mean.value=c(3.5,7.2,2.6,9.9,6.1,7.3,1.9,2.6,3.4),
trans=c(1,1,1,0.3,0.3,0.3,1,1,1))

d.frame$trans <- factor (d.frame$trans)


### plot with transparency (alpha=trans)

pl <- ggplot(d.frame, aes(x=city, y=mean.value,fill=variable,
alpha=trans))
pl <- pl + geom_bar(stat="identity",position = "dodge")
pl <- pl + geom_text(aes(label=((round(mean.value,
0)))),position=position_dodge(width=1), colour="black",
size=10,hjust=-0.3)
pl <- pl + coord_flip()
pl <- pl + scale_y_continuous("",limits=c(0,15))
pl <- pl + opts (axis.text.y = theme_text(size=20, hjust=1))
pl


################

my question:

why is the name of the city ("Basel") still plotted opaque?
The bars and the numbers next to the bars are transparent,
the name of the city is not.

any suggestions to solve my problems.

thanks a lot!

igreg

Hadley Wickham

unread,
Oct 15, 2010, 1:02:13 PM10/15/10
to igreg, ggplot2
> why is the name of the city ("Basel") still plotted opaque?
> The bars and the numbers next to the bars are transparent,
> the name of the city is not.

Because you are plotting it once for each row it appears in.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

igreg

unread,
Oct 15, 2010, 2:33:22 PM10/15/10
to ggplot2
thanks for your hint, Hadley!

only for a short test: i tried this

d.frame$city[c(4:5)] <- NA ### delete "Basel" twice

new data.frame looks like this
city variable mean.value trans
1 Geneve read.books 3.5 1
2 Geneve watch.tv 7.2 1
3 Geneve shopping 2.6 1
4 <NA> read.books 9.9 0.3
5 <NA> watch.tv 6.1 0.3
6 Basel shopping 7.3 0.3
7 Luzern read.books 1.9 1
8 Luzern watch.tv 2.6 1
9 Luzern shopping 3.4 1

plotted it again. But Basel is still not transparent...

Brandon Hurr

unread,
Oct 15, 2010, 2:47:51 PM10/15/10
to igreg, ggplot2
I think you're trying to apply an alpha transparency to axis text. Can you even do that? 


This is as close as I can get. 


library(ggplot2)


d.frame  <- data.frame(city=rep(c("Geneve","Basel","Luzern"),each=3),

variable=rep(c("read.books","watch.tv","shopping"),3),

mean.value=c(3.5,7.2,2.6,9.9,6.1,7.3,1.9,2.6,3.4),
                   trans=c(1,1,1,0.3,0.3,0.3,1,1,1))

d.frame$trans  <- factor (d.frame$trans)

d.frame$city[c(4:5)]  <- NA

ggplot(d.frame, aes(x=city, y=mean.value, fill=variable, alpha=trans))+
geom_bar(stat="identity",position = "dodge")+
geom_text(aes(label=((round(mean.value,0)))),position=position_dodge(width=1), colour="black", size=10,hjust=-0.3)+
geom_text(aes(x= city, y=12, label=city, size=12))+
scale_y_continuous("",limits=c(0,15))+
theme_bw()+
opts(axis.text.y = theme_blank())+
coord_flip()
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

switzerland.png

Brandon Hurr

unread,
Oct 15, 2010, 3:27:52 PM10/15/10
to igreg, ggplot2
Less complicated than I originally thought...

I subseted the data for geom_text() and plotted it. 

ggplot(d.frame, aes(x=city, y=mean.value, fill=variable, alpha=trans))+
geom_bar(stat="identity",position = "dodge")+
geom_text(aes(label=((round(mean.value,0)))),position=position_dodge(width=1), colour="black", size=10,hjust=-0.3)+
geom_text(data = d.frame[d.frame$variable == "read.books",], aes(x= city, y=12, label=city), size=12)+
scale_y_continuous("",limits=c(0,15))+
scale_fill_discrete('', breaks= c("read.books","watch.tv","shopping"), label = c("Reading", "Watch TV", "Shopping"))+
theme_bw()+
opts(axis.text.y = theme_blank())+
coord_flip()
switzerland.png

igreg

unread,
Oct 15, 2010, 5:39:46 PM10/15/10
to Brandon Hurr, ggplot2
„I think you're trying to apply an alpha transparency to axis text.“

Yes, that's exactly what I want, Brandon. :-)

"Can you even do that?"

I don't know. Maybe one of the ggplot experts in here knows a solution.

Thanks for your suggestion to plot the names of the cities on the right side
(works perfect). But i would prefer them on the left side (see attachment:
created by a friend with illustrator)

The idea behind this attempt with transparent variables is this: for a
powerpoint presentation i want to show differences over some variables.
First, i show them all opaque. In a second step only the significant
differences stay opaque. The rest gets transparent (blends into the
background).

But if it's not possible with ggplot, i'm thinking about another solution to
mark the significant variables within the plot. Any suggestions?

Thanks

igreg

switzerland-cities.jpg

Ista Zahn

unread,
Oct 15, 2010, 7:00:12 PM10/15/10
to igreg, Brandon Hurr, ggplot2
Hi,

On Fri, Oct 15, 2010 at 5:39 PM, igreg <zoor...@gmx.net> wrote:
> „I think you're trying to apply an alpha transparency to axis text.“
>
> Yes, that's exactly what I want, Brandon. :-)
>
> "Can you even do that?"
>
> I don't know. Maybe one of the ggplot experts in here knows a solution.

Yes, you can do that. I don't know if this is supported, but it works for me:

## Prepare the plot


pl <- ggplot(d.frame, aes(x=city, y=mean.value,fill=variable,
alpha=trans))
pl <- pl + geom_bar(stat="identity",position = "dodge")

pl <- pl + geom_text(aes(label=((round(mean.value,
0)))),position=position_dodge(width=1), colour="black",


size=10,hjust=-0.3)
pl <- pl + coord_flip()
pl <- pl + scale_y_continuous("",limits=c(0,15))

## Set y axis text theme, including alpha level (manually).
pl + opts (axis.text.y = theme_text(size=20, hjust=1,
colour=alpha("black", c(.3, 1, 1))))

## Or, if you must set the alpha level according to d.frame$trans:
pl + opts (axis.text.y = theme_text(size=20, hjust=1,
colour=alpha("black", daply(d.frame, .(city), function(x)
as.numeric(as.character((unique(x$trans))))))))


>
> Thanks for your suggestion to plot the names of the cities on the right side
> (works perfect). But i would prefer them on the left side (see attachment:
> created by a friend with illustrator)
>
> The idea behind this attempt with transparent variables is this: for a
> powerpoint presentation i want to show differences over some variables.
> First, i show them all opaque. In a second step only the significant
> differences stay opaque. The rest gets transparent (blends into the
> background).

Cool idea. I like it.

>
> But if it's not possible with ggplot, i'm thinking about another solution to
> mark the significant variables within the plot. Any suggestions?

I think the solution above will work for you. Let me know how it turns out!

-Ista

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

igreg

unread,
Oct 16, 2010, 4:54:16 AM10/16/10
to Ista Zahn, ggplot2
this is awesome, Ista

it works great.
You saved my weekend :-)

Thanks a lot!

On Monday I will adjust all my syntaxes..

Best. greg

Reply all
Reply to author
Forward
0 new messages