1

我有一个数据集,我想使用 geom_histogram 用 ggplot 绘制(数据如下)。我在尝试获取渐变配色方案时遇到问题。

structure(list(UserID = c(39120L, 39536L, 39550L, 39627L, 39632L, 39709L, 39971L, 39988L, 39990L, 40062L, 40065L, 40065L, 40066L, 40142L, 40142L, 40143L, 40161L, 40193L, 40364L, 40437L, 40439L, 40440L, 40451L, 40453L, 40665L, 40665L, 40668L, 40751L, 40843L, 40843L, 40843L, 40846L, 40846L, 40847L, 40847L, 40850L, 40850L, 40884L, 40884L, 40884L, 40896L, 40900L, 40902L, 40903L, 40905L, 40963L, 40966L, 40966L, 40967L, 40967L, 40969L, 40971L, 40971L, 40985L, 40985L, 41010L, 41079L, 41080L, 41080L, 41081L, 41093L, 41108L, 41110L, 41111L, 41113L, 41114L, 41133L, 41137L, 41138L, 41140L, 41161L, 41162L, 41182L, 41186L, 41260L), sales = c(0.0119, 0.0032, 0.0091, 0.0098, 0.0086, 0.0101, 0.0107, 0.0111, 0.0085, 0.0178, 0.0069, 0.0055, 0.0133, 0.0112, 0.0084, 0.0141, 0.0159, 0.01, 0.0054 , 0.0129, 0.011, 0.0116, 0.0099, 0。0134, 0.0046, 0.004, 0.0076, 0.005, 0.0027, 0.0037, 3e-04, 0.022, 0.012, 0.0082, 0.0108, 0.0092, 0.0101, 0.0016, 0.0082, 0.0035, 0.007, 0.0098, 0.0146, 0.0074, 0.005, 0.0152, 0.0046, 0.0032、0.0028、0、0.0179、0.0185、0.0095、0.0401、0.0163、0.0085、0.0099、0.0064、0.0064、0.0067、0.0052、0.0191、0.0191、0.0118,0.0118,0.0054,0.0054,0.0054,0.00111,0.0065,0.0065,0.0001,0001,0001,000124,0.00,0124,0.00 ,, 0.00,0124,0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.00 ,, 0.0066, 0.0169, 0.0071 ), salesfromtarget = c(0.214285714285714, -0.673469387755102, -0.0714285714285714, 0, -0.122448979591837, 0.0306122448979592, 0.0918367346938777, 0.13265306122449, -0.13265306122449, 0.816326530612245, -0.295918367346939, -0.438775510204082, 0.357142857142857, 0.142857142857143, -0.142857142857143, 0.438775510204082, 0.622448979591837, 0.0204081632653062, -0.448979591836735, 0.316326530612245, 0.122448979591837, 0.183673469387755, 0.010204081632653, 0.36734693877551, -0.530612244897959, -0.591836734693878, -0.224489795918367, -0.489795918367347, -0.724489795918367, -0.622448979591837, -0.969387755102041, 1.24489795918367, 0.224489795918367, -0.163265306122449, 0.102040816326531, -0.0612244897959184, 0.0306122448979592, -0.836734693877551, -0.163265306122449, -0.642857142857143, -0.285714285714286, 0, 0.489795918367347, -0.244897959183673, -0.489795918367347, 0.551020408163265, -0.530612244897959, -0.673469387755102, -0.714285714285714, -1, 0.826530612244898, 0.887755102040817, -0.0306122448979592, 3.09183673469388, 0.663265306122449, -0.13265306122449, 0.010204081632653, -0.346938775510204, -0.316326530612245, - 0.469387755102041, 0.948979591836735, 0.204081632653061, -0.448979591836735, 0.13265306122449, -0.336734693877551,0.26530612244898, -0.520408163265306, 0.13265306122449, -0.357142857142857, -0.26530612244898, -0.36734693877551, -0.0714285714285714, -0.326530612244898, 0.724489795918367, -0.275510204081633)), .Names = c("UserID", "sales", "salesfromtarget" ), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L , 28L, 29L, 31L, 32L, 35L, 36L, 37L, 38L, 39L, 41L, 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 56L, 57L, 58L , 59L, 60L, 61L, 62L, 63L, 64L, 65L, 67L, 69L, 70L, 72L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L , 93L), 类 = "data.frame")326530612244898, 0.724489795918367, -0.275510204081633)), .Names = c("UserID", "sales", "salesfromtarget"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L , 10L, 11L, 12L, 13L, 15L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 28L, 29L, 31L, 32L, 35L, 36L, 37L, 38L, 39L, 41L , 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 67L, 69L, 70L , 72L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 93L), 类别 = "data.frame")326530612244898, 0.724489795918367, -0.275510204081633)), .Names = c("UserID", "sales", "salesfromtarget"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L , 10L, 11L, 12L, 13L, 15L, 16L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 28L, 29L, 31L, 32L, 35L, 36L, 37L, 38L, 39L, 41L , 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 67L, 69L, 70L , 72L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 93L), 类别 = "data.frame")32L, 35L, 36L, 37L, 38L, 39L, 41L, 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 56L, 57L, 58L, 59L, 60L, 61L, 62L、63L、64L、65L、67L、69L、70L、72L、77L、78L、79L、80L、81L、82L、83L、84L、85L、86L、87L、88L、89L、90L、93L),等级=“数据.frame")32L, 35L, 36L, 37L, 38L, 39L, 41L, 42L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 56L, 57L, 58L, 59L, 60L, 61L, 62L、63L、64L、65L、67L、69L、70L、72L、77L、78L、79L、80L、81L、82L、83L、84L、85L、86L、87L、88L、89L、90L、93L),等级=“数据.frame")

使用此代码我试图用来scale_fill_brewer获得一个渐变方案(但它不必是这个特定的,它只是一个不工作的例子)。所有的条都是灰色的,理想情况下它们不会是灰色的。最好的情况我想使用类似的东西scale_fill_gradient(low = "green", high = "blue")(同样这对我不起作用)。我觉得我错过了 ggplot2 的一些基本内容。

require(ggplot2)
require(scales)
require(ggthemes)

ggplot(repex, aes(x = salesfromtarget)) +
geom_histogram(binwidth = .1, alpha = 0.5, colour = "white") +
scale_fill_brewer(palette = "Spectral") +
guides(fill = FALSE) +
theme_solarized() +
ggtitle("Standard Distribution of Sales") + xlab("Sales") + ylab("Frequency") +
theme(plot.title = element_text(size = 13, colour = "black", face = "bold", vjust = 1)) +
theme(axis.title.x = element_text(size = 12, colour = "black", vjust = -.005), 
      axis.title.y = element_text(size = 12, colour = "black", vjust = 0.2),
      axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_x_continuous(breaks = seq(-1.5, 1.5, by = .25),
                 limits = c(-1.5, 1.5), labels = percent) 
4

1 回答 1

8

您需要在 aes() 部分定义“填充”变量:

ggplot(repex, aes(x=salesfromtarget, fill=..x..))
    +geom_histogram(binwidth=.1)
    +scale_fill_gradient("Legend",low = "green", high = "blue")

在此处输入图像描述

由于直方图条是每个 x 轴值的计数,因此如果要使用原始 x 值,则应使用“ ..x.. ”。您可以使用“ ..count.. ”填充直方图计数:

ggplot(repex, aes(x=salesfromtarget, fill=..count..))
    +geom_histogram(binwidth=.1)
    +scale_fill_gradient("Legend",low = "green", high = "blue")

在此处输入图像描述

于 2013-07-18T19:54:01.973 回答