ggplot2中变量级别对应的等高线级别
Contour levels corresponding to variable levels in ggplot2
我正在尝试使用 ggplot2 绘制等高线图,事实证明它比我想象的要难一些。使用 iris
数据集,我可以生成此图:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_density2d(geom="polygon", aes(fill=..level..))
我的问题是我似乎无法弄清楚如何显示(而不是密度值)原始 Sepal.Width
值。这是我尝试过的:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, z=Sepal.Width)) +
geom_tile(aes(fill=Sepal.Width))+
stat_contour(aes(colour=..level..))
这会产生一条特别奇怪的错误消息:
Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'
我也试过这个:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_density2d(geom="polygon", aes(fill=Sepal.Width))
最后这个:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
geom_tile()
任何人都可以推荐一种在 ggplot2 中生成等高线图的好方法,其中变量本身的值产生等高线的水平吗?
已更新
来自 stat_contour
示例:
# Generate data
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
# Basic plot
ggplot(volcano3d, aes(x, y, z = z)) +
stat_contour(geom="polygon", aes(fill=..level..))
工作很棒,看起来很棒。但是如果我像这样将它精确地应用到 iris 示例中:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_contour(geom="polygon", aes(fill=..level..))
我收到此错误消息:
Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'
这些都是具有相似结构的数据帧,所以我无法弄清楚导致此问题的两者之间有什么不同。
尝试分解 stat_density2d()
中的 fill
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_density2d(geom="polygon", aes(fill = factor(..level..)))
这种方式的最终解决方案是使用 akima
包进行插值,然后使用 ggplot2
进行最终绘图。这是我使用的方法:
library(ggplot2)
library(akima)
library(dplyr)
interpdf <-interp2xyz(interp(x=iris$Petal.Width, y=iris$Petal.Length, z=iris$Sepal.Width, duplicate="mean"), data.frame=TRUE)
interpdf %>%
filter(!is.na(z)) %>%
tbl_df() %>%
ggplot(aes(x = x, y = y, z = z, fill = z)) +
geom_tile() +
geom_contour(color = "white", alpha = 0.05) +
scale_fill_distiller(palette="Spectral", na.value="white") +
theme_bw()
我正在尝试使用 ggplot2 绘制等高线图,事实证明它比我想象的要难一些。使用 iris
数据集,我可以生成此图:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_density2d(geom="polygon", aes(fill=..level..))
我的问题是我似乎无法弄清楚如何显示(而不是密度值)原始 Sepal.Width
值。这是我尝试过的:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, z=Sepal.Width)) +
geom_tile(aes(fill=Sepal.Width))+
stat_contour(aes(colour=..level..))
这会产生一条特别奇怪的错误消息:
Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'
我也试过这个:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_density2d(geom="polygon", aes(fill=Sepal.Width))
最后这个:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
geom_tile()
任何人都可以推荐一种在 ggplot2 中生成等高线图的好方法,其中变量本身的值产生等高线的水平吗?
已更新
来自 stat_contour
示例:
# Generate data
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")
# Basic plot
ggplot(volcano3d, aes(x, y, z = z)) +
stat_contour(geom="polygon", aes(fill=..level..))
工作很棒,看起来很棒。但是如果我像这样将它精确地应用到 iris 示例中:
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_contour(geom="polygon", aes(fill=..level..))
我收到此错误消息:
Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'
这些都是具有相似结构的数据帧,所以我无法弄清楚导致此问题的两者之间有什么不同。
尝试分解 stat_density2d()
fill
ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
stat_density2d(geom="polygon", aes(fill = factor(..level..)))
这种方式的最终解决方案是使用 akima
包进行插值,然后使用 ggplot2
进行最终绘图。这是我使用的方法:
library(ggplot2)
library(akima)
library(dplyr)
interpdf <-interp2xyz(interp(x=iris$Petal.Width, y=iris$Petal.Length, z=iris$Sepal.Width, duplicate="mean"), data.frame=TRUE)
interpdf %>%
filter(!is.na(z)) %>%
tbl_df() %>%
ggplot(aes(x = x, y = y, z = z, fill = z)) +
geom_tile() +
geom_contour(color = "white", alpha = 0.05) +
scale_fill_distiller(palette="Spectral", na.value="white") +
theme_bw()