如何绘制每个属性的边际分布?

问题描述

我正在尝试为c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")的三个"Species"中的每个iris绘制每个属性"Species"的边际分布。本质上,对于每个ks,我需要4个边际分布图。我尝试使用attach(iris) library(ks) library(rgl) library(misc3d ) s <- levels(iris$Species) fhat <- kde(x=iris[iris$Species == s[1],2]) plot(fhat,cont=50,xlab="Sepal length",main="Setosa") 包,但似乎无法将它们拆分为单独的种类。

我使用了以下内容:

print (df2)
   Length
0  541.56
1  541.73
2  482.22
3  500.00 <- whole number for better sample 

是否有办法将其循环生成所需的12个图?如何绘制二维尺寸?

解决方法

使用ggplot,您可以在一张图中布置所有密度。为此,您需要首先将数据转换为长格式,然后可以通过变量和Species facet

library(tidyverse)
iris %>%
  pivot_longer(Sepal.Length:Petal.Width) %>%
  ggplot() +
  geom_density(aes(x = value)) +
  facet_wrap(~ name + Species,scales = "free")

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...