geom_text font is ugly in histograms

1,025 views
Skip to first unread message

Raivo Kolde

unread,
Jun 16, 2009, 6:24:56 AM6/16/09
to ggp...@googlegroups.com
Hi

I just encountered a wierd bug. Usually geom_text works fine, but when
annotating histograms, the font turns somehow really ugly. As a small
reproducible example you might try this:

qplot(x = cyl, geom = "bar", data = mtcars) + geom_text(aes(x = 6, y
= 10, label = "Ugly font"))

I also added a pdf with the example.

Best,
Raivo

example.pdf

baptiste auguie

unread,
Jun 16, 2009, 6:49:07 AM6/16/09
to Raivo Kolde, ggp...@googlegroups.com
Weird! Here's a more minimal example,

library(ggplot2)
p <- # minimal example
qplot(0,0, geom="blank")+
geom_text(aes(0,0),label="Ugly text", size=10)
p # fine

p %+% mtcars # adding data makes it ugly ???


sessionInfo()
R version 2.9.0 (2009-04-17)
i386-apple-darwin8.11.1

locale:
en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] datasets stats grid graphics grDevices utils
methods base

other attached packages:
[1] baptMisc_1.0 constants_1.0 ggplot2_0.8.3
reshape_0.8.2 plyr_0.1.8 proto_0.3-8
latticeExtra_0.5-4 lattice_0.17-22
[9] fortunes_1.3-6 Cairo_1.4-4 RColorBrewer_1.0-2
--
_____________________________

Baptiste Auguié

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

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

baptiste auguie

unread,
Jun 16, 2009, 8:11:50 AM6/16/09
to Raivo Kolde, ggp...@googlegroups.com
I think I've found a plausible cause for this: several labels seem to
get overlaid on top of each other, and presumably the aliasing
produces some Moiré effect that messes up the edges. See the Grid call
below (rendered with quartz anyway),

library(ggplot2)
p <- # minimal example
qplot(0,0, geom="blank")+
geom_text(aes(0,0),label="Ugly text", size=10)
p # fine

p2 <-
p %+% mtcars # ugly ???


g <- # store the plot as a grob
ggplotGrob(p)
g2 <- # ugly version
ggplotGrob(p2)

str(getGrob(g,"texts", grep = T)) # one label
str(getGrob(g2,"texts", grep = T)) # 32 labels stacked on top of each other

grid.newpage()
grid.draw(textGrob("Ugly text", gp=gpar(cex=5))) # looks fine
grid.newpage()
l_ply(1:32, function(.) grid.draw(textGrob("Ugly text",
gp=gpar(cex=5)))) #looks ugly

So, returning to the initial example,

qplot(x = cyl, geom = "bar", data = mtcars) +
annotate("text", x = 6, y= 10, label = "Ugly font")

# or

qplot(x = cyl, geom = "bar", data = mtcars) +
geom_text(data=data.frame( x = 6, y= 10), map=aes(x=x, y=y), label =
"Ugly font")


seem to work fine.

HTH,

baptiste

hadley wickham

unread,
Jun 16, 2009, 8:33:10 AM6/16/09
to baptiste auguie, Raivo Kolde, ggp...@googlegroups.com
On Tue, Jun 16, 2009 at 7:11 AM, baptiste auguie<bapt...@googlemail.com> wrote:
>
> I think I've found a plausible cause for this: several labels seem to
> get overlaid on top of each other, and presumably the aliasing
> produces some Moiré effect that messes up the edges. See the Grid call
> below (rendered with quartz anyway),

Exactly - you get one label for every row of the original dataset.

Hadley

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

Reply all
Reply to author
Forward
0 new messages