我想在ggplot2中制作热图.我的玩具数据和代码是:
set.seed(12345)
dat <-
data.frame(
Row = rep(x = LETTERS[1:5], times = 10)
, Col = rep(x = LETTERS[1:10], each = 5)
, Y = rnorm(n = 50, mean = 0, sd = 1)
)
library(ggplot2)
p <- ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y), colour = "white") +
scale_fill_gradient(low = "white", high = "steelblue")
p
Run Code Online (Sandbox Code Playgroud)
我希望为这样的范围值设置颜色方案:
-3 <= Y < -2 ---> Dark Blue
-2 <= Y < -1 ---> Blue
-1 <= …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来拉伸两个值之间的颜色渐变并标记图例,而不管数据集中的数据值范围如何.基本上,是否有功能等同ylim() 于颜色渐变?
给定通常在-1和1之间绘制az值的代码,如果中断在数据范围内,我可以绘制并标记渐变:
library(ggplot2)
#generator from http://docs.ggplot2.org/current/geom_tile.html
pp <- function (n, r = 4) {
x <- seq(-r * pi, r * pi, len = n)
df <- expand.grid(x = x, y = x)
df$r <- sqrt(df$x^2 + df$y^2)
df$z <- cos(df$r^2) * exp(-df$r / 6)
return(df)
}
t <- pp(30)
summary(t)
b <- c(-.5, 0, .5)
colors <- c('navyblue', 'darkmagenta', 'darkorange1')
p <- ggplot(data = t, aes(x = x, y = y))+
geom_tile(aes(fill = z))+
scale_fill_gradientn(colors = colors, …Run Code Online (Sandbox Code Playgroud) 我对ggplot2中的线条颜色有疑问.我需要绘制太阳辐射数据,但我只有6小时的数据,所以geom_line不要给出"好"的输出.我试过geom_smooth了,结果接近我的需要.但我有一个新问题,是否可以根据y值改变线条颜色?
用于绘图的代码是
library(ggplot2)
library(lubridate)
# Lectura de datos
datos.uvi=read.csv("serie-temporal-1.dat",sep=",",header=T,na.strings="-99.9")
datos.uvi=within(datos.uvi, fecha <- ymd_h(datos.uvi$fecha.hora))
# geom_smooth
ggplot(data=datos.uvi, aes(x=fecha, y=Rad_Global_.mW.m2., colour="GLOBAL")) +
geom_smooth(se=FALSE, span=0.3)
Run Code Online (Sandbox Code Playgroud)
在所需的输出中,对于250以下的辐射值,250-500区间内的绿色,以及高于500的值,蓝线应为红色.

有可能geom_smooth吗?我试图在这里重用代码,但找不到重点.
用于情节的数据:
dput(datos.uvi)
structure(list(fecha.hora = c(2016012706L, 2016012712L, 2016012718L,
2016012800L, 2016012806L, 2016012812L, 2016012818L, 2016012900L,
2016012906L, 2016012912L, 2016012908L, 2016013000L), latitud = c(37.75,
37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75,
37.75, 37.75), longitud = c(-1.25, -1.25, -1.25, -1.25, -1.25,
-1.25, -1.25, -1.25, -1.25, -1.25, -1.25, -1.25), altitud = c(300L,
300L, …Run Code Online (Sandbox Code Playgroud)