可以使用R中的Likert包,通过添加ggplot“主题”命令来更改绘图中的字体大小。但是,当条形图和直方图同时绘制时,这些更改不会影响生成的图形。有没有一种方法可以修改字体大小,同时仍然绘制条形图和直方图?例如,以下代码将成功修改轴文本:
likert.bar.plot(items, legend.position = "none") +
theme(text = element_text(size = rel(6), colour = "red"),
axis.text.y = element_text(colour = "blue",
family = "Courier"))`..。但是下面的代码不会:
plot(items, include.histogram=T, legend.position = "none") +
theme(text = element_text(size = rel(6), colour = "red"),
axis.text.y = element_text(colour = "blue",
family = "Courier"))`这个问题解释了How do I change the text font, size and colour of all the different texts in the R package, Likert?的基础知识
发布于 2016-07-23 04:06:44
问题是plot.likert正在使用grid包来组合绘图。这意味着它不会返回任何可以修改的对象(自己尝试一下,保存输出--它返回NULL)。但是,当只打印一个绘图时,它会返回带有ggplot类的绘图(以及其他类),从而允许theme和其他ggplot函数对其进行操作。
如果您希望在单个函数调用中执行此操作,则可能需要自己编辑plot.likert的代码。或者,可能更健壮/灵活:您可能希望自己查看grid包,以组合绘图。
例如,:
data(pisaitems)
items29 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q']
names(items29) <- c("Magazines", "Comic books", "Fiction",
"Non-fiction books", "Newspapers")
l29 <- likert(items29)
a <-
likert.bar.plot(l29, legend.position = "none") +
theme(text = element_text(size = rel(6), colour = "red"),
axis.text.y = element_text(colour = "blue",
family = "Courier"))
b <-
likert.histogram.plot(l29, legend.position = "none") +
theme(text = element_text(size = rel(6), colour = "red"),
axis.text.y = element_text(colour = "blue",
family = "Courier")) +
theme(axis.text.y = element_blank())
library(gridExtra)
grid.arrange(a,b,widths=c(2,1))(请注意,其中包含了一个MWE,以便其他人也可以运行代码。)
(感谢@eipi10提供更清晰的代码来组合它们)

https://stackoverflow.com/questions/38533995
复制相似问题