web-dev-qa-db-fra.com

Comment bien annoter un ggplot2 (manuel)

En utilisant ggplot2 J'utilise normalement geom_text et quelque chose comme position=jitter pour annoter mes tracés.

Cependant - pour une belle intrigue, je trouve souvent utile d'annoter manuellement. comme ci-dessous:

data2 <- structure(list(type = structure(c(5L, 1L, 2L, 4L, 3L, 5L, 1L, 
2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L), .Label = c("EDS", 
"KIU", "LAK", "MVH", "NA*"), class = "factor"), value = c(0.9, 
0.01, 0.01, 0.09, 0, 0.8, 0.05, 0, 0.15, 0, 0.41, 0.04, 0.03, 
0.52, 0, 0.23, 0.11, 0.02, 0.64, 0.01), time = c(3L, 3L, 3L, 
3L, 3L, 6L, 6L, 6L, 6L, 6L, 15L, 15L, 15L, 15L, 15L, 27L, 27L, 
27L, 27L, 27L), year = c(2008L, 2008L, 2008L, 2008L, 2008L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2006L, 2006L, 2006L, 2006L, 2006L)), .Names = c("type", "value", 
"time", "year"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 
11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 24L), class = "data.frame")
ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw()+
annotate("text", x=6, y=0.9, label="this is a wrong color")+
annotate("text", x=15, y=0.6, label="this is a second annotation with a wrong color")

Le problème est que je ne peux pas obtenir la couleur des annotations de texte pour correspondre à la couleur de la ligne. Je suppose que je pourrais résoudre ce problème avec une échelle manuelle, mais j'espère qu'il existe une meilleure solution?

42
Andreas

Si vous utilisez geom_text () au lieu d'annotate (), vous pouvez passer une couleur de groupe à votre tracé:

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(aes(7, .9, label="correct color", color="NA*")) +
geom_text(aes(15, .6, label="another correct color!", color="MVH")) 

Donc, en utilisant annotate (), cela ressemble à ceci: texte alternatif http://www.cerebralmastication.com/wp-content/uploads/2010/03/before.png

puis après avoir utilisé geom_text (), cela ressemble à ceci: texte alternatif http://www.cerebralmastication.com/wp-content/uploads/2010/03/after.png

48
JD Long

J'ai eu un problème similaire et l'ai résolu avec la réponse JD Long. Mais à la suite de la mise à jour de ggplot2 Vers la version 0.9.0, j'ai remarqué que tous les appels geom_text() étaient quelque peu flous sur les tracés.

Grâce à kohske j'ai découvert que ce code

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
geom_line()+
geom_point()+
theme_bw() +
geom_text(aes(7, .9, label="correct color", color="NA*")) +
geom_text(aes(15, .6, label="another correct color!", color="MVH")) 

trace le geom_text nrow(data2) fois!

La manière correcte de fournir des données à geom_text consiste à créer un autre data.frame contenant les coordonnées, les étiquettes et les couleurs des chaînes que vous souhaitez tracer:

data2.labels <- data.frame(
  time = c(7, 15), 
  value = c(.9, .6), 
  label = c("correct color", "another correct color!"), 
  type = c("NA*", "MVH")
  )

ggplot(data2, aes(x=time, y=value, group=type, col=type))+
  geom_line()+
  geom_point()+
  theme_bw() +
  geom_text(data = data2.labels, aes(x = time, y = value, label = label))
64
mbask