删除geom_histogram的基线颜色 [英] Remove baseline color for geom_histogram
问题描述
我要在多面直方图中添加 color
美观度.在下面的reprex中,没有颜色美感,直方图仅显示该构面级别内的数据.但是,在定义了 color
的情况下,添加了一个基线,该基线将拉伸范围扩展到包括所有方面的数据范围.有没有办法使这种情况不发生?
我正在寻找与 triom = TRUE
的 geom_density
类似的东西,但是 geom_histogram
.
库(tidyverse)数据<-tibble(a = rchisq(1000,df = 3),b = rchisq(1000,df = 1),c = rchisq(1000,df = 10))%>%收集()ggplot(数据,aes(x =值))+geom_histogram()+facet_wrap(〜键,ncol = 1)#>使用bins = 30的stat_bin().用`binwidth`选择更好的价值.
ggplot(data,aes(x = value))+geom_histogram(color ="red")+facet_wrap(〜键,ncol = 1)#>使用bins = 30的stat_bin().用`binwidth`选择更好的价值.
ggplot(data,aes(x = value))+geom_density(color ="red",trim = TRUE)+facet_wrap(〜键,ncol = 1)
由
在这种情况下,如果我们检查与直方图图层关联的数据帧,则有许多行,其中 ymin
= ymax
= 0问题中看到的基准效果.
p<-ggplot(数据,aes(x =值))+geom_histogram(color ="red")+facet_wrap(〜键,ncol = 1)View(layer_data(p)%>%filter(PANEL == 2))#查看与构面面板2相关的数据
解决方法:由于数据计算是在 StatBin
的 compute_group
函数中完成的,因此我们可以定义该函数的替代版本,并通过附加步骤删除该函数.完全对数据框中的行进行零计数:
#修改版继承自StatBin,但#compute_group()函数中的最后第二行StatBin2<-ggproto("StatBin2",StatBin,compute_group =函数(数据,小数位数,binwidth = NULL,bins = NULL,中心= NULL,边界= NULL,已关闭= c("right","left"),pad = FALSE,breaks = NULL,origin = NULL,right = NULL,降= NULL,宽度= NULL){如果(!is.null(breaks)){如果(!scales $ x $ is_discrete()){中断<-缩放$ x $ transform(breaks)}箱<-ggplot2 ::: bin_breaks(休息,已关闭)}否则if(!is.null(binwidth)){如果(is.function(binwidth)){binwidth<-binwidth(data $ x)}bins<-ggplot2 ::: bin_breaks_width(scales $ x $ dimension(),binwidth,中心=中心,边界=边界,已关闭=已关闭)}别的 {bins<-ggplot2 ::: bin_breaks_bins(scales $ x $ dimension(),bins,中心=中心,边界=边界,已关闭=已关闭)}res<-ggplot2 ::: bin_vector(data $ x,bins,weight = data $ weight,pad = pad)#在返回数据帧之前完全删除0计数箱res<-res [res $ count>0,]资源})
用法:
ggplot(data,aes(x = value))+geom_histogram(color ="red",stat = StatBin2)+#指定stat = StatBin2facet_wrap(〜键,ncol = 1)
I'm adding a color
aesthetic to a faceted histogram. In the reprex below, with no color aesthetic, the histogram only show data within that facet level. However, with color
defined, a baseline is added which stretches the stretches to include the range of data across all facets. Is there a way to make this not happen?
I'm looking for something similar to geom_density
with trim = TRUE
, but there doesn't appear to be a trim option for geom_histogram
.
library(tidyverse)
data <- tibble(a = rchisq(1000, df = 3),
b = rchisq(1000, df = 1),
c = rchisq(1000, df = 10)) %>%
gather()
ggplot(data, aes(x = value)) +
geom_histogram() +
facet_wrap(~ key, ncol = 1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data, aes(x = value)) +
geom_histogram(color = "red") +
facet_wrap(~ key, ncol = 1)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data, aes(x = value)) +
geom_density(color = "red", trim = TRUE) +
facet_wrap(~ key, ncol = 1)
Created on 2019-07-20 by the reprex package (v0.3.0)
geom_histogram
draws its bars using using rectGrob
from the grid package, and a zero-width / zero-height rectGrob is depicted as a vertical / horizontal line in the outline colour, at least in my set-up for RStudio (& OP's as well, I presume). Demonstration below:
library(grid)
r1 <- rectGrob(width = unit(0, "npc"), gp = gpar(col = "red", fill = "grey")) # zero-width
r2 <- rectGrob(height = unit(0, "npc"), gp = gpar(col = "red", fill = "grey")) # zero-height
grid.draw(r1) # depicted as a vertical line, rather than disappear completely
grid.draw(r2) # depicted as a horizontal line, rather than disappear completely
In this case, if we check the data frame associated with the histogram layer, there are many rows with ymin
= ymax
= 0, which are responsible for the 'baseline' effect seen in the question.
p <- ggplot(data, aes(x = value)) +
geom_histogram(color = "red") +
facet_wrap(~ key, ncol = 1)
View(layer_data(p) %>% filter(PANEL == 2)) # look at the data associated with facet panel 2
Workaround: Since the data calculations are done in StatBin
's compute_group
function, we can define an alternative version of the same function, with an additional step to drop the 0-count rows from the data frame completely:
# modified version of StatBin2 inherits from StatBin, except for an
# additional 2nd last line in compute_group() function
StatBin2 <- ggproto(
"StatBin2",
StatBin,
compute_group = function (data, scales, binwidth = NULL, bins = NULL,
center = NULL, boundary = NULL,
closed = c("right", "left"), pad = FALSE,
breaks = NULL, origin = NULL, right = NULL,
drop = NULL, width = NULL) {
if (!is.null(breaks)) {
if (!scales$x$is_discrete()) {
breaks <- scales$x$transform(breaks)
}
bins <- ggplot2:::bin_breaks(breaks, closed)
}
else if (!is.null(binwidth)) {
if (is.function(binwidth)) {
binwidth <- binwidth(data$x)
}
bins <- ggplot2:::bin_breaks_width(scales$x$dimension(), binwidth,
center = center, boundary = boundary,
closed = closed)
}
else {
bins <- ggplot2:::bin_breaks_bins(scales$x$dimension(), bins,
center = center, boundary = boundary,
closed = closed)
}
res <- ggplot2:::bin_vector(data$x, bins, weight = data$weight, pad = pad)
# drop 0-count bins completely before returning the dataframe
res <- res[res$count > 0, ]
res
})
Usage:
ggplot(data, aes(x = value)) +
geom_histogram(color = "red", stat = StatBin2) + # specify stat = StatBin2
facet_wrap(~ key, ncol = 1)
这篇关于删除geom_histogram的基线颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!