我想知道是否可以通过某种方式访问ggplot2标题图中提供的数据的列。所以像这样:
ggplot(mpg %>% filter(manufacturer == 'audi'),
aes(x = hwy, y = displ, label = model)) +
geom_point() +
geom_text(data = . %>% filter(hwy > 28)) +
ggtitle(unique(.$manufacurer))
Run Code Online (Sandbox Code Playgroud)
我经常像上面的示例中那样创建图以仅绘制子集,并且希望为该子集自动标注。目前,.不会在内识别出,ggtitle而是在geom_text其中起作用。
编辑:
由于我得到了一个很好的注释,并从@Brian标记为重复项,是否有解决方案可以{}在dplyr::group_by函数中使用此技巧?这不起作用。我很乐意为每个组创建单独的图,但是不知何故,只有完整的数据框可以ggplot调用。
mpg %>%
group_by(manufacturer) %>% {
ggplot(., aes(cyl, displ, color=manufacturer)) +
geom_point() +
ggtitle(unique(.$manufacturer))
}
Run Code Online (Sandbox Code Playgroud)
它说奥迪,但在单个图中打印所有制造商。
有没有办法在命令中强制自动换行ggtitle?
作为闪亮应用程序的一部分,我使用不同的绘图和标题的输入变量。不幸的是,其中一些变量值太长而无法显示。
我通过\n提前手动插入(或换行)遇到了一种可能性(详细信息在这里:改进使用 \n 并向右延伸的 ggplot 中多行标题的编辑)。在那里,ggtitle(paste("", title, "", sep = "\n"))会建议类似的东西。
由于包含\n容易出错并且对于不同的绘图大小不灵活,我正在寻找另一种可能性。
这是一个最小的工作示例(改编自上面链接的 lawyeR 问题):
DF <- data.frame(x = rnorm(400))
title_example <- "This is a very long title describing the plot in its details. The title should be fitted to a graph, which is itself not restricted by its size."
plot <- ggplot(DF, aes(x = x)) + geom_histogram() +
ggtitle(title_example)
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到一张图片:https : //i.stack.imgur.com/6MMKF.jpg
您是否知道如何使sep = …