旋转整个ggplot()而不旋转任何文本R
Rotate entire ggplot() without rotating any text R
我希望旋转整个绘图、轴和所有内容,但保持轴标签和标题不变,以便可以水平阅读。
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 度左右 counter-clockwise,如下所示:
但标题仍应水平显示,而不是 30 度旋转。与轴标签相同(我将绘图放在 MS word 中并用绿色小圆圈旋转)。有什么想法吗?有可能吗?
这给出了一些警告,但有效:
library(ggplot2)
library(grid)
data(mtcars)
grid.newpage()
pushViewport(viewport(angle = -30))
grid.draw(ggplotGrob(
ggplot() + geom_point(data = mtcars,aes(x = mpg,y = cyl)) +
labs(title = "MPG vs Cylinders",
x = "", y = "") +
theme(text = element_text(angle = 30),
plot.margin = unit(c(0.07, 0.08, 0.2, 0.04), "npc"))
))
根据需要微调。
这对你有用吗(下面的代码)
# 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")))
我希望旋转整个绘图、轴和所有内容,但保持轴标签和标题不变,以便可以水平阅读。
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 度左右 counter-clockwise,如下所示:
但标题仍应水平显示,而不是 30 度旋转。与轴标签相同(我将绘图放在 MS word 中并用绿色小圆圈旋转)。有什么想法吗?有可能吗?
这给出了一些警告,但有效:
library(ggplot2)
library(grid)
data(mtcars)
grid.newpage()
pushViewport(viewport(angle = -30))
grid.draw(ggplotGrob(
ggplot() + geom_point(data = mtcars,aes(x = mpg,y = cyl)) +
labs(title = "MPG vs Cylinders",
x = "", y = "") +
theme(text = element_text(angle = 30),
plot.margin = unit(c(0.07, 0.08, 0.2, 0.04), "npc"))
))
根据需要微调。
这对你有用吗(下面的代码)
# 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")))