带有 dplyr 和 ggplot - R 的独特轴标签
Unique axis labels with dplyr and ggplot - R
我正在使用 purrr
制作多个 ggplot2
图。如何使用数字字符串为每个 x 轴指定唯一的名称?
我最好的尝试(下面)只能得到字符串的第一个值,所以所有 3 个图的 x 轴都显示 1% explained variance
,而我想要 [=13] 的三个不同名称=]、2% explained variance
和 3% explained variance
。谢谢
library(tidyverse)
new<-c(1,2,3)
iris%>%
split(.$Species) %>%
purrr::map2(.y = names(.),
~ ggplot(data=., aes(x=Sepal.Length, y=Sepal.Width))+
geom_point()+
labs(x=paste(round(new,2),'% explained variance', sep=''))
)
在map2
中,我们可以将.y
指定为seq_along(.)
,然后用它来索引'new',因为'new'是一个长度向量3.
l1 <- iris %>%
split(.$Species) %>%
map2( seq_along(.), ~
ggplot(data=., aes(x=Sepal.Length, y=Sepal.Width))+
geom_point()+
labs(x=paste(round(new[.y],2),'% explained variance', sep='')))
注意:如果我们不使用 names
,则只需将 'new' 作为 .y
传递(此处未使用 list
元素的名称)
这些图可以保存为一个 pdf
library(gridExtra)
l2 <- map(l1, ggplotGrob)
ggsave(marrangeGrob(grobs = l2, nrow = 1, ncol =1), file = "plots.pdf")
或者将其另存为单个 png 并在同一页上显示多个绘图
ggsave(marrangeGrob(grobs = l2, nrow = 3, ncol =1), file = "plots.png")
我正在使用 purrr
制作多个 ggplot2
图。如何使用数字字符串为每个 x 轴指定唯一的名称?
我最好的尝试(下面)只能得到字符串的第一个值,所以所有 3 个图的 x 轴都显示 1% explained variance
,而我想要 [=13] 的三个不同名称=]、2% explained variance
和 3% explained variance
。谢谢
library(tidyverse)
new<-c(1,2,3)
iris%>%
split(.$Species) %>%
purrr::map2(.y = names(.),
~ ggplot(data=., aes(x=Sepal.Length, y=Sepal.Width))+
geom_point()+
labs(x=paste(round(new,2),'% explained variance', sep=''))
)
在map2
中,我们可以将.y
指定为seq_along(.)
,然后用它来索引'new',因为'new'是一个长度向量3.
l1 <- iris %>%
split(.$Species) %>%
map2( seq_along(.), ~
ggplot(data=., aes(x=Sepal.Length, y=Sepal.Width))+
geom_point()+
labs(x=paste(round(new[.y],2),'% explained variance', sep='')))
注意:如果我们不使用 names
,则只需将 'new' 作为 .y
传递(此处未使用 list
元素的名称)
这些图可以保存为一个 pdf
library(gridExtra)
l2 <- map(l1, ggplotGrob)
ggsave(marrangeGrob(grobs = l2, nrow = 1, ncol =1), file = "plots.pdf")
或者将其另存为单个 png 并在同一页上显示多个绘图
ggsave(marrangeGrob(grobs = l2, nrow = 3, ncol =1), file = "plots.png")