ggplot2 多行标题,不同的缩进
ggplot2 multiline title, different indentations
我正在为出版物生成图表,我希望能够在 ggplot 本身中标记图形的面板(而不是导出到发布者等),以便它们在最终的结果中整齐地组合在一起文档。我打算通过在标题中添加一个字母 ("A") 来尝试做到这一点,但我希望我的标题居中并且我想要左上角的字母。
# base graph:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom")
现在,如果我愿意 "fake it" 通过 hand-spacing 每个标题,我可以让它发挥作用,但这似乎很费时间而且很粗糙。
# sloppy solution
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom")
有没有办法单独调用标题的每个 'line' 以获得它自己的 hjust 值?
还有其他创造性的解决方案吗?
此外,我在 mtext (Splitting axis labels with expressions) 中看到了潜力,但无法弄清楚如何使用 ggplot2 来实现它(与基本绘图功能相比……似乎它们不兼容)。
这个 post 很有趣 (),但我对 R 还是个新手,我不知道如何编辑这个聪明的东西来改变 缩进.
谢谢!
您可以使用包 grid
中的 grid.text
,将文本添加到您的绘图中。首先在标题上方添加一些 space 使用 plot.margin
.
library(grid)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom",
plot.margin = unit(c(4, 3, 1, 1), "lines"))
p
grid.text('A', x = unit(0.1, 'npc'), y = unit(.90, 'npc'), gp = gpar(fontsize=28))
最简单的方法可能是使用标题和副标题,
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5) +
labs(title = "A", subtitle = "The Actual Long, Normal Title of Titliness") +
theme(plot.subtitle = element_text(hjust = 0.5))
你可以通过操纵 Grobs 来做到这一点。在 ggplot2
.
之上使用 gtable
和 grid
library("ggplot2")
library("gtable")
library("grid")
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom",
plot.margin = unit(c(4, 1, 1, 4), "lines")) # We need bigger top/left margins to show the letter
gt <- ggplot_gtable(ggplot_build(p)) # Building a gtable from the ggplot object
# Adding the textGrob for the panel letter
panel_letter = textGrob("A", x = 0.5, y = .9,
just = c("left", "top"),
gp = gpar(fontsize = 40, col = "black", face="bold"))
gt <- gtable_add_grob(gt, panel_letter, t=1, l=1, r=1) # Append the Grob to the table
p <- grid.draw(gt) # Display the plot
ggsave("plot.png",gt, width=10, height=10) # If you want to save it.
更新:从 ggplot2 3.0.0 开始,现在原生支持绘图标签,
这是我的做法,使用我专门为此目的编写的 cowplot 包。请注意,您会得到一个干净的主题作为奖励。
library(cowplot)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
geom_jitter(size = 3.5) +
ggtitle("The Actual Long, Normal Title of Titliness")
# add one label to one plot
ggdraw(p) + draw_plot_label("A")
# place multiple plots into a grid, with labels
plot_grid(p, p, p, p, labels = "AUTO")
然后您想要使用 save_plot
函数而不是 ggsave
来保存绘图,因为 save_plot
具有默认值和参数,可帮助您获得相对于主题的正确缩放比例,特别是对于网格中的图。
因为 ggplot2 3.0.0
, there is a native way to do this, using the tag
label:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
geom_point() +
labs(title = "The Actual Long, Normal Title of Titliness",
tag = "A")
我正在为出版物生成图表,我希望能够在 ggplot 本身中标记图形的面板(而不是导出到发布者等),以便它们在最终的结果中整齐地组合在一起文档。我打算通过在标题中添加一个字母 ("A") 来尝试做到这一点,但我希望我的标题居中并且我想要左上角的字母。
# base graph:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom")
现在,如果我愿意 "fake it" 通过 hand-spacing 每个标题,我可以让它发挥作用,但这似乎很费时间而且很粗糙。
# sloppy solution
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom")
有没有办法单独调用标题的每个 'line' 以获得它自己的 hjust 值?
还有其他创造性的解决方案吗?
此外,我在 mtext (Splitting axis labels with expressions) 中看到了潜力,但无法弄清楚如何使用 ggplot2 来实现它(与基本绘图功能相比……似乎它们不兼容)。
这个 post 很有趣 (
谢谢!
您可以使用包 grid
中的 grid.text
,将文本添加到您的绘图中。首先在标题上方添加一些 space 使用 plot.margin
.
library(grid)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom",
plot.margin = unit(c(4, 3, 1, 1), "lines"))
p
grid.text('A', x = unit(0.1, 'npc'), y = unit(.90, 'npc'), gp = gpar(fontsize=28))
最简单的方法可能是使用标题和副标题,
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5) +
labs(title = "A", subtitle = "The Actual Long, Normal Title of Titliness") +
theme(plot.subtitle = element_text(hjust = 0.5))
你可以通过操纵 Grobs 来做到这一点。在 ggplot2
.
gtable
和 grid
library("ggplot2")
library("gtable")
library("grid")
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom",
plot.margin = unit(c(4, 1, 1, 4), "lines")) # We need bigger top/left margins to show the letter
gt <- ggplot_gtable(ggplot_build(p)) # Building a gtable from the ggplot object
# Adding the textGrob for the panel letter
panel_letter = textGrob("A", x = 0.5, y = .9,
just = c("left", "top"),
gp = gpar(fontsize = 40, col = "black", face="bold"))
gt <- gtable_add_grob(gt, panel_letter, t=1, l=1, r=1) # Append the Grob to the table
p <- grid.draw(gt) # Display the plot
ggsave("plot.png",gt, width=10, height=10) # If you want to save it.
更新:从 ggplot2 3.0.0 开始,现在原生支持绘图标签,
这是我的做法,使用我专门为此目的编写的 cowplot 包。请注意,您会得到一个干净的主题作为奖励。
library(cowplot)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
geom_jitter(size = 3.5) +
ggtitle("The Actual Long, Normal Title of Titliness")
# add one label to one plot
ggdraw(p) + draw_plot_label("A")
# place multiple plots into a grid, with labels
plot_grid(p, p, p, p, labels = "AUTO")
然后您想要使用 save_plot
函数而不是 ggsave
来保存绘图,因为 save_plot
具有默认值和参数,可帮助您获得相对于主题的正确缩放比例,特别是对于网格中的图。
因为 ggplot2 3.0.0
, there is a native way to do this, using the tag
label:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
geom_point() +
labs(title = "The Actual Long, Normal Title of Titliness",
tag = "A")