Power Bi:在R Visual

时间:2019-02-19 13:28:52

标签: r filtering powerbi powerbi-desktop

我正在做一个PowerBi仪表盘,比较目标群体和基本人群中某些特征的发生。这与PowerBi中的向下钻取完美配合,如下所示:

enter image description here

现在,我想在Power Bi仪表板中包含的R Visual中实现类似的效果。在Shiny中,我通过将经过过滤和未经过滤的数据集彼此相邻并将其组合成一个图表来实现这一点。

R的样本数据集

TargetGroupFilter <- sample( c("Yes", "No"), 50, replace=TRUE, prob=c(0.5, 0.5) )
ActivityA <- sample( c("Yes", "No"), 50, replace=TRUE, prob=c(0.5, 0.5) )
ActivityB <- sample( c("Yes", "No"), 50, replace=TRUE, prob=c(0.7, 0.3) )
ActivityC <- sample( c("Yes", "No"), 50, replace=TRUE, prob=c(0.2, 0.8) )
WEIGHT <- sample (30:50, 50, replace = TRUE)
df <- data.frame(TargetGroupFilter, ActivityA, ActivityB, ActivityC, WEIGHT)

R代码

library(tidyverse)
library(scales)

dfBase <- df
dfTarget <- df[df$TargetGroupFilter == "Yes",]

dfBase1 <- gather(dfBase, key = "Activity", value = "Answer", ActivityA, ActivityB, ActivityC)
dfBase1 <- group_by(dfBase1, Activity, Answer)
dfBase1 <- summarise(dfBase1, Population = sum(WEIGHT))
dfBase1 <- ungroup(dfBase1)
dfBase1 <- mutate(dfBase1, Share = (Population / sum(dfBase$WEIGHT)))
dfBase1$Target <- rep("Base",NROW(dfBase1))

dfTarget1 <- gather(dfTarget, key = "Activity", value = "Answer", ActivityA, ActivityB, ActivityC)
dfTarget1 <- group_by(dfTarget1, Activity, Answer)
dfTarget1 <- summarise(dfTarget1, Population = sum(WEIGHT))
dfTarget1 <- ungroup(dfTarget1)
dfTarget1 <- mutate(dfTarget1, Share =  (Population / sum(dfTarget$WEIGHT)))
dfTarget1$Target <- rep("Target",NROW(dfTarget1))

dfCompare <- rbind(dfBase1,dfTarget1)
dfCompare <- dfCompare [dfCompare$Answer == "Yes",]
dfCompare

ggplot(dfCompare)+
  aes(x=Activity, y=Share, fill=Target)+
  geom_bar(position = "dodge", stat= "identity")+
  scale_y_continuous(labels=percent, limits = c(0,1))

结果图表

enter image description here

问题

已过滤的数据集应该来自于功率Bi中的向下钻取选择或报告级别过滤器选择。当我仅显示过滤后的数据时,这可以完美工作。当我想在R Visual中显示过滤的以及未过滤的数据时,会发生问题。 我知道,在将未过滤的数据添加到相应的视觉对象之后,我可以使用编码过滤器。但是我希望过滤后的数据显示对仪表板用户在Power Bi UX中所做的过滤器或向下钻取选择有反应。

问题

是否可以将已过滤以及未过滤的数据馈送到R Visual? 仅使用Power Bi可以实现这种视觉效果吗?

0 个答案:

没有答案