r - geom_text and facets not working -
data file way big dput
hoping able identify error. here story: produced following small multiples plot using ggplot following code. working fine.
ggplot(sm.df, aes(year, n, group = 1)) + geom_line(aes(colour = top10)) + facet_wrap( ~ shorttitle, ncol=7) + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.text.x = element_text(angle=45, hjust=1, size =8)) + scale_x_discrete(breaks=seq(1989, 2020, 8)) + ylim(0, 150)
now, want annotate each facet overall n. created vector containing x position, y position, , actual values plotted. so, basically, in same x , y position on every plot, different label.
'data.frame': 78 obs. of 3 variables: $ x1 : num 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 12.5 ... $ y1 : num 130 130 130 130 130 130 130 130 130 130 ... $ n.label: int 1650 1456 1348 1216 1086 1035 985 940 888 826 ...
but, when add geom_text(data = x.df, aes(x = x1, y = y1, label = n.label), inheret.aes=false)
plot, appears though every label being reprinted on every facet. doing wrong.
i have poured through on how annotate faceted plots, missing problem. appreciated if have ideas -- not straightforward produce data, guess if need to.
this answer similar question should help. there needs identifier identifies facet print label on. in case, should use shorttitle
purpose.
this modified example using mtcars
, both faceting , labeling according number of gears
library(ggplot2) xpos <- c(10,10,10) ypos <- c(inf,inf,inf) lab <- c(378,2,50) gears <- c(3:5) ldata <- data.frame(xpos, ypos, lab, gears) ggplot(mtcars) + geom_bar(aes(x=cyl)) + facet_wrap(~gears, ncol=2) + geom_text(data=ldata, aes(x=xpos, y=ypos, label=lab, size=1), vjust=2, parse=false)
try adding shorttitle
list of labels. looks may need. if provide subsample of data frame, sure.
Comments
Post a Comment