Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate entire ggplot() without rotating any text R

I am looking to rotate the entire plot, axis and all, but keeping the axis labels and title how they are so they can be read horizontally.

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

So the plot that this code generated would ideally be rotated 30 degrees or so counter-clockwise like so: enter image description here

But the title should still be displayed horizontal, instead of with a 30 degree turn. Same with the axis labels (I put the plot in MS word and rotated it with the little green circle). Any thoughts? Is it even possible?

like image 588
James Avatar asked Feb 08 '23 16:02

James


1 Answers

Would this work for you (code below)

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")))
like image 66
Eric Fail Avatar answered Feb 11 '23 06:02

Eric Fail