gpt4 book ai didi

r - ggplot : gradient fill high and low points

转载 作者:行者123 更新时间:2023-12-04 12:07:10 24 4
gpt4 key购买 nike

示例数据:

set.seed(123)
dtf <- as.data.frame(rnorm(n = 1000, mean = 0, sd = 0.02))
dtf = as.data.frame(dtf)
dtf = cbind(Obs = rownames(dtf), dtf)
names(dtf)[2] = "random"
head(dtf)

Obs random
1 1 -0.011209513
2 2 -0.004603550
3 3 0.031174166
4 4 0.001410168
5 5 0.002585755
6 6 0.034301300

简单的点图:

gp = ggplot() +
geom_point(data = dtf, aes(x = Obs, y = random))

gp

我想用 darkgreen 填充高点,用 darkred 填充低点。以下无效:

gp = ggplot() +
geom_point(data = dtf, aes(x = Obs, y = random)) +
scale_colour_gradient(low = "darkgreen", mid = "blue", high = "darkred")

gp

有什么建议吗?

最佳答案

您正在错误地构造您的 ggplot 调用。为什么您的调用不起作用是因为您没有将颜色(通过 aes())映射到任何变量。尝试

ggplot(dtf, aes(x = Obs, y = random, color = random)) +
geom_point() +
scale_colour_gradient2(low = "darkgreen", mid = "blue", high = "darkred")

关于r - ggplot : gradient fill high and low points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44173263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com