TER
TER

Reputation: 119

Label help for histograms

I have a set of histograms and would like to put labels on the +- 1 stdev and the mean. I have the lines, but want the labels as well. I would like the labels toward the bottom of the histograms with white background so the font is legible against the histogram colors.

library(dply)
library(ggplot2)

a = runif(1000,1,100)
b = runif(1000,1,100)
c = runif(1000,1,100)

amount = c(a,b,c)

cat = c(rep("a",1000), rep("b",1000), rep("c",1000))

hist.data = data.frame(amount,cat)
names(hist.data) = c("amount","cat")

hist.data$cat = factor(hist.data$cat, levels = c("a","b","c"))
pricedata = ddply(hist.data, c("cat"), summarize, avg = mean(amount), minus.stdev = mean(amount)-sd(amount), 
  plus.stdev = mean(amount) + sd(amount))
pricedata = pricedata[order(pricedata$avg),]


ggplot(hist.data, aes(x=amount, fill = cat))+
  geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
  theme_test()+
  geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+


  geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+


  geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+


  facet_grid(cat ~., scales = "free")+
  scale_y_continuous(expand = c(0,0),name = "Count")+
  scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))

enter image description here

Upvotes: 2

Views: 98

Answers (2)

Chuck P
Chuck P

Reputation: 3923

And if you want to make the fewest changes to your existing code (although I agree the other answers are more elegant) you can add three lines to your existing


ggplot(hist.data, aes(x=amount, fill = cat))+
  geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
  theme_test()+
  geom_vline(aes(xintercept = avg), data = pricedata, color = "black", size = 1)+
  geom_label(aes(x = avg, y = 10, label = scales::dollar_format()(avg)), data = pricedata, fill = "white", size = 3) +
  geom_vline(aes(xintercept = minus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
  geom_label(aes(x = minus.stdev, y = 10, label = scales::dollar_format()(minus.stdev)), data = pricedata, fill = "white", size = 3) +
  geom_vline(aes(xintercept = plus.stdev), data = pricedata, color = "black", size = .75, linetype = "dotted")+
  geom_label(aes(x = plus.stdev, y = 10, label = scales::dollar_format()(plus.stdev)), data = pricedata, fill = "white", size = 3) +
  facet_grid(cat ~., scales = "free")+
  scale_y_continuous(expand = c(0,0),name = "Count")+
  scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))

Upvotes: 1

Gregor Thomas
Gregor Thomas

Reputation: 146164

I'd recommend converting your pricedata to long format - that somewhat simplifies your geom_vline calls (though you have to add size and linetype scales...) and makes using geom_label pretty simple. At this point, geom_label is pretty new, and doesn't allow you to do things like change the angle.

pricedata_long = tidyr::pivot_longer(pricedata, -cat)


ggplot(hist.data, aes(x=amount, fill = cat))+
  geom_histogram(color="white", alpha = .8, position = 'identity', binwidth = 5)+
  theme_test()+
  geom_vline(aes(xintercept = value, linetype = name, size = name),
             data = pricedata_long, color = "black", show.legend = FALSE) +
  geom_label(aes(x = value, label = scales::dollar_format()(value)), data = pricedata_long,
             y = 0, fill = "white", vjust = -.1,
             show.legend = FALSE) +
  scale_size_manual(values = c("avg" = 1, "minus.stdev" = 0.75, "plus.stdev" = 0.75)) +
  scale_linetype_manual(values = c("avg" = "solid", "minus.stdev" = "dotted", "plus.stdev" = "dotted")) +

  facet_grid(cat ~., scales = "free")+
  scale_y_continuous(expand = c(0,0),name = "Count")+
  scale_x_continuous(labels = scales::dollar, name="\nAmount", limits = c(0,100))

enter image description here

Upvotes: 3

Related Questions