繁体   English   中英

如何将y轴标签的位置更改为直接在y轴的顶部?

[英]How to change the position of y axis label to be directly on top of the y axis?

我在将y轴标签直接放在y轴上时遇到麻烦。

我尝试使用margins参数来引导标签。 我正确调整了左右调整,但无法在标签顶部指定标签t = xx的位置到标签顶部。

代码产生下面的图。 如您所见,y轴标签需要进一步向上调整b / c,我希望它直接位于y轴上方。

干杯,

图形


library(tidyverse)
housing <- txhousing %>% group_by(year, city)  %>%
           summarise(total = sum(volume, na.rm = T)) %>% filter(city %in% c("El Paso","Dallas", "Houston"))

dat <- housing
yvar  <- dat$total
xvar <- dat$year
gruppe <- dat$city


ggplot(data = dat, aes(x = xvar, y = yvar/1e6, colour = gruppe)) + geom_line() + theme_classic() + theme(plot.margin = margin(20,0,0,0), axis.title.y = element_text(angle = 0, margin = margin(t = -20, l = 10, r = -40))) + labs(y = "y-label")

您可以假装它只是任何旧文本,然后将其放置在任意位置。

hjustvjustyminxmin vjust ,以将标签准确地放置在所需位置。

library(tidyverse)
library(ggplot2)
library(grid) #grobs come from grid
housing <- txhousing %>% group_by(year, city)  %>%
summarise(total = sum(volume, na.rm = T)) %>% filter(city %in% c("El Paso","Dallas", "Houston"))

dat <- housing
yvar  <- dat$total
xvar <- dat$year
gruppe <- dat$city

p<-ggplot(data = dat, aes(x = xvar, y = yvar/1e6, colour = gruppe)) +
  geom_line() + theme_classic() + 
  theme(plot.margin = margin(50,0,0,0))+ 
  annotation_custom(
    grob = textGrob(label = "y-label", hjust = 0, vjust=-0.9,gp = gpar(cex = 1.0)),
    ymin = (max(yvar/1e6)),
    xmin = min(xvar)-(0.009*min(xvar)))+
  labs(y = NULL)
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off" #this lets you put stuff outside the margins
grid.draw(gt)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM