2

I have some data like this:

site <- c('twitter', 'facebook', 'gplus')
bouncerate <- c(35, 29, 17)
conversionrate <- c(20, 30, 32)
users <- c(350, 800, 42)
df <- data.frame(site, bouncerate, conversionrate, users) %>% melt()

I’m plotting it like so:

ggplot(subset(df, variable!='users'), aes(x=site, y=value)) + 
  geom_bar(stat="identity", aes(fill=variable), position="dodge") +
  coord_flip() +
  scale_fill_brewer(palette = "Set1”)

As you can see, I’m trying to plot the “rate” variables. But I’d also like to drop the value of the “users” variable over on the right side of the bars. It’d look something like this:

sample plot

So my question is, how do I get those user counts over there? I thought maybe geom_label could do this, but I can’t see how to tie the coordinates of the label to the cluster.

And, while the above example doesn’t show it, I would be thrilled if the cluster with the biggest number of users was at the top, and the rest descended from there.

hoosteeno
  • 644
  • 5
  • 17
  • Ordering bars (equivalent to ordering your axis) is [an r-faq](http://stackoverflow.com/q/5208679/903061). Just reorder your factor with `reorder()`. – Gregor Thomas Feb 10 '16 at 22:35
  • Great! I’ll work on that. I just clarified the text of this question to make sure my main issue is clear. – hoosteeno Feb 10 '16 at 22:39
  • It might also be good to show your attempt - personally, I would construct a data frame with the data I wanted to plot (a factor called `site` with the same levels as in your data and a column called `label` with the text you want to add) and use `geom_text`. – Gregor Thomas Feb 10 '16 at 22:45

1 Answers1

5

Here's a way with geom_text:

ggplot(subset(df, variable!='users'), aes(x=site, y=value)) + 
  geom_bar(stat="identity", aes(fill=variable), position="dodge") +
  coord_flip() +
  scale_fill_brewer(palette = "Set1") +
  geom_text(data=subset(df, variable=="users"), 
aes(x=site, y=40, 
label=paste("(", value ,"\n", variable, ")", sep = "")))

enter image description here

erc
  • 10,113
  • 11
  • 57
  • 88