将标签放置到ggplot图表中

时间:2013-01-09 16:44:17

标签: r ggplot2

dput(pp)
structure(list(Team = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 
2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 
1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 
3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), Date = structure(c(1L, 1L, 2L, 
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 
12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 
14L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 
20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L, 26L, 
26L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L
), .Label = c("2012-02-23", "2012-02-24", "2012-02-27", "2012-02-28", 
"2012-02-29", "2012-03-01", "2012-03-02", "2012-03-05", "2012-03-06", 
"2012-03-07", "2012-03-08", "2012-03-09", "2012-03-12", "2012-03-13", 
"2012-03-14", "2012-03-15", "2012-03-16", "2012-03-19", "2012-03-20", 
"2012-03-21", "2012-03-22", "2012-03-23", "2012-03-26", "2012-03-27", 
"2012-03-28", "2012-03-29", "2012-03-30", "2012-04-02", "2012-04-03", 
"2012-04-04", "2012-04-05", "2012-04-06", "2012-04-09", "2012-04-10", 
"2012-04-11", "2012-04-12", "2012-04-13", "2012-04-16", "2012-04-17", 
"2012-04-18", "2012-04-19", "2012-04-20", "2012-04-23", "2012-04-24", 
"2012-04-25", "2012-04-26", "2012-04-27", "2012-04-30", "2012-05-01", 
"2012-05-02", "2012-05-03", "2012-05-04", "2012-05-07", "2012-05-08", 
"2012-05-09", "2012-05-10", "2012-05-11", "2012-05-14", "2012-05-15", 
"2012-05-16", "2012-05-17", "2012-05-18", "2012-05-21", "2012-05-22", 
"2012-05-23", "2012-05-24", "2012-05-25", "2012-05-28", "2012-05-29", 
"2012-05-30", "2012-05-31", "2012-06-01", "2012-06-04", "2012-06-05", 
"2012-06-06", "2012-06-07", "2012-06-08", "2012-06-11", "2012-06-12", 
"2012-06-13", "2012-06-14", "2012-06-15", "2012-06-18", "2012-06-19", 
"2012-06-20", "2012-06-21", "2012-06-22", "2012-06-25", "2012-06-26", 
"2012-06-27", "2012-06-28", "2012-06-29", "2012-07-02", "2012-07-03", 
"2012-07-04", "2012-07-05", "2012-07-06", "2012-07-09", "2012-07-10", 
"2012-07-11", "2012-07-12", "2012-07-13", "2012-07-16", "2012-07-17", 
"2012-07-18", "2012-07-19", "2012-07-20", "2012-07-23", "2012-07-24", 
"2012-07-25", "2012-07-26", "2012-07-27", "2012-07-30", "2012-07-31", 
"2012-08-01", "2012-08-02", "2012-08-03", "2012-08-06", "2012-08-07", 
"2012-08-08", "2012-08-09", "2012-08-10", "2012-08-13", "2012-08-14", 
"2012-08-15", "2012-08-16", "2012-08-17", "2012-08-20", "2012-08-21", 
"2012-08-22", "2012-08-23", "2012-08-24", "2012-08-27", "2012-08-28", 
"2012-08-29", "2012-08-30", "2012-08-31", "2012-09-03", "2012-09-04", 
"2012-09-05", "2012-09-06", "2012-09-07", "2012-09-10", "2012-09-11", 
"2012-09-12", "2012-09-13", "2012-09-14", "2012-09-17", "2012-09-18", 
"2012-09-19", "2012-09-20", "2012-09-21", "2012-09-24", "2012-09-25", 
"2012-09-26", "2012-09-27", "2012-09-28", "2012-10-01", "2012-10-02", 
"2012-10-03", "2012-10-04", "2012-10-05", "2012-10-08", "2012-10-09", 
"2012-10-10", "2012-10-11", "2012-10-12", "2012-10-15", "2012-10-16", 
"2012-10-17", "2012-10-18", "2012-10-19", "2012-10-22", "2012-10-23", 
"2012-10-24", "2012-10-25", "2012-10-26", "2012-10-29", "2012-10-30", 
"2012-10-31", "2012-11-01", "2012-11-02", "2012-11-05", "2012-11-06", 
"2012-11-07", "2012-11-08", "2012-11-09", "2012-11-12", "2012-11-13", 
"2012-11-14", "2012-11-15", "2012-11-16", "2012-11-19", "2012-11-20", 
"2012-11-21", "2012-11-22", "2012-11-23", "2012-11-26", "2012-11-27", 
"2012-11-28", "2012-11-29", "2012-11-30", "2012-12-03", "2012-12-04", 
"2012-12-05", "2012-12-06", "2012-12-07", "2012-12-10", "2012-12-11", 
"2012-12-12", "2012-12-13", "2012-12-14", "2012-12-17", "2012-12-18", 
"2012-12-19", "2012-12-20", "2012-12-21", "2012-12-24", "2012-12-25", 
"2012-12-26", "2012-12-27", "2012-12-28", "2012-12-31", "2013-01-01", 
"2013-01-02", "2013-01-03", "2013-01-04", "2013-01-07", "2013-01-08"
), class = "factor"), Seconds = c(-51388L, -51388L, -41357L, 
-41357L, -53787L, -53787L, -52401L, -52401L, -53850L, -53850L, 
-52555L, -52555L, -20996L, -20996L, -51169L, -51169L, -36027L, 
-36027L, 1260L, 1260L, -34407L, -34407L, -54489L, -54489L, -28192L, 
-28192L, -53124L, -53124L, -45377L, -45377L, -17748L, -17748L, 
-45122L, -45122L, -52681L, -52681L, -29807L, -29807L, -51305L, 
-51305L, -54618L, -54618L, 4487L, 4487L, -53112L, -53112L, -28269L, 
-28269L, -3408L, -3408L, -26260L, -26260L, -51686L, -51686L, 
-26149L, -26149L, -51637L, -51637L, -52347L, -52347L, -20492L, 
-20492L, -50541L, -50541L, -43557L, -43557L, -50862L, -50862L, 
-17465L, -17465L, -51538L, -51538L, -50680L, -50680L, -38414L, 
-38414L, -51690L, -51690L, -40387L, -40387L, -46609L, -46609L, 
-51753L, -51753L, -51318L, -51318L, -51072L, -51072L, -44116L, 
-44116L, -53716L, -53716L, -4488L, -4488L, -52479L, -52479L, 
-51851L, -51851L, -24389L, -24389L)), .Names = c("Team", "Date", 
"Seconds"), row.names = c(NA, 100L), class = "data.frame")

我喜欢使用facet_wrap为每个团队创建一个ggplot。在0上绘制红线并将标签放在图表之外,在0之下,如下所示:开启时间及0以上:图中所示为晚期。

这是我到目前为止所做的:

ggplot(pp, aes(Date, Seconds, group=Team, colour=Team)) + geom_point() + facet_wrap( ~ Team) + geom_hline(yintercept=0, colour="red")

enter image description here

1 个答案:

答案 0 :(得分:1)

一种解决方案是使用包grid.text()中的函数gridExtra

首先,在功能plot.margin=中使用参数theme()在地块右侧留出额外空间。

library(ggplot2)
library(gridExtra)
p<-ggplot(pp, aes(Date, Seconds, group=Team, colour=Team)) + geom_point() + 
  facet_wrap( ~ Team) + geom_hline(yintercept=0, colour="red") +
    theme(plot.margin = unit(c(1,3,1,1), "cm"),
        legend.position="none")

然后绘制它并添加带有函数grid.text()的文本,给出x和y坐标以及设置文本参数。

p
grid.text("Late",x = unit(0.9, "npc"), y = unit(0.85, "npc"), just = c("left", "bottom"), 
          gp = gpar(fontface = "bold", fontsize = 14))
grid.text("On Time",x = unit(0.9, "npc"), y = unit(0.75, "npc"), just = c("left", "bottom"), 
          gp = gpar(fontface = "bold", fontsize = 14))

enter image description here