Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

In the below code I am getting the error as:

"Error: Aesthetic must be a one-sided formula, call, name, or constant."

Here is the code: 


multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {

  # Make a list from the ... arguments and plotlist

  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout

  if (is.null(layout)) {

    # Make the panel

    # ncol: Number of columns of plots

    # nrow: Number of rows needed, calculated from # of cols

    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),

                     ncol = cols, nrow = ceiling(numPlots/cols))


  if (numPlots==1) {


  } else {

    # Set up the page


    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location

    for (i in 1:numPlots) {

      matchidx <- == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,layout.pos.col = matchidx$col))




# plot the first variable against all others

plotList <- list()

for (i in 1:99) {

plotList[[i]] <- ggplot(data = data, aes_(x = data[,100-i], y = data[100])) + geom_point() + xlab("x") + ylab("y")


# actually draw the multiplot

multiplot(plotlist = plotList, cols = 3)

1 Answer

0 votes
by (108k points)

You can create a small-multiples plot of one variable plotted against every other variable  in your data, then components of the tidyverse (tidyr and ggplot2) are a much simpler approach then trying to manually create the grid.



# plot all numeric variables against year

longer_gapminder <- gapminder %>%

  pivot_longer(c(-year, -country, -continent),

               names_to = "variable",

               values_to = "value")

longer_gapminder %>% 

  ggplot(aes(year, value)) + 

  geom_point() + 

  facet_wrap(~variable, scales = "free_y")

If you want to know more about R then do check out the R programming tutorial that will help you in learning R from scratch. 

Browse Categories