添加标题到使用map()创建的ggplots

时间:2018-01-27 02:32:47

标签: r ggplot2 purrr

使用地图功能向我在下面创建的每个ggplot添加标题的最简单方法是什么?我希望标题反映每个数据框的名称 - 即4,6,8(圆柱体)。

谢谢:)

reset
set terminal win

set ticslevel 0
set xlabel 'Length position'  rotate parallel offset -2,-1
set ylabel 'Width position'   rotate parallel
set zlabel 'Absorbance' rotate parallel
set pm3d
set style fill transparent solid 0.55
set palette color negative
#set view equal xy 
set xtics ("1" 0, "2" 1, "3" 2, "4" 3, "5" 4, "6" 5)
set ytics ("1" 0, "2" 1, "3" 2)

set dgrid3d 40,40 splines
splot 'data_file.txt' matrix w l lw 0.6 lc rgbcolor 'black'


set terminal pdf
set output "name_of_output_file.pdf"
replot 

3 个答案:

答案 0 :(得分:7)

map2names一起使用。

plots <- map2(
  mtcars_split,
  names(mtcars_split),
  ~ggplot(data = .x, mapping = aes(y = mpg, x = wt)) + 
    geom_jitter() +
    ggtitle(.y)
)

编辑:alistaire指出这与imap

相同
plots <- imap(
  mtcars_split,
  ~ggplot(data = .x, mapping = aes(y = mpg, x = wt)) + 
    geom_jitter() +
    ggtitle(.y)
)

答案 1 :(得分:0)

也许您有兴趣使用facet_wrap代替

ggplot(mtcars, aes(y=mpg, x=wt)) + geom_jitter() + facet_wrap(~cyl)

答案 2 :(得分:0)

您可以使用purrr::map2()

mtcars_split <- mtcars %>% split(mtcars$cyl)

plots <- map2(mtcars_split, titles, 
  ~ ggplot(data=.x, aes(mpg,wt)) + geom_jitter() + ggtitle(.y)
)

修改

抱歉与保罗回答重复。