R: Producing several barcharts with ggplot2 and lapply: how to insert the subtitles according to a list?

烈酒焚心 提交于 2019-12-11 01:04:32

问题


My aim is to produce and save several bar plots with lapply and ggplot2. For this Purpose, I have created a list out of my data. Now everything works fine apart from the subtitles: I would like to insert the names of the elements of my list into the graphs. So far I could only insert the name of the first element.

I have found another post, which helped me a lot to get so far. I'm new here, so I hope I'm posting this question in the right way (I haven't found an option to relate to this other post).

I adapted this Code from this question because I have a follow-up question to the case provided there.

###creating some random data:

df <- data.frame(value = floor(runif(20,min=0,max=30)), 
                 Intervall = paste("Intervall",rep(1:10,2)), type = rep(c("a", "b")))

list1 <- split(df, df$type)

###producing plots with lapply and ggplot

plots <- lapply(list1, function(x) {
                       ggplot(x, aes(Intervall, value)) + 
                        geom_bar(stat="identity") + 
                        labs(title="Intervalle", subtitle =names(list1))})
lapply(names(plots), 
       function(x) ggsave(filename=paste(x,".emf",sep=""), plot=plots[[x]]))

The elements of my list are called a and b. Now the first graph should have the subtitle "a", and the second graph the subtitle "b".

How can I do so? (also how can I first see my plots in the console before saving them?)

With names(list1) "a" becomes the subtitle for both graphs…


回答1:


The issue is not related to the second command. It comes from the creation of 'plots'. In the subtitle, we are passing the whole names(list1) instead of corresponding elements. If we loop through the names of 'list1', it becomes easier to get the corresponding name for each list element, also, the list can be subsetted based on the same names

plots <- lapply(names(list1), function(nm) {
                       ggplot(list1[[nm]], aes(Intervall, value)) + 
                        geom_bar(stat="identity") + 
                        labs(title="Intervalle", subtitle =nm)})
names(plots) <- names(list1)

Now, we use the same command as in the OP (changed the .emf to .png

lapply(names(plots), function(nm) ggsave(filename =
       paste(path, nm, ".png", sep=""), plot = plots[[nm]]))

-plots




回答2:


Consider by in place of split + lapply and use type in subtitle and filename arguments.

# NAMED LIST OF PLOTS
plots <- by(df, df$type, function(sub) {
            p <- ggplot(sub, aes(Intervall, value)) + 
                  geom_bar(stat="identity") + 
                  labs(title="Intervalle", subtitle = sub$type[1])

            ggsave(filename=paste0(sub$type[1],".emf"), plot=p)

            return(p)
         })


来源:https://stackoverflow.com/questions/56717569/r-producing-several-barcharts-with-ggplot2-and-lapply-how-to-insert-the-subtit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!