样本数据:
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")