为整个facet_grid绘制一条垂直线?

时间:2011-11-07 03:33:14

标签: r graph ggplot2

我正在使用以下内容绘制数据集:

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(...)

在我的数据集中绘制Category和Type之间的网格图。但是,geom_vlinegeom_text会使垂直线和文字标签出现在每个子图上。有没有办法在整个情节中获得单个vline和文本元素?有什么建议吗?

数据集

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

脚本

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)

剧情

enter image description here

我想要什么

需要的是标签在整个图中只出现一次。

1 个答案:

答案 0 :(得分:5)

这是一个简单的例子:

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)

技巧是为每一层使用单独的数据集。

enter image description here