2
下面是哈德利韦翰的书2D密度图:添加一行二维密度图中的R使用GGPLOT2
f2d <- with(faithful, MASS::kde2d(eruptions, waiting,
h = c(1, 10), n = 50))
df <- with(f2d, cbind(expand.grid(x, y), as.vector(z)))
names(df) <- c("eruptions", "waiting", "density")
erupt <- ggplot(df, aes(waiting, eruptions, fill = density)) +
geom_tile() +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))
erupt + scale_fill_gradient(limits = c(0, 0.04), low = "white", high = "black")
我尝试绘图在此之上的行:
mydf <- data.frame(x=c(50,60,70),y=c(2,3,4))
erupt + scale_fill_gradient(limits = c(0, 0.04), low = "white", high = "black") + geom_line(aes(x=x,y=x), data = mydf)
,但我得到了以下错误:
Error in data.frame(x = c(50, 60, 70), y = c(50, 60, 70), fill = function (x, :
arguments imply differing number of rows: 3, 0
我不明白这是为什么。谢谢。
您在'ggplot'设置'fill'全球和'geom_line'找不到变量*密度*在'mydf'中。除此之外,您可以将'inherit.aes = FALSE'添加到'geom_line'。 – aosmith