1

I am plotting a dataset using the following:

plot =  ggplot(transData, aes(x=Time, y=Value)) + 
        scale_fill_brewer(palette="Set1") + 
        facet_grid(Category ~ .) + 
        geom_line(aes(group=Type,colour=Type, linetype=Type), size=1.5) +
        geom_vline(...) +
        geom_text(...)

which draws a grid plot between Category and Type in my dataset. However, the geom_vline and geom_text make the vertical line and text label appear on every sub plot. Is there a way to to get a single vline and text element on the entire plot? Any suggestions?

Dataset

Class$Time$Type$Total
Class1$283$Category1$1
Class1$276$Category1$1
Class1$260$Category1$1
Class1$450$Category1$1
Class1$572$Category1$9
Class1$667$Category1$9
Class1$535$Category1$10
Class1$579$Category1$10
Class1$522$Category1$12
Class1$231$Category1$12
Class1$774$Category1$13
Class1$7240$Category1$14
Class1$510$Category1$14
Class1$3863$Category1$14
Class1$954$Category1$15
Class1$455$Category1$15
Class1$644$Category1$15
Class1$1859$Category1$15
Class1$413$Category1$16
Class1$13$Category1$19
Class1$22$Category1$19
Class1$13$Category1$19
Class1$14$Category1$19
Class1$523$Category1$19
Class1$123$Category1$19
Class1$684$Category1$19
Class1$350$Category1$19
Class1$581$Category1$19
Class1$28$Category2$18
Class1$18$Category2$18
Class1$17$Category2$18
Class1$73$Category2$18
Class1$17$Category2$18
Class1$18$Category2$18
Class1$17$Category2$18
Class1$73$Category2$18
Class1$25$Category2$18
Class1$74$Category2$18
Class1$18$Category2$18
Class1$78$Category2$18
Class1$19$Category2$18
Class1$75$Category2$18
Class1$51$Category2$18
Class1$24$Category2$18
Class1$32$Category2$18
Class1$94$Category2$18
Class1$80$Category2$18
Class1$19$Category2$18
Class1$34$Category2$18
Class1$73$Category2$18
Class1$28$Category2$18
Class1$78$Category2$18
Class1$84$Category2$18
Class1$77$Category2$18
Class1$85$Category2$18
Class1$80$Category2$18
Class1$82$Category2$18
Class1$72$Category2$18
Class1$17$Category2$18
Class1$87$Category2$18
Class1$78$Category2$18
Class1$74$Category2$18
Class1$74$Category2$18

Script

library(ggplot2)

options(stringsAsFactors = FALSE)
rootdir = "./"
input = paste(rootdir, "Test.txt", sep="")

data = data.frame(Class=data$Class, Type=data$Type, Time=as.numeric(data$Time), Total=as.numeric(data$Total))

plot =  ggplot(data, aes(x=Time, y=Total)) + 
        scale_fill_brewer(palette="Set1") + 
        facet_grid(Type ~ Class) + 
        scale_x_log10() +
        geom_line(aes(group=Type,colour=Type, linetype=Type), size=1.5) + 
        geom_vline(aes(xintercept=1), linetype=2) +
        geom_vline(aes(xintercept=4), linetype=2) +
        geom_vline(aes(xintercept=8), linetype=2) +
        geom_vline(aes(xintercept=16), linetype=2) +
        geom_text(aes(1, 4, label="Label1")) +
        geom_text(aes(4, 3, label="Label2")) +
        geom_text(aes(8, 25, label="Label3")) +
        geom_text(aes(16, 2, label="Label4")) +


print(plot)

options(stringsAsFactors = TRUE)

Plot

enter image description here

What I want

What is want is that the labels appear only once across the entire plot.

Legend
  • 113,822
  • 119
  • 272
  • 400
  • 1
    Could you please attach a reproducible example? – kohske Nov 07 '11 at 03:40
  • I second kohske's request for more details. Perhaps you mean that you want to use `geom_vline` and `geom_text` to place elements only on a _single_ subplot? I'm just guessing, though... – joran Nov 07 '11 at 03:44
  • @kohske: Sorry. I will get a sample of that and post it in a few minutes. Thanks for helping me out. @joran: Yes. That's exactly what I want. However, getting it for `geom_text` alone is enough. It is ok if the `geom_vlines` occur all over. – Legend Nov 07 '11 at 03:52
  • In that case, this is very simple. The examples [here](http://had.co.nz/ggplot2/facet_grid.html) include instances of this, as well as countless previous SO questions. If you just start perusing questions on '[ggplot2] facet_grid' I'm sure you'll find it. Or I'm equally certain that someone will post an answer for you, since this question is like free rep for speedy `ggplot2`ers. Personally, I will abstain, having answered this question at least a few times already, I'm sure. – joran Nov 07 '11 at 04:01
  • then, probably this http://stackoverflow.com/questions/7903972/can-you-specify-different-geoms-for-different-facets-in-a-ggplot/7904134#7904134 is near from what you want to do. – kohske Nov 07 '11 at 04:04
  • @joran: I see. Interesting that I did not find what I was looking for on that page. I will search SO again. – Legend Nov 07 '11 at 04:09
  • @kohske: Thanks for the link. Am looking into it now. – Legend Nov 07 '11 at 04:10
  • I got these two links and I will wrestle with them :) http://stackoverflow.com/questions/7068665/r-ggplot2-manually-annotate-one-panel-w-multiple-labels and http://stackoverflow.com/questions/2417623/manual-annotate-a-ggplot-with-different-labels-in-different-facets – Legend Nov 07 '11 at 04:15

1 Answers1

6

here is a quick example:

dat.vline <- data.frame(Type = c("Category1", "Category2"), xp = c(1000, 4000))
dat.text <- data.frame(Type = c("Category1", "Category2"), x = c(1000, 4000), y = c(5, 10), label = c("hoge", "boke"))

ggplot(d, aes(Time, Total)) + 
  facet_grid(Type ~ .) + 
  geom_line(aes(group = Type, colour = Type, linetype = Type), size = 1.5) +
  geom_vline(aes(xintercept = xp), data = dat.vline) +
  geom_text(aes(x, y, label = label), data = dat.text)

the trick is using separate dataset for each layer.

enter image description here

kohske
  • 65,572
  • 8
  • 165
  • 155