0

I am working on the dataset reported here below (pre.sss)

pre.sss <- pre.sss <- structure(list(Pretest.num = c(63, 62, 61, 60, 59, 58, 57, 4,2, 1), stress = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,1L), .Label = c("[0,6]", "(6,9]"), class = "factor"), time = c(1L,1L, 1L, 1L, 1L, 1L, 1L, 8L, 8L, 8L), after = structure(c(2L,2L, 2L, 2L, 2L, 2L, 1L, 1L, NA, 1L), .Label = c("no", "yes"), class = "factor"),id = c("call_fam", "call_fam", "call_fam", "call_fam", "call_fam","call_fam", "call_fam", "counselor", "counselor", "counselor")), .Names = c("Pretest.num", "stress", "time", "after","id"), reshapeLong = structure(list(varying = structure(list(after = c("after.call.fam", "after.speak", "after.send.email","after.send.card", "after.attend", "after.fam.mtg", "after.sup.grp","after.counselor")), .Names = "after", v.names = "after", times = 1:8),v.names = "after", idvar = "Pretest.num", timevar = "time"), .Names = c("varying","v.names", "idvar", "timevar")), row.names = c("63.1", "62.1","61.1", "60.1", "59.1", "58.1", "57.1", "4.8", "2.8", "1.8"), class = "data.frame")

and I need to plot the counts of several categorical variables according to a specific level of another categorical variable ('stress'): so, a faceted bobble-lot would do the job in my case So what I do is the following:

ylabels = c('call_fam' = "call fam.member for condolences",
            'speak' = "speak to fam.member in person", 
            'send.email' = "send condolence email to fam.member",
            'send.card' = "send condolence card/letter to fam.member", 
            'attend' = "attend funeral/wake", 
            'fam.mtg' = "provide fam.meeting",
            'sup.grp' = "suggest attending support grp.",
            'counselor' = "make referral to bereavement counselor" )

p = ggplot(pre.sss, aes(x = after, y = id)) + 
    geom_count(alpha = 0.5, col = 'darkblue') + 
    scale_size(range = c(1,30)) +
    theme(legend.position = 'none') +
    xlab("Response") + 
    ylab("What did you do after learning about death?") + 
    scale_y_discrete(labels = ylabels) +
    facet_grid(.~ pre.sss$stress, labeller = as_labeller(stress.labels))

and I obtain the following image, exactly as I want.

enter image description here

Now I would like to label each bubble with the count with which the corresponding data appear in the dataset.

dat = data.frame(ggplot_build(p)$data[[1]][, c('x', 'y', 'PANEL', 'n')])
dat$PANEL = ifelse(dat$PANEL==1, "[0,6]", "(6-9]")
colnames(dat) = c('x', 'y', 'stress', 'n')

p + geom_text(aes(x, y, label = n, group = NULL), data = dat)

This gives me the following error I really can't understand.

> p +  geom_text(aes(x, y, label=n, group=NULL), data=dat)
Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 1L, 1L, 1L,  : 
  replacement has 504 rows, data has 46

Can anybody help me with this?

Thanks!

EM

4
  • Without a sample of your data, I can't reproduce it. But at a guess, you're defining data, x and y in your initial ggplot() and in your geom_text() call. You could try ggplot() + geom_count(data = pre.sss, aes(x = after, y = id) + geom_text(data = pre.sss, aes(x = after,y = id, label = n, group = NULL) Commented Jan 31, 2018 at 0:09
  • 1
    Please do not use dropbox to share your data. Not everyone's going to download something from an external link into their own computers. Including the results of dput(pre.sss) in your would be preferable. Commented Jan 31, 2018 at 1:58
  • 2
    "This gives me an error I really can't understand." We can't either, because you didn't post it. And I can't recreate without some data. Commented Jan 31, 2018 at 8:47
  • Thanks, and apologies for the bad posting. I have attached a hopefully more reproducible example. Commented Jan 31, 2018 at 15:27

1 Answer 1

1

The function you refer to as your labeller function is missing from this example still. geom_count uses stat_sum, which calculates a parameter n, the number of observations at that point. Because you can use this calculated parameter, you don't actually have to assign the plot to a variable and pull out its data, as you did with ggplot_build.

This should do what you're looking for:

ggplot(pre.sss, aes(x = after, y = id)) + 
    geom_count(alpha = 0.5, col = 'darkblue') + 
# note the following line
    stat_sum(mapping = aes(label = ..n..), geom = "text") +
    scale_size(range = c(1,30)) +
    theme(legend.position = 'none') +
    xlab("Response") + 
    ylab("What did you do after learning about death?") + 
    scale_y_discrete(labels = ylabels) +
    facet_grid(.~ stress)

The line I added computes the same thing as what's behind the scenes in geom_count, but gives it a text geom instead, with the label mapped to that computed parameter n.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.