轮廓线对应于ggplot2中的变量级 [英] Contour levels corresponding to variable levels in ggplot2

查看:278
本文介绍了轮廓线对应于ggplot2中的变量级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2绘制轮廓图,并且证明比我想象的要难一些。使用> iris 数据集,我可以产生这个图:

  ggplot (虹膜,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(color = .. level ..))



这会产生一个特别奇怪的错误信息:

 警告信息:
`stat_contour()`中的计算失败:
(list)对象不能被强制输入'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中生成等高线图的好方法,其变量本身的值可以生成等高线的等级?



UPDATED



stat_contour 生成数据
library(reshape2)#for melt
volcano3d< - melt(volcano)
$基本图
ggplot(volcano3d,aes(x,y,z = z))+
stat_contour(geom =polygon,aes(fill = .. level ..))

工作得很好,看起来很棒。但是,如果我将它恰好应用于虹膜示例,如下所示:

  ggplot(iris,aes(x = Petal.Width,y = Petal.Length,fill = Sepal.Width))+ 
stat_contour(geom =polygon,aes(fill = .. level ..))

我收到以下错误消息:

 警告消息:
计算在`stat_contour()`中失败:
(list)对象不能被强制输入'double'

这些都是具有相似结构的数据框,所以我无法弄清楚导致这个问题的两者之间有什么不同。

解决方案使用 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()


I am trying to draw contour plot with ggplot2 and it is proving to be a little harder than I imagined. Using the iris dataset I am able to produce this plot:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  stat_density2d(geom="polygon", aes(fill=..level..))

My issue is that I can't seem to figure out how to display- rather than the density values -the raw Sepal.Width values. Here is what I've tried:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, z=Sepal.Width)) +
  geom_tile(aes(fill=Sepal.Width))+
  stat_contour(aes(colour=..level..)) 

This produces an especially odd error message:

 Warning message:
 Computation failed in `stat_contour()`:
 (list) object cannot be coerced to type 'double' 

I also tried this:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  stat_density2d(geom="polygon", aes(fill=Sepal.Width))

And lastly this:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  geom_tile()

Can anyone recommend a good way to produce a contour plot in ggplot2 with the values of the variable itself producing the levels of the contour?

UPDATED

From the stat_contour example:

# 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..))

Work great and looks great. But if I apply this exactly to the iris example like so:

ggplot(iris, aes(x=Petal.Width, y=Petal.Length, fill=Sepal.Width)) +
  stat_contour(geom="polygon", aes(fill=..level..))

I get this error message:

Warning message:
Computation failed in `stat_contour()`:
(list) object cannot be coerced to type 'double'

These are both dataframes with similar structure so I can't figure out what is different between the two causing this issue.

解决方案

The ultimate solution to this way to use the akima package for interpolation then the ggplot2 for final plotting. This is the method I used:

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中的变量级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆