Power Bi和R。我的数据集发生了什么?

时间:2020-02-20 23:31:24

标签: r powerbi data-visualization

我希望能够在Power BI中获得这种可视化效果

https://www.neo-reliability.com/post/building-an-interactive-risk-matrix-using-r/

只要我使用教程中的r脚本导入数据,我就可以显示ggplot(risk_p)。但是,当我尝试将数据导入power bi中,然后像尝试其他视觉效果一样尝试利用这些列时,就会遇到这个奇怪的错误。

Insufficient values in manual scale. 4 needed but only 3 provided.

我只是踢了一下脚步,所以我添加了另一个颜色值以查看会发生什么。奇怪的是,它可以工作,但是我得到了这个输出。

risk matrix

对于每个类别,“汇总”都处于关闭状态,但是似乎它在绘制带有汇总箱的汇总“类型” ...每种类型都有一个绘图点(包括从未发生过的空白,因此在图例中除外) 。 Power BI会怎么做才能认为应该有第四个空白类别?我该怎么做才能正确绘制?

下面我为power bi编辑的代码的副本。

谢谢!

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset <- data.frame(ID, Title, Risk, Type, Interim, Consequence, Likelihood)
# dataset <- unique(dataset)

# Paste or type your script code here:

library("ggplot2", lib.loc = .libPaths("C:/Users/Bparsons/Documents/R/win-library/3.6"))
library("plotly", lib.loc = .libPaths("C:/Users/Bparsons/Documents/R/win-library/3.6"))
library("dplyr", lib.loc = .libPaths("C:/Users/Bparsons/Documents/R/win-library/3.6"))

# Creating heatmap background for Risk Matrix

# setting the score in order to calculate the risk level
Likelihood_score <- rep(c(1,2,4,6,12),5)
Consequence_score <- rep(c(1,2,4,6,12),each=5)
Likelihood <- rep(c(1:5),5)
Consequence <- rep(c(1:5),each=5)
df <- data.frame(Likelihood,Consequence)
df <- mutate(df, risk_score = Consequence_score * Likelihood_score,
             Risk = case_when(risk_score >= 0 & risk_score < 6 ~ 1,
                              risk_score >= 6 & risk_score < 12 ~ 2,
                              risk_score >= 12 & risk_score < 32  ~ 3,
                              risk_score >= 32 ~ 4) )


# plotting 
risk_p<- ggplot(df,aes(x =Likelihood, y =Consequence, fill=Risk))+
  geom_tile()+
  scale_fill_gradientn(colours = c("red", "orange","#EEEE00","#008000"),guide=FALSE)+
  scale_x_continuous(trans = "reverse",name= "Frequency",breaks = 0:5, expand = c(0, 0))+
  scale_y_continuous(trans = "reverse",name = "Consequence",breaks = 0:5, expand = c(0, 0))+
  #coord_fixed()+
  theme_bw()+
  geom_hline(yintercept = seq(1.5,5.5), color = "white")+
  geom_vline(xintercept = seq(1.5,5.5), color = "white")+
  ggtitle("5 X 5 Interactive Risk Matrix")+
  theme(legend.position="bottom")+
  guides(color=guide_legend(title="Selected Plants"))+
  geom_jitter(data = dataset,
              # position = "jitter",
              inherit.aes = FALSE, width= 0.3,height = 0.3,
              aes(y = Consequence,
                  x = Likelihood, 
                  col = Type,
                  text = paste("<b>ID#:</b>",ID,"<br>",
                               "<b>Risk:</b>",Risk,"<br>",
                               "<b>Type:</b>",Type,"<br>",
                               "<b>Interim Action:</b>",Interim)))+
  scale_color_manual(values = c("#9400D3", "#9400D3","#009fdf","#aaaaaa")
  )

risk_p

0 个答案:

没有答案