Estou fazendo um gráfico em ggplot2 e quero economizar espaço no título de quebra de lib do gráfico alinhando-o à esquerda. O problema é que usar hjust
não funciona direito.
library(ggplot2)
chart <- ggplot(
data = cars,
aes(
x = speed,
y = dist
)
) +
geom_point() +
labs(
title = "Here is a very long title that will need a\nlinebreak here",
subtitle = "This subtitle will also have\na linebreak"
) +
theme(
plot.title = element_text(
hjust = -0.1
)
)
chart
ggsave(
filename = "~/Desktop/myplot.png",
plot = chart,
# type = "cairo",
height = 4,
width = 6,
dpi = 150)
Gostaria que "Aqui" e "quebra de linha" se alinhassem com o título do eixo y. Isso é possível apenas com ggplot2?
Você pode usar geom_text
junto com o coord_cartesian(clip = "off")
qual permite desenhar o elemento do gráfico fora do painel do gráfico
library(ggplot2)
ggplot(
data = cars,
aes(x = speed,
y = dist)) +
geom_point() +
labs(subtitle = "This subtitle will also have\na linebreak") +
geom_text(
x = 1,
y = 160,
inherit.aes = FALSE,
label = "Here is a very long title that will need a\nlinebreak here",
check_overlap = TRUE,
hjust = 0,
size = 6
) +
coord_cartesian(clip = "off") +
theme(plot.margin = unit(c(4, 1, 1, 1), "lines"))
Outra forma é usar a ggarrange
partir do egg
pacote que tem o top
argumento que pode ser usado para o título
chart <- ggplot(
data = cars,
aes(
x = speed,
y = dist)) +
geom_point() +
labs(subtitle = "This subtitle will also have\na linebreak")
library(grid)
# devtools::install_github('baptiste/egg')
library(egg)
#> Loading required package: gridExtra
ggarrange(chart,
ncol = 1,
top = textGrob(
"Here is a very long title that will need a\nlinebreak here",
gp = gpar(fontface = 1, fontsize = 14),
hjust = 0,
x = 0.01)
)
Criado em 18/09/2018 pelo pacote reprex (v0.2.1.9000)
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras