geom_text is my nemesis

20 views
Skip to first unread message

Stuart Luppescu

unread,
Feb 11, 2015, 9:43:50 PM2/11/15
to ggplot2
Hello, Thanks to the wonderful R Graphics Cookbook, I was able to get
this graph to make but I still cant get the annotation I want. I just
want to put the numbers of people below each faceted graph. The code
runs without an error message, but no annotations appear. Under each of
the 3 facets I expect "N=nnn" to be printed but I get nothing.
Here is the main data frame:

structure(list(Var1 = structure(c(1L, 2L, 1L, 2L, 3L, 2L, 3L,
4L), .Label = c("1", "2", "3", "4"), class = "factor"), Var2 =
structure(c(1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("1", "2", "3", "4"), class =
"factor"),
count = c(40L, 25L, 2L, 214L, 46L, 17L, 164L, 2L), type =
c("elem_iva",
"elem_iva", "elem_iva", "elem_iva", "elem_iva", "elem_iva",
"elem_iva", "elem_iva"), plain_sum = c(65L, 65L, 262L, 262L,
262L, 183L, 183L, 183L), plain_pct = c(0.615384615384615,
0.384615384615385, 0.00763358778625954, 0.816793893129771,
0.175572519083969, 0.092896174863388, 0.896174863387978,
0.0109289617486339), adj_summative_cat = c(1, 2, 1, 2, 3,
2, 3, 4), plain_summative_cat = c(1, 1, 2, 2, 2, 3, 3, 3),
foo = c(1, 2, 0, 1, 2, 0, 1, 2), cat_diff = structure(c(2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Lower", "No Change",
"Higher"), class = c("ordered", "factor"))), .Names = c("Var1",
"Var2", "count", "type", "plain_sum", "plain_pct", "adj_summative_cat",
"plain_summative_cat", "foo", "cat_diff"), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -8L), vars = list(
type, Var2), drop = TRUE, indices = list(0:1, 2:4, 5:7), group_sizes
= c(2L,
3L, 3L), biggest_group_size = 3L, labels = structure(list(type =
c("elem_iva",
"elem_iva", "elem_iva"), Var2 = structure(1:3, .Label = c("1",
"2", "3", "4"), class = "factor")), class = "data.frame", row.names =
c(NA,
-3L), .Names = c("type", "Var2"), vars = list(type, Var2)))

This is the annotation data frame:
structure(list(type = c("elem_iva", "elem_iva", "elem_iva"),
plain_summative_cat = structure(1:3, .Label = c("1", "2",
"3", "4"), class = "factor"), N = c(65, 262, 183)), .Names =
c("type",
"plain_summative_cat", "N"), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -3L), vars = list(type), drop =
TRUE, indices = list(
0:2), group_sizes = 3L, biggest_group_size = 3L, labels =
structure(list(
type = "elem_iva"), class = "data.frame", row.names = c(NA,
-1L), .Names = "type", vars = list(type)))

And here is my code:
ggplot(all_tab3[all_tab3$type=="elem_iva",], aes(x=cat_diff,
y=plain_pct)) +
geom_bar(stat="identity") +
scale_y_continuous(name="Percentage", labels=percent) +
facet_grid(. ~ plain_summative_cat) +
theme(axis.text.x = element_text(angle=90, hjust=1)) +
geom_text(data=all_ns[all_ns$type=="elem_iva",], x=1, y=-.1,
aes(label=paste("N=", N)))

I'm thinking it must be the x location. In the main graph, the bars are
located by cat_diff, which is coded 0, 1, 2. I thought I'd put the
annotation in the middle of the three bars, so I tried to locate the
text at x=1. Doesn't seem to be working. Can anyone give me a bit of
help?

TIA
--
Stuart Luppescu <s...@ccsr.uchicago.edu>

Reply all
Reply to author
Forward
0 new messages