Search code examples
rloopssavepie-chart

How can I save a number of piecharts in a list?


I am trying to make a function. The function has to provide me a list of piecharts after i give it a data frame with grades.

I am failing to see what's going wrong, if you could give me a little push so I can get on with my report :)

function_pie.chart<- function(data) {
   pie_charts <- list()
   for(i in 1:ncol(data)) {

       slices <- c(count_if(lt(5.5), data[,i]), count_if(gt(5.4),data[,i]))
       lbls <- c("Passed", "Failed")
       pct <- round(slices/sum(slices)*100)
       lbls <- paste(lbls, pct)
       lbls <- paste(lbls,"%",sep="")
       p1 <- eval(substitute(pie(slices,labels = lbls, 
                           col=rainbow(length(lbls)),
                           main=colnames(data[,i]))))
       pie_charts[[i]] <- p1
     }
}

I expect the output to be a list so I can easily plot an image with multiple piecharts. The output on the other hand gives me this error:

"Error in seq.int(x[i], x[i + 1], length.out = n) : 'length.out' must be a non-negative number"

Sample data:

data <- data.frame(
`BSTAT-THI Beschrijvende Statistiek` = c(7.3, 6.3, 5.8, 6.9, 5.7, 8.9, 6.1, 7, 8.1, 7.1, 6.2, 6.5, 7, 4.9, 9, 6.2, 5.6, 7.3, 6.6, 7.7, 8.3, 7.3, 7.1, 5.3, 9.3, 7.4, 6.6, 6.3, 7, 5.7, 6.7, 6.9, 6.9, 6.6, 5.3, 6.5, 5.7, 7.5, 6.3, 6.8, 7.3, 5.9, 7.1, 6.9, 7, 7.6, 7, 7.1, 6.5, 5.2, 5.8, 6.2, 6.4, 6.7, 5.3, 7.3, 7, 7.3, 8.6, 6.2, 5.8, 5.8, 6.6, 7.1, 6, 9.5, 7.3, 8, 7.3, 6.5, 9, 8.2, 7.3, 6.5, 7.8, 7.4, 6.9, 7.3, 6.2, 7.5, 7.6, 7.1, 6.3, 6.9, 7.5, 7.7, 8.3, 7.4, 5.2, 7, 6.6, 6.5, 7.5, 6.3, 8.1, 6.9, 6.8, 8.1, 7.4, 7.2, 9.2, 7.8, 6.6, 6.1, 8.3, 6.2, 9.2, 7.4, 9, 8.6, 7.1, 5.8, 6.5, 6.4, 5.7, 7.1, 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, 8.2, 6.5, 7.2),
`CALEID-TH|Caleidoscoop theorie` = c("7.4000000000000004", "6.2999999999999998", "5.7000000000000002", "7.0999999999999996", "6.0999999999999996", "7.5", "5.5", "6", "5.0999999999999996", "6.5999999999999996", "6.2999999999999998", "6.5", "4.9000000000000004", "NA", "8.5", "6.9000000000000004", "3.2000000000000002", "7.2999999999999998", "4.2999999999999998", "4.7000000000000002", "7.9000000000000004", "8.6999999999999993", "5.2999999999999998", "6", "6.4000000000000004", "5.7999999999999998", "3.8999999999999999", "5.7999999999999998", "7.7000000000000002", "4.7999999999999998", "5.7000000000000002", "7.2000000000000002", "5.2999999999999998", "5.7999999999999998", "5.7999999999999998", "5.5", "5.7000000000000002", "8.9000000000000004", "6.4000000000000004", "5.7999999999999998", "4.7000000000000002", "5", "5.7000000000000002", "4.9000000000000004", "5.0999999999999996", "6", "5.7999999999999998", "5.5999999999999996", "6", "6.2999999999999998", "6.9000000000000004", "6.2000000000000002", "5", "5.5999999999999996", "NA", "8.5999999999999996", "4.2999999999999998", "7", "8.9000000000000004", "5.2000000000000002", "5.2000000000000002", "6", "5.5", "6.7999999999999998", "3.6000000000000001", "5.9000000000000004", "8.9000000000000004", "4.4000000000000004", "6.2000000000000002", "6.2000000000000002", "6.2000000000000002", "5.2999999999999998", "5.5999999999999996", "6", "7.5999999999999996", "6.5", "7.5", "4.2999999999999998", "6.2000000000000002", "8.5999999999999996", "6", "4.7000000000000002", "3.6000000000000001", "8.4000000000000004", "8.9000000000000004", "6.2999999999999998", "8.1999999999999993", "8.8000000000000007", "6.5999999999999996", "4.4000000000000004", "8", "6.5999999999999996", "6.0999999999999996", "6.0999999999999996", "8", "6.2000000000000002", "5.2999999999999998", "6.2000000000000002", "5.7999999999999998", "7.4000000000000004", "6.5", "8", "6.9000000000000004", "4.5", "7.5999999999999996", "6.0999999999999996", "6.0999999999999996", "4.7999999999999998", "6", "5.5999999999999996", "5", "5.4000000000000004", "4.4000000000000004", "5.4000000000000004", "6.2999999999999998", "7.4000000000000004", "9", "6.2000000000000002", "5.5", "6.2000000000000002", "7.7999999999999998", "6", "7", "5.9000000000000004", "6.2000000000000002", "4.2999999999999998", "7.0999999999999996", "5.4000000000000004", "7.5999999999999996", "5", "5.7999999999999998", "6.9000000000000004"),
`COVA1-PRI Communicatieve vaardigheden 1` = c("7", "6", "6", "7.5", "5.5", "7.5", "6", "8", "7.5", "7.5", "6", "6", "7", "6.5", "8", "6.5", "NA", "6.5", "6", "7.5", "7", "7.5", "5.5", "6.5", "6.5", "5", "6", "6", "7", "6", "7.5", "6", "5", "7.5", "6.5", "7.5", "NA", "7.5", "7", "8.5", "8", "6", "7", "8", "6.5", "7.5", "7", "5", "7.5", "6.5", "7.5", "8", "7.7000000000000002", "5.5", "6", "7.5", "7.5", "6.5", "7.5", "6.5", "6", "5.5", "6", "6", "5.5", "6", "8", "6.5", "7.5", "5.5", "6", "7", "8", "8.5", "7.5", "8", "7", "7.5", "8.5", "7.5", "8.5", "7.5", "5.5", "7.5", "7.5", "6", "6.5", "7", "7", "8", "6.5", "7", "8", "NA", "6", "8", "6.5", "8", "7.5", "6.5", "6", "7.5", "7.5", "6.5", "7.5", "6.5", "6.5", "6.5", "6", "8", "6.5", "6", "6", "6", "7", "6", "7.5", "6.5", "NA", "8", "6.5", "7.5", "7", "6", "6", "6.5", "6", "6", "6.5", "6.5", "6", "8"),
stringsAsFactors = FALSE)


Solution

  • The fifth and sixth columns in the dataframe are of character class. For you to perform numeric operations like length, you would need to pass a numeric value. Also, some of the rows in fifth column have 'NA' values. We would also need to handle that.

    Perform the following on your dataframe.

    # Convert NA values to 0
    data[data=="NA"] <- 0
    
    # Convert columns from character to numeric
    data[[5]] <- as.numeric(data[[5]])
    data[[6]] <- as.numeric(data[[6]])
    

    EDIT: Just found that you cannot save pie chart object to variables to use later. One possibility for you is to use the method provided here. Another option would be to use plotly instead.

    # Updated function using plotly
    
    library(plotly)
    
    plotfunc <- function(data) {
        pie_charts <- list()
        for(i in 1:ncol(data)) {
    
            slices <- c(count_if(lt(5.5), data[,i]), count_if(gt(5.4),data[,i]))
            lbls <- c("Passed", "Failed")
            pct <- round(slices/sum(slices)*100)
            lbls <- paste(lbls, pct)
            lbls <- paste(lbls,"%",sep="")
            pie_charts[[i]] <- plot_ly(labels = lbls, values = slices, type = 'pie')
        }
        pie_charts
    }
    
    # Pass the dataframe to the function
    
    plots <- plotfunc(data)
    
    # The 'plots' array will have individual plots
    # This will print the first plot
    plots[[1]]
    
    # The second...and so on
    plots[[2]]
    

    Hope this is clear!