2017-10-08 32 views
0

特定位数图我用following visulization (Decile term)制作中的R

enter image description here

非常感兴趣,并且我不知道它是如何能够做到这一点在R.

当然还有直方图和密度阴谋,但他们并没有这样一个很好的可视化。特别是,我想知道是否有可能与ggplot/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

但我怎么能得到coloful吧?如何实现小矩形? (箭头不太相关)。

+1

我希望编辑使它更加具体。谢谢! – Drey

回答

5

您必须定义多个休息点,并使用与这些直方图间隔匹配的近似十进制数。否则,两个十进制将在一个小节中结束。

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

enter image description here

或者更多不同的颜色:

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 

enter image description here

添加一些造型,增加休息,以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') 

enter image description here

这里是使最后一个功能:

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') 
+0

这太神奇了,谢谢! – Drey

+0

@Axeman,非常详细的答案。谢谢。如何将OP问题中显示的标签添加到答案图上,有什么建议? – Ashish

+0

你可以使用'quantiles'向量添加一些标签或线条,或者如果你想让它们与条形线对齐,但不太准确,可以使用'quantiles2'。让他们在正确的高度需要更多的工作。我没有打扰,因为OP说他并不重要。 – Axeman