Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 调整打印轴标签以适合.pdf_R_Pdf_Graphics_Axis Labels - Fatal编程技术网

R 调整打印轴标签以适合.pdf

R 调整打印轴标签以适合.pdf,r,pdf,graphics,axis-labels,R,Pdf,Graphics,Axis Labels,我想将柱状图另存为.pdf,但当我这样做时,并非所有x轴标签都可见。是否有一种方法可以自动调整绘图的大小,以便所有标签都能很好地匹配并读取?非常感谢您的帮助 # Example data dd <- iris dd$Species <- as.character(dd$Species) dd$Species[dd$Species=="setosa"] <- "setosa and more text that should also fit in the pdf" dd$Spe

我想将柱状图另存为.pdf,但当我这样做时,并非所有x轴标签都可见。是否有一种方法可以自动调整绘图的大小,以便所有标签都能很好地匹配并读取?非常感谢您的帮助

# Example data
dd <- iris
dd$Species <- as.character(dd$Species)
dd$Species[dd$Species=="setosa"] <- "setosa and more text that should also fit in the pdf"
dd$Species[dd$Species=="versicolor"] <- "versicolor and more text that should also fit in the pdf"
dd$Species[dd$Species=="virginica"] <- "virginica and more text that should also fit in the pdf"
dd$Species <- as.factor(dd$Species)

# Plotting & saving as .pdf
windows()
plot(dd$Species)
dev.copy(pdf, file="%/test.pdf") # % is the directory in my computer
dev.off()
#示例数据

dd如果问题是您需要在每个标签上提供更多信息和更多文本,您可以使用下一行

dd <- iris
dd$Species <- as.character(dd$Species)
dd$Species[dd$Species=="setosa"] <- "setosa is the name \n of iris-more text"
dd$Species[dd$Species=="versicolor"] <- "versicolor is the name \n of iris-more text "
dd$Species[dd$Species=="virginica"] <- "virginica is the name \n of iris-more text"
dd$Species <- as.factor(dd$Species)
plot(dd$Species)

dd如果问题是您需要在每个标签上提供更多信息和更多文本,您可以使用下一行

dd <- iris
dd$Species <- as.character(dd$Species)
dd$Species[dd$Species=="setosa"] <- "setosa is the name \n of iris-more text"
dd$Species[dd$Species=="versicolor"] <- "versicolor is the name \n of iris-more text "
dd$Species[dd$Species=="virginica"] <- "virginica is the name \n of iris-more text"
dd$Species <- as.factor(dd$Species)
plot(dd$Species)

dd当您打印为pdf时,重要的是pdf的大小,而不是打印,以便标签适合。换句话说,如果你使用

pdf("filename.pdf", width = W)
plot(dd$Species)
dev.off()
如果宽度参数W足够大,您应该得到一个pdf,其中的条足够宽,以便所有标签都可见

然而,这在美学上可能并不令人满意,在这种情况下,您可能需要尝试使用ggplot2。这将使您能够更轻松地使用标签。例如,可以将所有标签旋转一个角度,以便它们都能很好地配合

library(ggplot2)
ggplot(dd, aes(Species)) + 
    theme(axis.text.x = element_text(angle = 90)) +
    geom_bar()
ggsave("filename.pdf")

您还可以调整标签字体的大小,或使用图例(如果标签太多,这可能是按顺序列出所有标签的一种更简洁的方式-如果在
aes
中使用
fill=Species
,您还可以为每个标签添加不同的颜色)。您可以通过键入
?theme
,了解如何设置这些参数,并且ggplot2还有许多优秀的文档,其中有许多示例。

当您打印到pdf时,重要的是pdf的大小,而不是打印,以便标签适合。换句话说,如果你使用

pdf("filename.pdf", width = W)
plot(dd$Species)
dev.off()
如果宽度参数W足够大,您应该得到一个pdf,其中的条足够宽,以便所有标签都可见

然而,这在美学上可能并不令人满意,在这种情况下,您可能需要尝试使用ggplot2。这将使您能够更轻松地使用标签。例如,可以将所有标签旋转一个角度,以便它们都能很好地配合

library(ggplot2)
ggplot(dd, aes(Species)) + 
    theme(axis.text.x = element_text(angle = 90)) +
    geom_bar()
ggsave("filename.pdf")

您还可以调整标签字体的大小,或使用图例(如果标签太多,这可能是按顺序列出所有标签的一种更简洁的方式-如果在
aes
中使用
fill=Species
,您还可以为每个标签添加不同的颜色)。您可以通过键入
?theme
,了解如何设置这些参数,并且ggplot2还提供了优秀的文档,其中有大量示例。

虽然很有用,但这并不是自动安装感谢您的回答!但是,由于我的级别比示例中的级别多,所以在输出PDF中省略的是实际的级别名称。虽然这很有用,但这并不是自动安装您的答案!但是,由于我的级别比示例中的级别多,所以在输出PDF中省略的是实际的级别名称。非常感谢您的回复!是的,我会试试ggplot。非常感谢您的回复!是的,我会试试ggplot。