【问题标题】:R ggplot - ecdf chart - adding table with summary stats inside/outside plot areaR ggplot - ecdf 图表 - 在绘图区域内/外添加带有汇总统计信息的表格
【发布时间】:2020-07-15 02:57:29
【问题描述】:

使用如下数据,绘制数据框中两个不同列的 CDF,需要在图中添加一些汇总统计信息。

要绘制 CDF,我执行以下操作

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()

上面给出了CDF

我需要在此图中添加一些摘要统计信息,并且需要帮助以获取以下 2 个表示。

汇总统计表生成为

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))

Samples 的一个特点是它应该省略 NAs。

  1. 带有摘要统计的 CDF位于左下角的绘图
  2. 具有摘要统计的 CDF位于左下角的图

寻找涵盖以下内容的答案

  1. 使用ggpmisctableGrob包或其他主线包
  2. 灵活地指定表格相对于图表的右下角或左下角或右上角或左上角或整个绘图区域的位置(如第二个示例所示)。

【问题讨论】:

标签: r ggplot2 dplyr


【解决方案1】:

您可以使用ggpmisc 将绘图和表格放置在空绘图上。
为方便定位,空图坐标设置为(0,1)范围。
您可以定义小标题中每个对象的位置(见下文)并使用以下参数调整大小/位置:

  • vp.heightvp.width 定义每个图的宽度和高度(从 0 到 1 = 100%)
  • hjustvjust 定义表格如何与其位置对齐:0 是左/下调整,1 是上/右调整
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))

请注意,ggmisc 不控制表格宽度,因此很难将绘图边框与表格边框完美对齐,必须根据图形大小手动完成。

【讨论】:

  • 第一个表示表需要在其中的位置 - 步骤代码中的 cmets 会有帮助
  • 对于表格来说hjust=0.3,vp.width=0.3 是什么意思 - 图表右侧是 0.3(单位?)?
  • 收到警告Ignoring unknown aesthetics: vp.width
  • 感谢您的反馈,抱歉警告:表格不使用 vp.width,这仅适用于情节部分。查看我的编辑。
猜你喜欢
  • 2012-09-01
  • 2020-12-03
  • 1970-01-01
  • 2014-02-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多