繁体   English   中英

在R中制作特定的分位数图

[英]Making a specific quantile plot in R

我对以下视觉化(十分位术语)很感兴趣

在此处输入图片说明

而且我想知道如何在R中做到这一点。

当然有直方图和密度图,但是它们并不能很好地显示出来。 特别是,我想知道是否可以使用ggplot / tidyverse做到这tidyverse

编辑以响应注释library(dplyr) library(ggplot2) someData <- data_frame(x = rnorm(1000)) ggplot(someData, aes(x = x)) + geom_histogram()这会产生直方图(请参见http:/ /www.r-fiddle.org/#/fiddle?id=LQXazwMY&version=1

但是我怎样才能得到瓦楞纸呢? 如何实现小矩形? (箭头不太相关)。

您必须定义多个中断,并使用与这些直方图中断匹配的近似十分位。 否则,两个十分之一将以一个小节结束。

d <- data_frame(x = rnorm(1000))

breaks <- seq(min(d$x), max(d$x), length.out = 50)
quantiles <- quantile(d$x, seq(0, 1, 0.1))
quantiles2 <- sapply(quantiles, function(x) breaks[which.min(abs(x - breaks))])

d$bar <- as.numeric(as.character(cut(d$x, breaks, na.omit((breaks + dplyr::lag(breaks)) / 2))))
d$fill <- cut(d$x, quantiles2, na.omit((quantiles2 + dplyr::lag(quantiles2)) / 2))

ggplot(d, aes(bar, y = 1, fill = fill)) +
  geom_col(position = 'stack', col = 1, show.legend = FALSE, width = diff(breaks)[1])

在此处输入图片说明

或具有更多不同的颜色:

ggplot(d, aes(bar, y = 1, fill = fill)) +
  geom_col(position = 'stack', col = 1, show.legend = FALSE, width = diff(breaks)[1]) +
  scale_fill_brewer(type = 'qual', palette = 3) # The only qual pallete with enough colors

在此处输入图片说明

添加一些样式并将断点增加到100:

ggplot(d, aes(bar, y = 1, fill = fill)) +
  geom_col(position = 'stack', col = 1, show.legend = FALSE, width = diff(breaks)[1], size = 0.3) +
  scale_fill_brewer(type = 'qual', palette = 3) +
  theme_classic() +
  coord_fixed(diff(breaks)[1], expand = FALSE) + # makes square blocks
  labs(x = 'x', y = 'count')

在此处输入图片说明

这是最后一个函数:

decile_histogram <- function(data, var, n_breaks = 100) {
  breaks <- seq(min(data[[var]]), max(data[[var]]), length.out = n_breaks)
  quantiles <- quantile(data[[var]], seq(0, 1, 0.1))
  quantiles2 <- sapply(quantiles, function(x) breaks[which.min(abs(x - breaks))])

  data$bar <- as.numeric(as.character(
    cut(data[[var]], breaks, na.omit((breaks + dplyr::lag(breaks)) / 2)))
  )
  data$fill <- cut(data[[var]], quantiles2, na.omit((quantiles2 + dplyr::lag(quantiles2)) / 2))

  ggplot2::ggplot(data, ggplot2::aes(bar, y = 1, fill = fill)) +
    ggplot2::geom_col(position = 'stack', col = 1, show.legend = FALSE, width = diff(breaks)[1], size = 0.3) +
    ggplot2::scale_fill_brewer(type = 'qual', palette = 3) +
    ggplot2::theme_classic() +
    ggplot2::coord_fixed(diff(breaks)[1], expand = FALSE) +
    ggplot2::labs(x = 'x', y = 'count')
}

用于:

d <- data.frame(x = rnorm(1000))
decile_histogram(d, 'x')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM