Search code examples
ralteryx

alteryx r tool multiple plot output (using a loop on unique)


In alteryx I am attempting to get multiple graph outputs in the R tool. My loop is running twice but I am not getting any outputs. How would I get this tool to output multiple graphs. This tool will give me one graph if I call the p outside of the loop but I will only get the last graph that was run in the tool.

    library(ggplot2)

    cd <- read.Alteryx("#1", mode="data.frame")


    AlteryxGraph(1, width=1008, height=298)

        #Batch graph output for each unique "group by" configuration.
        for (i in unique(cd$USN))
        {

                #Set graph data for each group:
                plot.data = subset(cd,cd$USN==i)

                #Plot settings:
                p <- ggplot(data=plot.data, aes(x=factor(Dates), y=Counts, fill=Group)) +
                    geom_bar(stat="identity", width = .8, position=position_dodge(), colour="black") + xlab("Kroger Weeks") + ylab("Units") +
                    scale_fill_manual(values=c("#88B4F7", "#FF9333")) + theme(legend.position="bottom", legend.title = element_blank()) + 
                        geom_text(aes(label=Counts), vjust=1.6, color="white", position = position_dodge(0.8), size=3.5) + ggtitle(plot.data$USN) +
                     theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=16, hjust=0.5)) 

                p + scale_x_discrete("Kroger Weeks", labels=c("11" = "Early", "12" = "P3W4", "13" = "P4W1", "14" = "P4W2", "15" = "P4W3", 
           "16" = "P4W4", "17" = "P5W1", "18" = "P5W2", "19" = "P5W3", "20" = "P5W4",
            "21" = "P6W1", "22" = "P6W2", "23" = "P6W3", "24" = "P6W4", "25" = "P7W1",
            "26" = "P7W2", "27" = "P7W3", "28" = "P7W4", "29" = "P8W1", "30" = "P8W2", "31" = "P8W3", "32" = "P8W4"))

        AlteryxMessage("How Many Times", msg.consts$INFO, priority.consts$LOW)
        p
        }

#p I ONLY GET AN OUTPUT IF I THIS HERE (and only my last graph)


invisible(dev.off())

Solution

  • I do have Alteryx but don't use it much. However, I assume the issue is related to displaying ggplot objects in a loop in R. Whenever you put p in the loop, it's very unlikely it shows up... I would suggest, try print(p) instead of p first. If it solves the problem. Then, done.

    If you would like to store the ggplot object, and look at them later, then try (I did not change anything inside, only replaced the loop with lapply):

        myList <- lapply(unique(cd$USN), function(x) {
            #Set graph data for each group:
            plot.data = subset(cd, cd$USN==x)
    
            #Plot settings:
            p <- ggplot(data=plot.data, aes(x=factor(Dates), y=Counts, fill=Group)) +
                geom_bar(stat="identity", width = .8, position=position_dodge(), colour="black") + xlab("Kroger Weeks") + ylab("Units") +
                scale_fill_manual(values=c("#88B4F7", "#FF9333")) + theme(legend.position="bottom", legend.title = element_blank()) + 
                geom_text(aes(label=Counts), vjust=1.6, color="white", position = position_dodge(0.8), size=3.5) + ggtitle(plot.data$USN) +
                theme(plot.title = element_text(family = "Trebuchet MS", color="#666666", face="bold", size=16, hjust=0.5)) 
    
            p + scale_x_discrete("Kroger Weeks", labels=c("11" = "Early", "12" = "P3W4", "13" = "P4W1", "14" = "P4W2", "15" = "P4W3", 
                                                          "16" = "P4W4", "17" = "P5W1", "18" = "P5W2", "19" = "P5W3", "20" = "P5W4",
                                                          "21" = "P6W1", "22" = "P6W2", "23" = "P6W3", "24" = "P6W4", "25" = "P7W1",
                                                          "26" = "P7W2", "27" = "P7W3", "28" = "P7W4", "29" = "P8W1", "30" = "P8W2", "31" = "P8W3", "32" = "P8W4"))
    
            return(p)
        }
    

    You can access your plots by myList[[1]], myList[[2]], etc. Hopefully, it helps.