我希望旋转整个图、轴和所有内容,但保持轴标签和标题的原样,以便可以水平阅读。
library(ggplot2)
data(mtcars)
ggplot() + geom_point(data=mtcars,aes(x=mpg,y=cyl)) +
labs(title = "MPG vs Cylinders",
x = "", y = "") +
theme(plot.title = element_text(size=40),axis.text.x=element_text(size=35),axis.text.y=element_text(size=35))
因此,此代码生成的图最好逆时针旋转 30 度左右,如下所示:
但标题仍应水平显示,而不是旋转 30 度。与轴标签相同(我将绘图放在 MS word 中并用小绿色圆圈旋转它)。有什么想法吗?甚至有可能吗?
最佳答案
# install.packages("ggplot2", dependencies = TRUE)
library(ggplot2)
rotation <- 30
p <- ggplot() + geom_point(data=mtcars,aes(x=mpg,y=cyl)) + labs(title = "MPG vs Cylinders", x = "", y = "") + theme(plot.title = element_text(size=20), axis.text.x=element_text(size=15),axis.text.y=element_text(size=15)) + theme(text = element_text(angle=(-1*rotation)))
# install.packages("grid", dependencies = TRUE)
library(grid)
print(p, vp=viewport(angle=rotation, width = unit(.75, "npc"), height = unit(.75, "npc")))
关于旋转整个 ggplot() 而不旋转任何文本 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396168/