Notna
Notna

Reputation: 525

Save plots separately (ggplots2)

I am struggling a little with ggplot and my dataframe that I use as input. I have a dataframe A that looks like this:

   x         y
1  0 50.825022
2  1 44.154257
3  0 50.116500
4  1 46.027000
5  0 55.905105
6  1 50.753209
7  0 44.804500
8  1 42.894000
9  0 15.030799
10 1 11.881330
11 0 21.456833
12 1 18.942833
13 0  5.664676
14 1  3.350577

and I would like to plot each pairs of rows together with a line. For instance, I would like to plot the data points from row 1 (x=0 and y= 50.825002) and row 2 (x=1 and y=44.154257) together on the same plot, and link the two data points with a line. Same thing for the pairs 3-4 and 5-6, etc.

The idea is to obtain 7 different plots that I can export separately. To illustrate, the first plot from the two first rows would be: enter image description here

If someone could hint me the trick with ggplot, I would really appreciate it. Thank you.

Here is the dataframe to copy and paste if needed:

structure(list(x = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 
                      0L, 1L, 0L, 1L), y = c(50.8250223621947, 44.1542573925467, 50.1165, 
                                             46.027, 55.9051046135438, 50.753208962261, 44.8045, 42.894, 15.0307991170913, 
                                             11.8813302333097, 21.4568333333333, 18.9428333333333, 5.66467592950172, 
                                             3.35057697360927)), .Names = c("x", "y"), class = "data.frame", row.names = c(NA, 
                                                                                                                           -14L))

Upvotes: 2

Views: 73

Answers (1)

AntoniosK
AntoniosK

Reputation: 16121

Assume that df is the dataset you posted.

One approach is to create a dataset that will have a column of plots, which are also saved in your working directory as pdf files.

library(tidyverse)

df2 = df %>%
  group_by(g = cumsum(1-x)) %>%               # apply a grouping for every two rows; (0,1) pairs
  nest() %>%                                  # nest data
  mutate(p = map(data, ~ggplot(., aes(x,y))+  # create a plot for each group
                   geom_point()+
                   geom_line()),
         ppdf = map2(p,g, ~ggsave(filename = paste0(.y,".pdf"), plot=.x, device = "pdf")))  # save that plot in your working directory

df2

# # A tibble: 7 x 4
#         g data             p        ppdf  
#     <dbl> <list>           <list>   <list>
#   1     1 <tibble [2 x 2]> <S3: gg> <NULL>
#   2     2 <tibble [2 x 2]> <S3: gg> <NULL>
#   3     3 <tibble [2 x 2]> <S3: gg> <NULL>
#   4     4 <tibble [2 x 2]> <S3: gg> <NULL>
#   5     5 <tibble [2 x 2]> <S3: gg> <NULL>
#   6     6 <tibble [2 x 2]> <S3: gg> <NULL>
#   7     7 <tibble [2 x 2]> <S3: gg> <NULL>

You can see the plots if you do df2$p[[1]], etc. But you also have them as pdf in your working directory.

Another solution is to plot all of them next to each other:

df %>%
  mutate(g = cumsum(1-x)) %>%
  ggplot(aes(x,y))+
  geom_point()+
  geom_line()+
  facet_wrap(.~g)

enter image description here

Of course, you can update the plots to look exactly as you like.

Upvotes: 2

Related Questions