solid and hollow symbols

339 views
Skip to first unread message

baptiste auguie

unread,
Mar 29, 2009, 7:15:21 AM3/29/09
to ggp...@googlegroups.com
Dear all,

I'm facing a minor aesthetic challenge in the attached graph.

load("d.rda")
dl <- subset(d2, a0 == "3" & ar %in% c(1, 1.6, 2.2))

p <-
ggplot(data = subset(d2, ar < 2.4), mapping = aes(x = value, y = int))+
stat_smooth( method = "lm", colour="grey") +
scale_shape("polarisation\naxis",labels=c("long", "short"), solid=F)+
geom_point(mapping=aes(shape=factor(variable), colour=a0))+
geom_text(data=dl,hjust=1.5, vjust=0, size=2,
mapping=aes(label=factor(ar), colour=a0))+
scale_x_continuous(breaks=10^-seq(8, 4, by=-1), trans="log10",
expression(paste(V/L, " (", mu*m^3, ")"))) +
scale_y_continuous(breaks=10^-seq(7, 3, by=-1),trans="log10",
expression(integral(sigma[ext]*(lambda)*d*lambda,0, infinity )* "
("*mu*m^3*")")) +
scale_colour_brewer("equ. radius\n / nm", palette="Set1")+theme_bw()

p

I'd like to specify symbol shapes that vary as "ar", but for each
shape there would be a hollow symbol for variable =="long" and a full
symbol for variable == "short". Hope this makes sense...

Best wishes,

baptiste

--

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________

Picture 4.png
d.rda

Mike Lawrence

unread,
Mar 29, 2009, 7:44:12 AM3/29/09
to baptiste auguie, ggp...@googlegroups.com
Instead of scale_shape, use scale_shape_manual and explicitly specify
the shape values you want:

df=data.frame(x=1:10,y=1:10,z=factor(c(1,1,1,1,1,2,2,2,2,2)))
ggplot(df,aes(x=x,y=y,shape=z))+geom_point()+scale_shape_manual(values=c(1,16))
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tinyurl.com/mikes-public-calendar

~ Certainty is folly... I think. ~

baptiste auguie

unread,
Mar 29, 2009, 8:03:15 AM3/29/09
to Mike Lawrence, ggp...@googlegroups.com
Hi, and thanks for your reply.

I've tried this,

d=data.frame(x=1:8,y=1:8, z1=factor(c(1:4)), z2=factor(rep(c("a",
"b"), each=4)))

d$z <- factor(paste(d$z1, d$z2)) # 8 symbols in total

ggplot(d,aes(x=x,y=y,shape=z))+geom_point()+
scale_shape_manual(values=c(rev(c(0, 1, 2, 5)), 15:18)) # select
identical shapes with and without color

however, I would like two different scales: one corresponding to the
shape (factor z1), and another corresponding to the full / hollow
character (factor z2).

I'm guessing this might require writing a new scale though.

The symbols 22 to 24 might do the job if I were able to map the fill
colour to the factor z2, but I fail to get this to work,

ggplot(d,aes(x=x,y=y,shape=z, fill=z2))+geom_point()+
scale_shape_manual(values=rep(22:24))

Any idea?

Thanks,

baptiste

baptiste auguie

unread,
Mar 29, 2009, 9:22:40 AM3/29/09
to ggp...@googlegroups.com
The more I think about this, i can see an opportunity to design a new
type of geom and scales. The TeachingDemos package provides the
concept of an arbitrary polygon to be used as a symbol in a plot,

my.symbols( 1:10, runif(10), ms.male, add=FALSE, xlab='x',
ylab='y', inches=0.3, col=c('blue','green'), xlim=c(0,11), ylim=c(-0.1,1.1))
my.symbols( (1:10)+0.5, runif(10), ms.female, add=TRUE, inches=0.3,
col=c('red','green') )

One could think of a situation where the symbol shape is varied
according to a factor or continuous variable (say, small ellipses of
varying aspect ratio), not only the size of it as currently done in
ggplot2. Similarly, my initial problem would simply translate in the
filling colour of the symbols mapped to a factor.

Would it be easy to create a geom_symbol() similar to geom_point() but
for which the point is an arbitrary grob ?

Best wishes,

hadley wickham

unread,
Mar 29, 2009, 11:17:03 AM3/29/09
to baptiste auguie, ggp...@googlegroups.com
On Sun, Mar 29, 2009 at 8:22 AM, baptiste auguie
<bapt...@googlemail.com> wrote:
>
> The more I think about this, i can see an opportunity to design a new
> type of geom and scales. The TeachingDemos package provides the
> concept of an arbitrary polygon to be used as a symbol in a plot,
>
>  my.symbols( 1:10, runif(10), ms.male, add=FALSE, xlab='x',
> ylab='y', inches=0.3, col=c('blue','green'), xlim=c(0,11), ylim=c(-0.1,1.1))
> my.symbols( (1:10)+0.5, runif(10), ms.female, add=TRUE, inches=0.3,
> col=c('red','green') )
>
> One could think of a situation where the symbol shape is varied
> according to a factor or continuous variable (say, small ellipses of
> varying aspect ratio), not only the size of it as currently done in
> ggplot2. Similarly, my initial problem would simply translate in the
> filling colour of the symbols mapped to a factor.
>
> Would it be easy to create a geom_symbol() similar to geom_point() but
> for which the point is an arbitrary grob ?

That's been sitting on my to do list for a long time, but I've never
needed it so I haven't made any progress on it. The drawing part
should be pretty easy - as always, it's the legends that are really
hard. If you want to have a go at writing your own geom, I'd
recommend looking at the source for geom_point. There's only one
method you really need to implement - draw().

Hadley


--
http://had.co.nz/

hadley wickham

unread,
Mar 29, 2009, 11:22:06 AM3/29/09
to baptiste auguie, Mike Lawrence, ggp...@googlegroups.com
On Sun, Mar 29, 2009 at 7:03 AM, baptiste auguie
<bapt...@googlemail.com> wrote:
>
> Hi, and thanks for your reply.
>
> I've tried this,
>
>        d=data.frame(x=1:8,y=1:8, z1=factor(c(1:4)), z2=factor(rep(c("a",
> "b"), each=4)))
>
>        d$z <- factor(paste(d$z1, d$z2)) # 8 symbols in total
>
>        ggplot(d,aes(x=x,y=y,shape=z))+geom_point()+
>                scale_shape_manual(values=c(rev(c(0, 1, 2, 5)), 15:18)) # select
> identical shapes with and without color
>
> however, I would like two different scales: one corresponding to the
> shape (factor z1), and another corresponding to the full / hollow
> character (factor z2).
>
> I'm guessing this might require writing a new scale though.
>
> The symbols 22 to 24 might do the job if I were able to map the fill
> colour to the factor z2, but I fail to get this to work,
>
>        ggplot(d,aes(x=x,y=y,shape=z, fill=z2))+geom_point()+
>                scale_shape_manual(values=rep(22:24))

Like this?

ggplot(d,aes(x=x,y=y,shape=z1, fill=z2)) +
geom_point() +
scale_shape_manual(values = 22:25)

The default legend isn't very good though. You can hack around it with:

GeomPoint$default_aes <- function(.) aes(shape=22, colour="black",
size=2, fill = NA, alpha = 1)
ggplot(d,aes(x=x,y=y,shape=z1, fill=z2)) +
geom_point() +
scale_shape_manual(values = 22:25)

Hadley

--
http://had.co.nz/

baptiste auguie

unread,
Mar 29, 2009, 12:12:57 PM3/29/09
to hadley wickham, ggp...@googlegroups.com
fantastic, as usual!

many thanks,

baptiste

PS: i'd like to have a go at the custom grob idea, but since i'm
currently finishing up my thesis it'll probably have to wait for me as
well.

--

Reply all
Reply to author
Forward
0 new messages