faced_grid和ggplot中y轴标签的对齐?

krcsximq  于 2023-04-18  发布在  其他
关注(0)|答案(3)|浏览(130)

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

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

我知道,通过把空格和使用一个单一的空间字体,我可以解决这个问题,但我必须使用字体'校准轻'。

c0vxltue

c0vxltue1#

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

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

deikduxw

deikduxw2#

我想一个选择是在右边画标签,并在gtable中移动该列

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

g <- ggplotGrob(p)
id1 <- unique(g$layout[grepl("axis-l", g$layout$name),"l"])
id2 <- unique(g$layout[grepl("axis-r", g$layout$name),"l"])
g2 <- gridExtra::gtable_cbind(g[,seq(1,id1-1)],g[,id2], g[,seq(id1+1, id2-1)], g[,seq(id2+1, ncol(g))])

library(grid)
grid.newpage()
grid.draw(g2)
fnvucqvd

fnvucqvd3#

这看起来像是ggplot 2中的一个bug,或者至少是我认为不受欢迎/意外的行为。你可能已经看到了approach suggested here,它在单空格字体上使用字符串填充来实现对齐。
这是相当hacky,但如果你需要实现对齐使用一个特定的字体,你可能会取代轴标签完全与geom_text.我有一个大部分工作的解决方案,但它是丑陋的,因为每一步似乎打破别的东西!

library(ggplot2); library(dplyr)

# To add a blank facet before 2014, I convert to character
grouped$yil = as.character(grouped$yil)

# I add some rows for the dummy facet, in year "", to use for labels
grouped <- grouped %>%
  bind_rows(grouped %>%
              group_by(gruplar) %>%
              slice(1) %>% 
              mutate(yil = "",
                     value = NA_real_) %>%
              ungroup())

p <- ggplot(grouped,
            aes(ay,gruplar,fill=value)) +
  geom_raster(aes(fill = value),na.rm = T) +
  scale_x_continuous(breaks = 4*0:3) +
  facet_grid(anagruplar~yil,
             scales = "free",space = "free",switch = "y") + 
  theme_minimal(base_size = 14) +
  labs(x="",y="") + 
  theme(strip.placement = "outside",
        strip.text.y = element_text(angle = 90),
        axis.text.y.left = element_blank(),
        panel.grid = element_blank()) +

  geom_text(data = grouped %>%
              filter(yil == ""),
            aes(x = -40, y = gruplar, label = gruplar), hjust = 0) +
  scale_fill_continuous(na.value = "white")
p

(The最后一个问题,我可以看到这个图是,它显示了一个孤立的“0”的x轴上的虚拟方面。需要另一个黑客来摆脱这一点!)

相关问题