首页 > 解决方案 > 如何在执行脚本和绘制GGPLOT2时将SAP应用程序中用户选择的值传递给R脚本

问题描述

我有绘制 ggplot 的 R 脚本。我已将脚本嵌入到 SAP 工具之一中。但是用户想要选择其中一列,并基于该选定的值绘图应该发生。

如果我选择 SAP 中的所有值,它可以正常工作,但是当我选择特定的用户定义值时,我的 x 轴和 Y 轴会发生变化。我在 X 轴和 Y 轴上都使用离散序列。

我附上我的代码以供参考。`#加载库

library(ggplot2)
library(plotly)
library(dplyr)
# Load the data
risk <-DATAFRAME
summary(risk)
# setting the score in order to calculate the risk level
Likelihood_score <- rep(c(1,2,3,4,5),5)
Impact_score <- rep(c(1,2,3,4,5),each=5)
Likelihood <- rep(c(1:5),5)
Impact <- rep(c(1:5),each=5)
df <- data.frame(Likelihood,Impact)
df <- mutate(df, risk_score = Impact_score * Likelihood_score,
             Risk = case_when(risk_score >= 0 & risk_score < 4 ~ 1,
                              risk_score >= 4 & risk_score < 9 ~ 2,
                              risk_score >= 9 ~ 3))
########################################################################################
#display_risk <- filter(risk(Risk_Name,AnalysisType)
## plotting
risk_p<- ggplot(df,aes(x =Likelihood, y =Impact, fill=Risk))+
  geom_tile()+
  scale_fill_gradientn(colours = c("#008000","orange","red"),guide=FALSE)+
  scale_x_discrete(name= "Likelihood",labels = c("1-Highly unlikely (0% - < 20%)" = "1:Highlyunlike","2-Unlikely (20% - < 40%)" = "2:Unlike",
  "3-Possible (40% - < 60%)" = "3:Possible","4-Probable (60% - < 80%)" = "4:Probable","5-Highly probable (> 80%)" = "5:HighProabale"))+
  scale_y_discrete(name = "Potential impact")+
  coord_fixed()+
  #coord_cartesian()+
  theme_bw()+
  geom_hline(yintercept = seq(1.5,5.5), color = "white")+
  geom_vline(xintercept = seq(1.5,5.5), color = "white")+
  ggtitle("RISK ASSESMENT MATRIX")+
  theme(legend.position="bottom")+
  guides(color=guide_legend(title="Selected Risk"))+
  geom_jitter(data =risk,size=5,
             # mapping=NULL,
              stat="identity",
              #na.rm=FALSE,
             # show.legend=NA,
              #position = "jitter",
              inherit.aes = FALSE, width= 0.5,height = 0.5,
              aes(y =PotentialImpact,
                  x = Likelihood, 
                  col = AnalysisType,
                  text = paste("<b>RISK_ID#:</b>",RISK_ID,"<br>",
                               "<b>PotentialImpact_Txt:</b>",PotentialImpact_Txt,"<br>",
                               "<b>AnalysisType:</b>",AnalysisType,"<br>",
                  `            "<b>OrganizationalUnit_Descr:</b>",OrganizationalUnit_Descr)))+
  scale_color_manual(values = c("#9400D3","#009fdf","#aaaaaa")
  )

config(ggplotly(risk_p,tooltip = "text", width = 950,height = 750), displayModeBar=FALSE, collaborate = FALSE)%>%
  layout(margin=list(l=190,b=50),
         legend = list(orientation = "h",y =-0.15, x =0))`
 

标签: rggplot2

解决方案


推荐阅读