在ggplot2中左键调整标题或ggtitle的绝对位置 [英] Left-adjust title in ggplot2, or absolute position for ggtitle
问题描述
ggplot(data = economics,aes(x =日期,y =失业))+
geom_line()+
ggtitle(1967年至2007年间美国的失业率)+
xlab()+
ylab(失业[千])
第一次尝试
ggplot(data = economics,aes(x = date,y = unemploy))+ geom_line()+
ggtitle(美国失业多年)+
xlab()+
ylab(失业[千])+
主题(plot.title = element_text(hjust = -0.45,vjust = 2.12)))
Yay成功!但是等等......还有更多......现在我想把标题改为别的。
ggplot(data = economics ,aes(x =日期,y =失业))+
geom_line()+
ggtitle(1967年至2007年间美国的失业率)+
xlab()+
ylab(失业[千])+
主题(plot.title = element_text(hjust = -0.45,vjust = 2.12))
现在我需要调整hjust ...:(b / b)
问题
我怎样才能让标题左对齐(y轴标签左边的几个像素)并且不会影响正确的值?或者hjust和长度之间的关系是什么的字符串?
我试图手动注释根据这个问题,但然后我
谢谢!
直到有人提出更好的解决方案,其中一种方式就像
library( ggplot2)
library(grid)
library(gridExtra)
p < - ggplot(data = economics,aes(x = date,y = unemploy))+
geom_line() +
labs(x = NULL,y =失业[千],title = NULL)
title.grob< - textGrob(
label =美国的失业几年,
x = unit(0,lines),
y = unit(0,lines),
hjust = 0,vjust = 0,
gp = gpar(fontsize = 16))
p1 < - arrangeGrob(p,top = title.grob)
grid.draw(p1)
I would like to left align the title in a plot like this
ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
ggtitle("Unemployment in USA between 1967 and 2007") +
xlab("") +
ylab("Unemployed [thousands]")
First attempt
ggplot(data = economics, aes(x = date, y = unemploy)) + geom_line() +
ggtitle("Unemployment in USA for some years") +
xlab("") +
ylab("Unemployed [thousands]") +
theme(plot.title = element_text(hjust = -0.45, vjust=2.12)))
Yay success! But wait... there's more... now I want to change the title to something else.
ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
ggtitle("Unemployment in USA between 1967 and 2007") +
xlab("") +
ylab("Unemployed [thousands]") +
theme(plot.title = element_text(hjust = -0.45, vjust=2.12))
So now I need to adjust hjust... :(
The question
How can I make the title left justified (a couple of pixels left of the y axis label or so) over and over again without messing with the hjust value? Or what is the relationship between hjust and the length of the string?
I have tried to annotate manually according to this question, but then I got only the title, and nothing else for some reason - and an error.
Thank you!
Until someone comes up with a better solution, one way would be something like
library(ggplot2)
library(grid)
library(gridExtra)
p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
geom_line() +
labs(x = NULL, y = "Unemployed [thousands]", title = NULL)
title.grob <- textGrob(
label = "Unemployment in USA for some years",
x = unit(0, "lines"),
y = unit(0, "lines"),
hjust = 0, vjust = 0,
gp = gpar(fontsize = 16))
p1 <- arrangeGrob(p, top = title.grob)
grid.draw(p1)
这篇关于在ggplot2中左键调整标题或ggtitle的绝对位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!