旋转整个ggplot()而不旋转任何文本R [英] Rotate entire ggplot() without rotating any text R

查看:162
本文介绍了旋转整个ggplot()而不旋转任何文本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度左右,如下所示:



但标题仍应水平显示,而不是30度转弯。与轴标签一样(我把这个图表放在MS word中,并用小的绿色圆圈将其旋转)。有什么想法吗?甚至可能吗?

解决方案

这会对你有用(下面的代码)


library(ggplot2)

rotation <-30

p < - ggplot()+ geom_point(data = mtcars,aes(x = mpg,y = (plot.title = element_text(size = 20),axis.text.x = element_text(size = 15))+ labs(title =MPG vs Cylinders,x =,y = ,axis.text.y = element_text(size = 15))+ theme(text = element_text(angle =( - 1 * rotation)))
$ b $ install.packages(grid,dependencies = TRUE)
library(grid)
print(p,vp = viewport(angle = rotation,width = unit(.75,npc),height = unit(.75,npc)) )


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:

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?

解决方案

Would this work for you (code below)

# 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆