Controlling legends and annotations

136 views
Skip to first unread message

Ross Boylan

unread,
May 16, 2013, 3:09:10 PM5/16/13
to ggp...@googlegroups.com
I'm having trouble controlling legends and adding annotations to some
graphs:

p <- ggplot(NHBS, aes(x=Age, y=Rate, group=Race, colour=Race))
p1 <- p+geom_line()+labs(title="MSM HIV+ Prevalence by Race x Age, 2004, 2008, 2011 NHBS LA")
# first graph fine
plot(p1)
p2 <- p1+geom_linerange(aes(ymin=Lower, ymax=Upper, size=2, alpha=0.5), position=position_jitter(h=0, w=0.5))+geom_point(aes(size=Negative+Positive))
# 2nd graph legends not so good
plot(p2)
dev.off()
# Could not get next one to work well
p3 <- p2+
geom_text(aes(x=0.5, y=1, label="Courtesy of TA Bingham. Not for redistribution.", colour="black"))+
geom_text(aes(x=0.5,y=0.9, label="Bars are exact 95% profile likilihood CI's.", colour="black"))

The legend for the second graph looks like this
2
<series of increasingly large boxes with numbers next to them>

0.5
<line labeled as 0.5>

Race
<colored lines with race name>

The first 2 groups seem to be labelled with the constants for size in
the first aes, and alpha.
The boxes under 2 probably reflect the range of sizes in the second aes.
The handling of Race is just what I want.

What I would like to show is a legend for size in the second aes,
with circles as the accompanying graphic. The points do appear as
circles in the figure.

As for the 3rd plot I'm looking for a way to get a legend in. Optimally,
it would appear below the graph, and the main graph would not take up
the entire vertical space. Googling revealed some methods that involved
graphics primitives from base R, but a) isn't there a way to do it
within ggplot2? and b) it's not really sufficient to overlay the plot,
since I want to shrink it.

Thanks.
Ross Boylan

Ito, Kaori (Groton)

unread,
May 16, 2013, 3:18:01 PM5/16/13
to Ross Boylan, ggp...@googlegroups.com
Dear Ross,
Could you provide the dataset (NHBS) so that we can see what is going on? Or, could you provide any toy example dataset which able to mimic your problem? It is hard to understand the problem unless seeing the plots....
Kaori
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Ross Boylan

unread,
May 16, 2013, 4:02:42 PM5/16/13
to Ito, Kaori (Groton), ggp...@googlegroups.com
On 5/16/2013 12:18 PM, Ito, Kaori (Groton) wrote:
> Dear Ross,
> Could you provide the dataset (NHBS) so that we can see what is going on? Or, could you provide any toy example dataset which able to mimic your problem? It is hard to understand the problem unless seeing the plots....
> Kaori
Thank you for your interest.

I'll work on providing a toy. You'll notice the legend I'm trying to
add includes "Not for redistribution", so I can't send the original data :)

Does the list accept attachments?
Ross

Ross Boylan

unread,
May 16, 2013, 4:53:08 PM5/16/13
to Ito, Kaori (Groton), ggp...@googlegroups.com
Here's a self-contained example:

set.seed(3)
d <- expand.grid(Age=seq(20, 50, 10), Race=c("B","A","L"))
d$Rate <- d$Age/10 + rnorm(nrow(d))/2
delta <- rep(0, nrow(d))
delta[d$Race=="B"] <- 0.1
delta[d$Race=="A"] <- -0.1
d$Rate <- d$Rate + delta
d$Lower <- d$Rate-0.15
d$Upper <- d$Rate+0.15
d$N <- 5+rpois(nrow(d), 15)
p <- ggplot(d, aes(x=Age, y=Rate, group=Race, colour=Race))
p1 <- p+geom_line()+labs(title="MSM HIV+ Prevalence by Race x Age, 2004, 2008, 2011 NHBS LA")
# first graph fine
plot(p1)
p2 <- p1+geom_linerange(aes(ymin=Lower, ymax=Upper, size=2, alpha=0.5), position=position_jitter(h=0, w=0.5))+geom_point(aes(size=N))
# 2nd graph legends not so good
plot(p2)

# Could not get next one to work well
p3 <- p2+
geom_text(aes(x=0.5, y=1, label="Courtesy of TA Bingham. Not for redistribution.", colour="black"))+
geom_text(aes(x=0.5,y=0.9, label="Bars are exact 95% profile likilihood CI's.", colour="black"))
plot(p3)


On 5/16/2013 12:18 PM, Ito, Kaori (Groton) wrote:
> Dear Ross,
> Could you provide the dataset (NHBS) so that we can see what is going on? Or, could you provide any toy example dataset which able to mimic your problem? It is hard to understand the problem unless seeing the plots....
> Kaori
>
> -----Original Message-----
> From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Ross Boylan
> Sent: Thursday, May 16, 2013 3:09 PM
> To: ggp...@googlegroups.com
> Subject: Controlling legends and annotations
>
> I'm having trouble controlling legends and adding annotations to some
> graphs:
> [snip]

Janesh Devkota

unread,
May 16, 2013, 5:07:01 PM5/16/13
to Ross Boylan, Ito, Kaori (Groton), ggp...@googlegroups.com
I am not sure what are you guys trying to achieve here. But I have looked at the p3 part from Ross . 

This might help :

p3 <- p2+
  geom_text(aes(x=30.5, y=1, label="Courtesy of TA Bingham.  Not for redistribution."), colour="black")+
  geom_text(aes(x=30.5,y=0.7, label="Bars are exact 95% profile likilihood CI's."), colour="black")
plot(p3)


--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

--- You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.

Ito, Kaori (Groton)

unread,
May 16, 2013, 5:20:04 PM5/16/13
to Ross Boylan, ggp...@googlegroups.com
Maybe something like this? If you want a legend simply at the bottom, you can say legend.position="bottom".

p2 <- p1+geom_linerange(aes(ymin=Lower, ymax=Upper), size=2, alpha=0.5)+
geom_point(aes(size=N)) + theme(legend.position=c(1, 0.6), legend.justification=c(1, 1))

p3 <- p2+
annotate("text", x=20, y=1, hjust=0,size=3, label="Courtesy of TA Bingham. Not for redistribution.", colour="black")+
annotate("text", x=20, y=0.8, hjust=0,size=3, label="Bars are exact 95% profile likilihood CI's.", colour="black")
plot(p3)

Ross Boylan

unread,
May 16, 2013, 5:21:37 PM5/16/13
to Janesh Devkota, Ito, Kaori (Groton), ggp...@googlegroups.com
On 5/16/2013 2:07 PM, Janesh Devkota wrote:
I am not sure what are you guys trying to achieve here. But I have looked at the p3 part from Ross . 

This might help :

p3 <- p2+
  geom_text(aes(x=30.5, y=1, label="Courtesy of TA Bingham.  Not for redistribution."), colour="black")+
  geom_text(aes(x=30.5,y=0.7, label="Bars are exact 95% profile likilihood CI's."), colour="black")
plot(p3)
That's a definite improvement.  Oddly, the lettering doesn't register as pure black; some of the letters seem to have a small red or green halo around them, or to have colored parts (e.g., the 2nd vertical line in the N of Not for redistribution is a dark red).
In terms of those notes, I'd really like them to appear completely below the area of the graph; that means the graph would not take the whole vertical page.

My other problem is the legends are wrong/not useful for the 2nd or 3rd plot (they have the same legends).  My original message (below) had the particulars.
Ross

Ross Boylan

unread,
May 16, 2013, 5:38:42 PM5/16/13
to Ito, Kaori (Groton), ggp...@googlegroups.com
On 5/16/2013 2:20 PM, Ito, Kaori (Groton) wrote:
> Maybe something like this? If you want a legend simply at the bottom, you can say legend.position="bottom".
>
> p2 <- p1+geom_linerange(aes(ymin=Lower, ymax=Upper), size=2, alpha=0.5)+
> geom_point(aes(size=N)) + theme(legend.position=c(1, 0.6), legend.justification=c(1, 1))
That got the legends I wanted. They key difference seems to be that I
size and alpha inside the first aes, while you have it outside, i.e.,
previous: geom_linerange(aes(ymin=Lower, ymax=Upper, size=2, alpha=0.5)
corrected: geom_linerange(aes(ymin=Lower, ymax=Upper), size=2, alpha=0.5)

I'm not sure what general principle to extract. I thought all the
aesthetics went in the aes function. Could anyone elaborate on what's
going on?

I wasn't trying to move the legends; it's the annotations below this
sentence I wanted to relocate.

Ross

Ito, Kaori (Groton)

unread,
May 16, 2013, 6:02:09 PM5/16/13
to Ross Boylan, ggp...@googlegroups.com
If you put something (color, shape, size, linetype..) in aes, it is mapping and corresponding legend(s) is created.
I thought you wanted to have different size point which reflecting N (in the dataset), so I put
geom_point(aes(size=N)). If you put "size" argument outside of aes, then it is setting the size of point, so you need to say size=2, etc. (not N).
If you want to add a simple text (like your example), I think annotate() is better. geom_text() can also add text on the graph, but x and y should come from the dataset. If you force to x and y locations to some values in geom_text(), it repeats adding the same layer again and again. Try run the example below and see the difference. (If you are fine with opaque "black", then this may doesn't matter much, but just wanted to illustrate the difference)

Data <- data.frame(x=rnorm(100), y=rnorm(100))
ggplot(Data, aes(x=x, y=y)) + geom_point(shape=1) +
annotate("text", x=0, y=0, label="test", alpha=0.2)+
geom_text(x=0, y=1, label="test", alpha=0.2)

Kaori
Reply all
Reply to author
Forward
0 new messages