旋转整个ggplot()而不旋转任何文本R.

时间:2015-10-28 16:13:50

标签: r ggplot2 rotation

我希望旋转整个绘图,轴和所有,但保持轴标签和标题的方式,以便它们可以水平读取。

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度左右,如下所示: enter image description here

但是标题应该仍然是水平显示,而不是30度转弯。与轴标签相同(我将绘图放在MS字中并用小绿圈旋转)。有什么想法吗?它甚至可能吗?

2 个答案:

答案 0 :(得分:5)

这适用于你(下面的代码)

dd

# 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")))

答案 1 :(得分:3)

这会给出一些警告,但有效:

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"))
))

resulting plot

根据需要进行微调。