将标题添加到ggplot2图中,因为标题显示在facet_wrap图中

时间:2015-03-09 13:11:06

标签: r ggplot2

可以将标题添加到单个地块中,例如:

library(ggplot2)
gdURL <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
gDat <- read.delim(file = gdURL)
jDat <- droplevels(subset(gDat, continent != "Oceania"))
str(jDat)

jYear <- 2007
q <- ggplot(subset(jDat, year == jYear),
            aes(x = gdpPercap, y = lifeExp)) + scale_x_log10()
q + geom_point()

q + geom_point(aes(size = sqrt(pop/pi)), pch = 21)

enter image description here

在这样的facet_wrap情节中显示标题(在灰色框中并与图集合):

r <- q +
   geom_point(aes(size = sqrt(pop/pi)), pch = 21, show_guide = FALSE) +
   scale_size_continuous(range=c(1,40))

r <- r + facet_wrap(~ continent)
r + aes(fill = continent)

enter image description here

1 个答案:

答案 0 :(得分:2)

根据@jraab评论,实现此目的的一种方法是添加一个虚拟列并使用facet_grid

jDat$dummy <- NA

ggplot(subset(jDat, year == jYear), aes(x = gdpPercap, y = lifeExp)) +
  scale_x_log10() +
  geom_point() +
  facet_grid(. ~ dummy)

如果您想操纵标题,可以使用:

facet_grid(. ~ dummy, labeller=label_bquote("My New Title"))

Plot