如何从 R 命令行保存直方图

发布于 2024-12-23 13:12:48 字数 893 浏览 3 评论 0原文

我正在尝试将直方图从我的虚拟机保存到 R 中的文件中。

我使用以下 R 代码:

> pdf("graph1.pdf")
> hist(nchar(as.character(m1$qf)),main="First name search 11-14 and 11-15",
  xlab="length of     name")
> dev.off()
null device 
      1 

我得到响应: null device 1

如果我只是运行 hist(nchar(as.character(m1$qf)),main="First name search 11-14 和 11-15",xlab="名称长度") 在命令行中我看到了正确的直方图。

但是当保存为 pdf 时,我得到的东西看起来像这样:

ET
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 41.76 160.01 Tm (500000) Tj
ET
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 41.76 249.50 Tm (1000000) Tj
ET
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 41.76 342.32 Tm (1500000) Tj
ET
Q q 59.04 73.44 414.72 371.52 re W n
0.000 0.000 0.000 RG
0.75 w
[] 0 d
1 J
1 j
10.00 M
74.40 87.20 16.00 156.65 re S
90.40 87.20 16.00 20.71 re S
106.40 87.20 16.00 86.75 re S

这不是我期望的直方图。如何将直方图保存到文件?

I am trying to save a histogram to file in R from my Virtual Machine.

I use the following R code:

> pdf("graph1.pdf")
> hist(nchar(as.character(m1$qf)),main="First name search 11-14 and 11-15",
  xlab="length of     name")
> dev.off()
null device 
      1 

I get the response: null device 1

If I just run the hist(nchar(as.character(m1$qf)),main="First name search 11-14 and 11-15",xlab="length of name") in the command line I see the correct histogram.

But when saved to pdf, I get something that looks something like this:

ET
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 41.76 160.01 Tm (500000) Tj
ET
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 41.76 249.50 Tm (1000000) Tj
ET
BT
/F2 1 Tf 0.00 12.00 -12.00 0.00 41.76 342.32 Tm (1500000) Tj
ET
Q q 59.04 73.44 414.72 371.52 re W n
0.000 0.000 0.000 RG
0.75 w
[] 0 d
1 J
1 j
10.00 M
74.40 87.20 16.00 156.65 re S
90.40 87.20 16.00 20.71 re S
106.40 87.20 16.00 86.75 re S

That's not the histogram I was expecting. How do I save a histogram to file?

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

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

发布评论

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

评论(2

迷乱花海 2024-12-30 13:12:48

如果您不熟悉 R 绘图,我建议您尽早开始使用 ggplot2

library(ggplot2)
data=data.frame(x=rnorm(100))
plot=qplot(x, data=data, geom="histogram") 
ggsave(plot,file="graph1.pdf")

If you are new to plotting in R, I recommend getting an early start on ggplot2

library(ggplot2)
data=data.frame(x=rnorm(100))
plot=qplot(x, data=data, geom="histogram") 
ggsave(plot,file="graph1.pdf")
感情旳空白 2024-12-30 13:12:48

R,Rscript,将直方图保存到文件中,例如:foobar.png

library(ggplot2)
data(PlantGrowth)
png("foobar.png")
hist(PlantGrowth$weight)
dev.off()

它会在与包含以下图像的 R 脚本相同的目录中生成 foobar.png(前提是您在图像编辑器中打开它,而不是文字处理器):

在此处输入图像描述

R, Rscript, save histogram to file like: foobar.png:

library(ggplot2)
data(PlantGrowth)
png("foobar.png")
hist(PlantGrowth$weight)
dev.off()

Which produces a foobar.png in the same directory as the R script which contains the below image (provided you open it up in an image editor, not a word processor):

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文