在同一图上绘制9个图表的最佳方法?

发布于 2025-02-03 20:22:57 字数 456 浏览 2 评论 0原文

试图对玻璃数据集中所有预测值进行直方图。我最初使用par(mfrow = c(3,3))在同一窗口上绘制所有9个预测指标,但遇到了边距太大的错误。没关系。无论如何,写历史记录(玻璃$ x)九次效率低下。

因此,我尝试一次绘制它们。我将数据子集征用,以便仅保留数字列,但是出现了一个错误,说它们不是数字。我已经验证了所有列都是数字。我在做什么错?

library(mlbench)
data(Glass)
library(lattice)

par(mfrow=c(3,3))
hist(Glass$RI)

> Error in plot.new() : figure margins too large

Glass2=subset(Glass[1:9])
hist(Glass2)

> Error in hist.default(Glass2) : 'x' must be numeric

Trying to make a histogram of all the predictor values in the Glass dataset. I initially used par(mfrow=c(3,3)) to plot all 9 predictors on the same window, but got an error that the margins were too large. That's okay. Writing hist(Glass$X) nine times would've been inefficient anyway.

So then I tried to plot them all at once. I subset the data so that only the numeric columns remained, but got an error saying that they weren't numeric. I've already verified all columns are numeric. What am I doing wrong?

library(mlbench)
data(Glass)
library(lattice)

par(mfrow=c(3,3))
hist(Glass$RI)

> Error in plot.new() : figure margins too large

Glass2=subset(Glass[1:9])
hist(Glass2)

> Error in hist.default(Glass2) : 'x' must be numeric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少年亿悲伤 2025-02-10 20:22:58

这是一个整洁的替代方案。

library(tidyr)
library(ggplot2)
library(mlbench)

data(Glass)

Glass %>% 
  pivot_longer(-Type) %>% 
  ggplot(aes(value)) + 
  geom_histogram() + 
  facet_wrap(~name, 
             ncol = 3, 
             scales = "free")

结果:

“

Here's a tidyverse alternative.

library(tidyr)
library(ggplot2)
library(mlbench)

data(Glass)

Glass %>% 
  pivot_longer(-Type) %>% 
  ggplot(aes(value)) + 
  geom_histogram() + 
  facet_wrap(~name, 
             ncol = 3, 
             scales = "free")

Result:

enter image description here

尽揽少女心 2025-02-10 20:22:58

将数据归为新的数据框架。确保您的情节窗口确实很大,否则您会发现保证金太小的错误。

X <- Glass[,1:9]
par(mfrow = c(3, 3))
for (i in 1:ncol(X)) {
  hist(X[ ,i], xlab = names(X[i]), main = paste(names(X[i]), "Histogram"), col="steelblue")  
}

Subset the data into a new data frame, X. Instead of typing out X$x nine times, use a for loop. Make sure your plot window is really big or you'll get an error that the margins are too small.

X <- Glass[,1:9]
par(mfrow = c(3, 3))
for (i in 1:ncol(X)) {
  hist(X[ ,i], xlab = names(X[i]), main = paste(names(X[i]), "Histogram"), col="steelblue")  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文