[英]scales_fill_continuous doesn't work (ggplot2)
I have a data:我有一个数据:
df_1 <- data.frame(
x = replicate(
n = 2, expr = rnorm(n = 3000, mean = 100, sd = 10)
),
y = sample(x = 1:3, size = 3000, replace = TRUE)
)
And the follow function:以及以下功能:
library(tidyverse)
ggplot(data = df_1, mapping = aes(x = x.1, fill = x.1)) +
geom_histogram(color = 'black', bins = 100) +
scale_fill_continuous(low = 'blue', high = 'red') +
theme_dark()
scale_fill_continuous
doesn't work. scale_fill_continuous
不起作用。 The graph is black and gray.该图为黑色和灰色。
Tks. Tks。
The problem, I think, is that there are nrow(df_1)
values for fill, but only 100 are needed.我认为问题在于填充有nrow(df_1)
值,但只需要 100 个。 This could be solved by pre-calculating the bin positions and counts and plotting with geom_col
, but a neater solution is to use stat
.这可以通过预先计算 bin 位置和计数并使用geom_col
绘图来geom_col
,但更简洁的解决方案是使用stat
。 stat
is supposed to be for computed variables (eg stat(count)
- see ?geom_histogram
) but we can give it the vector 1:nbin
and it works. stat
应该用于计算变量(例如stat(count)
- 请参阅?geom_histogram
)但我们可以给它向量1:nbin
并且它可以工作。
df_1 <- data.frame(
x = replicate(n = 2, expr = rnorm(n = 3000, mean = 100, sd = 10)),
y = sample(x = 1:3, size = 3000, replace = TRUE)
)
library(tidyverse)
nbins <- 100
ggplot(data = df_1, mapping = aes(x = x.1, fill = stat(1:nbins))) +
geom_histogram(bins = nbins) +
scale_fill_continuous(low = "red", high = "blue")
Created on 2020-01-19 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 1 月 19 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.