Plotting linear trend using the ggplot2 for multiple columns elements
1
0
Entering edit mode
5.1 years ago

Hi,

I have a question, I am using the ggplot2 package for plotting the linear trend using the data frame. The rows contains different timepoints (1, 2, 3) and the columns contain values under the headers (A, B. C, D,.....Z). Using the below code, I can plot only one column per run and save and export it. Is there a way to use or looping all the columns (B, C, D, E,......Z) corresponding to the Timepoints to plot and save it locally on the machine. I have hundreds of columns like this.

Example of a dataframe:

Timepoints  A   B
1   -1.6774235  2.6583222
2   -1.80880562 2.868172
3   -1.8716294  2.8304835

Code:

library(ggplot2)
library(hrbrthemes)
p3 <- ggplot(dataframe, aes(x=Timepoints, y=A)) +
    geom_point() +
    geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
    theme_ipsum()

Thank you,

Toufiq

ggplot2 linear R save looping • 4.7k views
ADD COMMENT
2
Entering edit mode
5.1 years ago

Hey, here is one way to do it via lapply() and aes_string()

Create random data

ggdata <- data.frame(
  Timepoints = c(1:5),
  matrix(rexp(20, rate=.1), ncol=4))
colnames(ggdata) <- c('Timepoints', 'A', 'B', 'C', 'D')

ggdata
  Timepoints         A        B         C         D
1          1  4.989092 5.805021 30.899651 16.831086
2          2 19.720387 8.898303 12.004030 17.947039
3          3  1.007443 3.966031  5.839473  4.721005
4          4 12.472524 8.367984  3.200908 21.200863
5          5 38.603744 2.362598  6.056351 10.085905

Use lapply() over the column names to create a separate plot, and return to a list object, p

library(ggplot2)
library(hrbrthemes)

p <- lapply(
  colnames(ggdata)[2:5],
  function(col) ggplot(ggdata, aes_string(x = 'Timepoints', y = col)) +
    geom_point() +
    geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
    theme_ipsum())

Plot the data

require(cowplot)
plot_grid(
  p[[1]], p[[2]], p[[3]], p[[4]],
  ncol = 4,
  labels = colnames(ggdata)[2:5])

ffff

There is also a way via facet_grid() and facet_wrap() (not shown here).

Kevin

ADD COMMENT
0
Entering edit mode

Thank you so much @Kevin Blighe. This resolved the issue and was very helpful.

Another related query is, I would like to export each of these plots to the .pdf file format to the local drive since I have more elements in the object. i.e

p[[1]], p[[2]], p[[3]], p[[4]], ........, p[[100]]

Is this something possible?

ADD REPLY
1
Entering edit mode

Hey, yes, you can modify the code somewhat so that it will work with any number of plots. Here, I do it for 50 groups:

ggdata <- data.frame(
  Timepoints = c(1:5),
  matrix(rexp(250, rate=.1), ncol=50))
colnames(ggdata) <- c('Timepoints', paste0('Group', 1:50))

library(ggplot2)
library(hrbrthemes)

p <- lapply(
  colnames(ggdata)[2:ncol(ggdata)],
  function(col) ggplot(ggdata, aes_string(x = 'Timepoints', y = col)) +
    geom_point() +
    geom_smooth(method=lm , color="red", fill="#69b3a2", se=TRUE) +
    theme_ipsum())

require(cowplot)
margin <- theme(
  plot.margin = unit(c(0.2, 0.2, 0.2, 0.2), 'cm'),
  plot.title = element_blank(),
  plot.subtitle = element_blank(),
  plot.caption = element_blank())
plots <- lapply(p, '+', margin)

cairo_pdf('out.pdf', width =24, height = 14)
  plot_grid(
    do.call(plot_grid, c(plots, ncol = 10, nrow = 5)),
    ncol = 1)
dev.off()

jjj

Note that the margin part is not required - it just helps to save space. You may also want to break up the plot numbers across multiple pages. Simply run separate plot_grid() commands for that.

ADD REPLY
1
Entering edit mode

Thank you so much Kevin.

ADD REPLY

Login before adding your answer.

Traffic: 2289 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6