将以下随机数据示例复制到 pdf 时遇到了一个小麻烦。我意识到可以将其复制为 png,也可能复制为其他位图格式,但不能复制为 pdf。
我试图通过这个例子学习如何在绘图上绘制“男性”和“女性”,并用不同的颜色等来显示它们。 我的设备是Windows。
x<- rnorm(100)
y<- x+rnorm(100)
g<- gl(2,50)
g<- gl(2,50, labels = c("Male","Female"))
str(g)
plot(x,y)
# Plot function of (x,y) above will display it but it's not clear who is women and who is men, so I do following steps to plot it with different colors.
# Plotting it with type="n".
plot(x,y, type="n")
points(x[g=="Female"], y[g=="Male"], col = "blue")
points(x[g=="Male"], y[g=="Female"], col = "green", pch=19)
fit<- lm(x~y)
abline(fit)
# Now I try to Copy it to png and that works fine.
dev.copy(png,"myfile.png",width=8,height=6,units="in",res=100)
dev.off()
# Now to pdf. This example doesn't work at all abd pdf won't even open in my pdf viewer.
pdf("myfile.pdf",width=8,height=6)
dev.off()
# So I try this and I am able to open it but only "abline" and "x" and "y" are present not the points I specified for males and females.
dev.copy2pdf(file="Examp1.pdf",out.type = "pdf")
dev.off()
您知道为什么会发生这种情况吗? PNG 可能就足够了,但它也有缺陷。那么知道如何在 R 中将其复制为 pdf 吗?
感谢您的回答。
最佳答案
只需使用:
pdf("myfile.pdf",width=8,height=6)
plot(x,y, type="n")
points(x[g=="Female"], y[g=="Male"], col = "blue")
points(x[g=="Male"], y[g=="Female"], col = "green", pch=19)
fit<- lm(x~y)
abline(fit)
dev.off()
说明:这将打开一个 pdf
设备,对其进行绘图并关闭该设备。
关于r - 将 R 中 type=n 的绘图复制到 PDF 不会复制绘图点,仅在输出 pdf 中显示其线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21334573/