2016-12-12 14 views
9

plot.title, plot.subtitle ve plot.caption'u yatay bir ggplot2 bar şemasına hizalamak istiyorum.Yatay ggplot çubuğu için başlığın, altyazının ve altyazının hizalanması

Örnek:

library("ggplot2") # ggplot2 2.2 
df <- data.frame(type=factor(c("Brooklyn", 
           "Manhatten and\n Queens")), 
       value=c(15,30)) 

# manual hjust for title, subtitle & caption 
myhjust <- -0.2 

ggplot(df, 
     aes(x=type, y=value)) + 
    geom_bar(stat='identity') + 
    coord_flip() + 
    labs(
    title = "This is a nice title", 
    subtitle = "A subtitle", 
    caption = "We even have a caption. A very long one indeed.") + 
    theme(axis.title=element_blank(), 
     plot.title=element_text(hjust = myhjust), 
     plot.subtitle=element_text(hjust = myhjust), 
     plot.caption=element_text(hjust = myhjust)) 

Ben tüm 3 labs elemanları (plot.title, plot.subtitle ve plot.caption) hizaya nasıl nerede axis.text başlar (kırmızı dikey çizgi, Manhatten "M")?

Ayrıca: myhjust düzeltmesi neden plot.title, plot.subtitle ve plot.caption için 3 farklı yatay konumda sonuçlanır? Ayrıca

Problem

+2

Bu büyük bir soru, iyi bir şekilde açıklanmıştır ve açıkça açıkladı. Sık sık ggplot ile karşılaştığım bir sorundur. Aşağıdaki grid.arrange() alternatifi işe yararken, bu soruya tam bir cevap değil. Örneğin, aynı hjust değeri neden başlığı, altyazıyı ve altyazıyı aynı şekilde etkilemiyor? Cevabın, referans noktasının, her durumda metin dizesinin ortası olduğunu düşünüyorum. Bu, merkezi doğrulanmış metin için mantıklı olsa da, arsa alanının ötesinde sol ya da sağa yaslama için çalışmaz. Bu basit bir ayar olması gerektiği gibi görünüyor. – jzadra

cevap

5

iken sen olabilir düzenlemek bu üç Grobs, sen tıpkı:

library(gridExtra) 
library(grid) 

grid.arrange(
    textGrob("This is a nice title", 
      gp=gpar(fontsize=16, col="#2b2b2b"), 
      x=unit(0.005, "npc"), just=c("left", "bottom")), 
    textGrob("A subtitle", 
      gp=gpar(fontsize=12, col="#2b2b2b"), 
      x=unit(0.005, "npc"), just=c("left", "bottom")), 
    ggplot(df, aes(x=type, y=value)) + 
    geom_bar(stat='identity') + 
    coord_flip() + 
    theme(axis.title=element_blank()), 
    textGrob("We even have a caption. A very long one indeed.", 
      gp=gpar(fontsize=9, col="#2b2b2b"), 
      x=unit(0.005, "npc"), just=c("left", "bottom")), 
    ncol=1, 
    heights=c(0.075, 0.025, 0.85, 0.05) 
) 

, bunun için bir sarıcı olun kişisel pkg koyun. Boom. Bitti.


library(ggplot2) 
library(gridExtra) 
library(grid) 

df <- data.frame(type=factor(c("Brooklyn","Manhatten and\n Queens")), value=c(15,30)) 

ggplot(df, aes(x=type, y=value)) + 
    geom_bar(stat='identity') + 
    coord_flip() + 
    theme(axis.title=element_blank()) + 
    theme(plot.margin=margin(l=0, t=5, b=5))-> gg 

flush_plot <- function(x, title, subtitle, caption) { 
    tg <- function(label, ...) { 
    textGrob(label, x=unit(0, "npc"), just=c("left", "bottom"), 
      gp=do.call(gpar, as.list(substitute(list(...)))[-1L])) } 
    grid.arrange(
    tg(title, fontsize=16, col="#2b2b2b"), 
    tg(subtitle, fontsize=12, col="#2b2b2b"), x, 
    tg(caption, fontsize=9, col="#2b2b2b"), 
    ncol=1, heights=c(0.075, 0.025, 0.85, 0.05) 
) 
} 

flush_plot(gg, "This is a nice title", "A subtitle", 
      "We even have a caption. A very long one indeed.") 
+0

Bu başlık, altyazı ve altyazıyı hizalar, ancak y ekseni etiketlerini değil, görünür. – Chrisss

+0

'plot.margin = birim (c (0,0,0,0)," satır ")' düzeltirdi – baptiste

+1

@hrbrmstr Büyük cevap! Ama dürüst olmak gerekirse: Daha az kodla olsa da saf ggplot2 çözümünü tercih ederim. –

İlgili konular