Home > front end >  ggplot2 adding label to geom_area
ggplot2 adding label to geom_area

Time:10-02

I'm teaching undergrad statistics and trying to make a useful little R script to help my students understand calculating probabilities in the standard normal distribution. I have this script, which takes zscore breakpoints, calculates the fraction of data between each breakpoint, and colors each breakpoint section:

library(tidyverse)
library(ggplot2)
library(magrittr)

sim_dat = data.frame(z = seq(-5,5, length.out = 1001))
sim_dat$y = dnorm(sim_dat$z, mean = 0, sd=1)

#fill in z-score bkpts, excluding zero: 0 will always be included

zscores <- c(-1,1.5)
zscores <- sort( setdiff(zscores,0) )
bkpoints <- sort( c(-Inf, zscores,0, Inf))

#find pct data between brekpoints
pctdata <- numeric(length=length(bkpoints)-1)
interval <- character(length=length(bkpoints)-1)

for(i in 1:length(pctdata)){
  pctdata[i] <- plyr::round_any(  pnorm(q=bkpoints[i 1]) - pnorm(q=bkpoints[i]) , 0.0001)
  interval[i] <- paste0(bkpoints[i],",",bkpoints[i 1])
  
  
}

pctdata_df <- cbind.data.frame(interval,pctdata,stringsAsFactors=FALSE)

sim_dat$standard_normal_sections = cut(sim_dat$z, breaks = bkpoints)

p1 <- ggplot2::ggplot(sim_dat, aes(z, y, fill = standard_normal_sections))   geom_area()  
  scale_x_continuous(breaks= c(seq(-5,5,1), zscores))

p1

pctdata_df

I'd like to use pctdata_df$pctdata(vector of how much data is in section of p1) as labels. I'm finding very little on how to add labels to geom_area. Any help is appreciated!

CodePudding user response:

There is nothing special about geom_area. If you want to add labels you could do so with geom_text where you pass your pctdata_df to the data argument. As you gave no information on where you want to add your labels I have put them beneath the area chart.

Note: There is no need for a for loop. You could simply pass a vector to pnorm or paste.

library(scales)
library(ggplot2)

# find pct data between brekpoints
lower <- bkpoints[1:(length(bkpoints) - 1)]
upper <- bkpoints[2:length(bkpoints)]

pctdata <- pnorm(q = upper) - pnorm(q = lower)
interval <- paste0(lower, ",", upper)

pctdata_df <- data.frame(interval, lower, upper, pctdata)
pctdata_df$x_label <- with(pctdata_df, ifelse(is.infinite(lower), upper - 1, .5 * (lower   upper)))
pctdata_df$x_label <- with(pctdata_df, ifelse(is.infinite(upper), lower   1, x_label))

sim_dat$standard_normal_sections <- cut(sim_dat$z, breaks = bkpoints)

ggplot(sim_dat, aes(z, y))  
  geom_area(aes(fill = standard_normal_sections))  
  geom_text(data = pctdata_df, aes(x = x_label, y = 0, label = scales::number(pctdata, .01)), 
            vjust = 1, size = 8 / .pt, nudge_y = -.01)  
  scale_x_continuous(breaks = c(seq(-5, 5, 1), zscores))

  • Related