本文介绍了y轴标签在faced_grid和ggplot中的对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用ggplot和faced_grid函数,我试图制作一个热图.我有一个y轴分类,我希望y轴标签保持左对齐.当我使用theme(axis.text.y.left = element_text(hjust = 0))时,每个面板的标签都是独立对齐的.这是代码:

By using ggplot and faced_grid functions I'm trying to make a heatmap. I have a categorical y axis, and I want y axis labels to be left aligned. When I use theme(axis.text.y.left = element_text(hjust = 0)), each panels' labels are aligned independently. Here is the code:

#data
set.seed(1)
gruplar <- NA
for(i in 1:20) gruplar[i] <- paste(LETTERS[sample(c(1:20),sample(c(1:20),1),replace = T) ],
                                   sep="",collapse = "")

gruplar <- cbind(gruplar,anagruplar=rep(1:4,each=5))
tarih <- data.frame(yil= rep(2014:2019,each=12) ,ay =rep_len(1:12, length.out = 72))

gruplar <- gruplar[rep(1:nrow(gruplar),each=nrow(tarih)),]
tarih <- tarih[rep_len(1:nrow(tarih),length.out = nrow(gruplar)),]

grouped <- cbind(tarih,gruplar)
grouped$value <- rnorm(nrow(grouped))

#plot
p <- ggplot(grouped,aes(ay,gruplar,fill=value))
p <- p + facet_grid(anagruplar~yil,scales = "free",
                    space = "free",switch = "y") 
p <- p + theme_minimal(base_size = 14) +labs(x="",y="") + 
  theme(strip.placement = "outside",
        strip.text.y = element_text(angle = 90))
p <- p + geom_raster(aes(fill = value), na.rm = T)
p + theme(axis.text.y.left = element_text(hjust = 0, size=14))

我知道通过放置空格并使用等宽字体可以解决问题,但是我必须使用'Calibri Light'字体.

I know that by putting spaces and using a mono-space font I can solve the problem, but I have to use the font 'Calibri Light'.

推荐答案

挖坑不是我最喜欢的黑客,但是它可以在这里发挥作用:

Digging into grobs isn't my favourite hack, but it can serve its purpose here:

# generate plot
# (I used a smaller base_size because my computer screen is small)
p <- ggplot(grouped,aes(ay,gruplar,fill=value)) + 
  geom_raster(aes(fill = value),na.rm = T) + 
  facet_grid(anagruplar~yil,scales = "free",space = "free",switch = "y") + 
  labs(x="", y="") +
  theme_minimal(base_size = 10) +
  theme(strip.placement = "outside",
        strip.text.y = element_text(angle = 90),
        axis.text.y.left = element_text(hjust = 0, size=10))

# examine ggplot object: alignment is off
p 

# convert to grob object: alignment is unchanged (i.e. still off)
gp <- ggplotGrob(p)
dev.off(); grid::grid.draw(gp)

# change viewport parameters for left axis grobs
for(i in which(grepl("axis-l", gp$layout$name))){
  gp$grobs[[i]]$vp$x <- unit(0, "npc")     # originally 1npc
  gp$grobs[[i]]$vp$valid.just <- c(0, 0.5) # originally c(1, 0.5)
}

# re-examine grob object: alignment has been corrected
dev.off(); grid::grid.draw(gp)

这篇关于y轴标签在faced_grid和ggplot中的对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 22:55