2

With data like below, that plots the CDF of two different columns in the data frame, need to add some part of the summary stats to the plot.

To plot the CDF I do the following

set.seed(1234)
df <- data.frame(height1 = round(rnorm(200, mean=60, sd=15)), height2 = round(rnorm(200, mean=55, sd=10)))
df %>% pivot_longer(c('height1', 'height2'), names_to = "Group", values_to = "value") %>%
  ggplot(aes(value, colour = Group)) + 
  stat_ecdf()

The above gives the CDF

Basic CDF

I need to add some summary stats to this plot and need help with getting the following 2 representations.

The summary stats table is generated as

tbl = df %>% pivot_longer(c('height1', 'height2'), names_to = "Group", values_to = "value") %>%
  group_by(Group) %>%
  summarize(Samples = n(), Median=median(value, na.rm=T), Pctl_10th=quantile(value, 0.1, na.rm=T))

One thing with Samples is that it should omit NAs.

  1. CDF with Summary Stats inside the plot positioned at the bottom left corner
  2. CDF with Summary Stats outside the plot positioned at the bottom left corner

CDF with Summary Stats inside the plot positioned at the bottom left corner CDF with Summary Stats outside the plot positioned at the bottom left corner

Looking for an answer that covers the following

  1. Using ggpmisc , tableGrob packages or other mainline package
  2. Flexibility in specifying the position of the table relative to bottom-right or bottom-left or top-right or top-left corners of the graph or the whole plot area ( as in the second example).
Waldi
  • 39,242
  • 6
  • 30
  • 78
user3206440
  • 4,749
  • 15
  • 75
  • 132
  • Possible duplicate of https://stackoverflow.com/questions/12318120/adding-table-within-the-plotting-region-of-a-ggplot-in-r – akrun Jul 15 '20 at 03:07

1 Answers1

3

You can position the plot and the table over an empty plot using ggpmisc.
To facilitate positioning, coordinates of the empty plot are set to (0,1) range.
You can define position of each object in the tibble (see below) and adjust sizing/positioning with following parameters :

  • vp.height and vp.width define width and heigth of each plot (from 0 to 1 = 100%)
  • hjust and vjust define how the table is aligned to its position : 0 is left/bottom adjustment, 1 is top/right adjustment
set.seed(1234)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggpmisc)

df <- data.frame(height1 = round(rnorm(200, mean=60, sd=15)), height2 = round(rnorm(200, mean=55, sd=10)))

tb <- df %>% pivot_longer(c('height1', 'height2'), names_to = "Group", values_to = "value") %>%
  group_by(Group) %>%
  summarize(Samples = n(), Median=median(value, na.rm=T), Pctl_10th=quantile(value, 0.1, na.rm=T))


p <- df %>% pivot_longer(c('height1', 'height2'), names_to = "Group", values_to = "value") %>%
  ggplot(aes(value, colour = Group)) + 
  stat_ecdf()

# x and y values define the position of the lower left corner of eah object
data.plot <- tibble(x = 0, y = 0, p = list(p ))
data.tb <- tibble(x = 0.6, y = 0, tbl = list(tb))

# Set up the plot
# vp.width defines the relative width of the plot : here = 70% (including legend)
# vp.height defines the relative height of the plot : here = 80%
# hjust defines table horizontal alignement, set to 0 so that x controls left position
# vjust defines table vertical alignement, set to 0 so that y control bottom position

# On the side of the plot
ggplot() + xlim(c(0,1)) + ylim(c(0,1))  +   theme_void() +
  geom_plot(data = data.plot, aes(x, y, label = p,vp.width = .7,vp.height = 0.8))+
  geom_table(data=data.tb, aes(x,y,label = tbl,hjust=0,vjust=0))

# Inside the plot
data.tb <- tibble(x = 0.4, y = 0.1, tbl = list(tb))

ggplot() + xlim(c(0,1)) + ylim(c(0,1))  +   theme_void() +
  geom_plot(data = data.plot, aes(x, y, label = p,vp.width = 0.95,vp.height = 0.8))+
  geom_table(data=data.tb, aes(x,y,label = tbl,hjust=0,vjust=0))

Note that ggmisc doesn't control table width, so it's difficult to perfectly align plot border with table border and has to be done manually depending on figure size. enter image description here enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78