alteryx r 工具多图输出(使用唯一循环)
alteryx r tool multiple plot output (using a loop on unique)
在 alteryx 中,我试图在 R 工具中获得多个图形输出。我的循环是 运行ning 两次,但我没有得到任何输出。我如何让这个工具输出多个图形。如果我在循环外调用 p,此工具会给我一个图表,但我只会得到工具中 运行 的最后一张图表。
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())
我有 Alteryx,但用得不多。但是,我认为这个问题与在 R 中的循环中显示 ggplot 对象有关。无论何时将 p
放入循环中,它都不太可能出现......
我建议,首先尝试 print(p)
而不是 p
。如果它解决了问题。然后,大功告成。
如果你想存储ggplot对象,稍后再看,那就试试(我没有改变里面的任何东西,只是用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)
}
您可以通过 myList[[1]]
、myList[[2]]
等方式访问您的地块。
希望对您有所帮助。
在 alteryx 中,我试图在 R 工具中获得多个图形输出。我的循环是 运行ning 两次,但我没有得到任何输出。我如何让这个工具输出多个图形。如果我在循环外调用 p,此工具会给我一个图表,但我只会得到工具中 运行 的最后一张图表。
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())
我有 Alteryx,但用得不多。但是,我认为这个问题与在 R 中的循环中显示 ggplot 对象有关。无论何时将 p
放入循环中,它都不太可能出现......
我建议,首先尝试 print(p)
而不是 p
。如果它解决了问题。然后,大功告成。
如果你想存储ggplot对象,稍后再看,那就试试(我没有改变里面的任何东西,只是用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)
}
您可以通过 myList[[1]]
、myList[[2]]
等方式访问您的地块。
希望对您有所帮助。