Venn Diagram

Frequent Visitor
12936 Views
juniper Frequent Visitor
Frequent Visitor

Re: Venn Diagram

Thanks so much!

 

##This Venn diagram code is for use in the Power Bi demonstration. Run it from R from within Power Bi

#set working directory i.e. the folder you are working out of
setwd("H:/Eliel JBBS Sent")
#set default library path - where to look for packages, which are also called libraries
.libPaths("H:/R/win-library/3.4")

#load Venn Diagram package

library(VennDiagram)
library("RColorBrewer")
library(grid)
library(futile.logger)
#get Java so we can export to Excel
Sys.setenv(JAVA_HOME = "C:/Program Files/Java/jdk1.8.0_151")

library(xlsx)
library(rJava)


Venn <- read.xlsx("Venn.xlsx", sheetName = "Venn", header = TRUE, stringsAsFactors = FALSE)


draw.triple.venn(margin = 0.1, area1 = nrow(subset(Venn, Felony == "Yes")), area2 = nrow(subset(Venn, Misdemeanor == "Yes")), area3 = nrow(subset(Venn, Ordinance == "Yes")),
n12 = nrow(subset(Venn, Felony == "Yes" & Misdemeanor == "Yes")), n23 = nrow(subset(Venn, Misdemeanor == "Yes" & Ordinance == "Yes")),
n13 = nrow(subset(Venn, Felony == "Yes" & Ordinance == "Yes")), n123 = nrow(subset(Venn, Felony == "Yes" & Misdemeanor == "Yes" & Ordinance == "Yes")),
category = c("Felony Offenders", "Misdemeanor Offenders", "Ordinance Offenders"), lty = "blank", fill = brewer.pal(n = 3, name = "Dark2"))

Moderator boefraty
Moderator

Re: Venn Diagram

I got your code to run in my PBI Desktop just fine.  You mentioned something about slicers. I can not reproduce your slicers from R script Smiley Sad 

 

Can you attach your PBIX (PBI desktop report file) ?  

juniper Frequent Visitor
Frequent Visitor

Re: Venn Diagram

Thanks so much.

 

Yes, my Venn code works great. The slicer does not.

 

I created a slicer for Race like I do for anything else in Power Bi (see attached screen shots) It's on the same page as my Venn Diagram, which I created in R. I expected that if I selected only "white" in the race slicer, it would filter out all the non-white subjects in my Venn diagram, just like it does for any other Power Bi visual.

 

Isn't that how it's supposed to work?

 

Thanks again,

 

Jennifer

Moderator boefraty
Moderator

Re: Venn Diagram

Try to change your R visual to "Table" visual. See how it responses to slicer. 

OR

Run the code with your filtered data in R IDE like RStudio (see my previous reply) and debug it 

OR   

Send me the PBIX Smiley Happy 
 Capture.PNG

 

juniper Frequent Visitor
Frequent Visitor

Re: Venn Diagram

Thank you so much! Here is my PBIX file.

 

Also I'vew attached a screen shot of what happens when I change it to a table. Yes the slicer works, but the visual isn't useful anymore.

 

Many thanks,

Jennifer

Moderator boefraty
Moderator

Re: Venn Diagram

You are reading input from exel file, ignoring the input from PBI report Smiley Happy 

 

#Comment out this line 

#Venn <- read.xlsx("Venn.xlsx", sheetName = "Venn", header = TRUE, stringsAsFactors = FALSE)

 

#Use this one instead 

Venn = dataset

 

I'd also recommend to clean the R code from loading unnecessary  libraries (rJava etc.) 

 

Good luck with your project Smiley Happy 

Moderator boefraty
Moderator

Re: Venn Diagram

Hi @juniper,
One more tip. You should add more columns to your R visual ,  so that each row  is unique. Otherwise  PBI visual  (any visual) will remove duplicates and your diagramm will be wrong.  Another option is to use ID column.  And make sure you do not aggregate fields. 
"Do not summarize" for all input fields. 

juniper Frequent Visitor
Frequent Visitor

Re: Venn Diagram

HI Again Boefraty,

 

I did everything you asked, but now I'm getting a count of one per category. There is a unique id for each observation, but Power Bi is still deleting "duplicates".

 

Here's the code and output, as well as a screenshot showing the unique ids.

 

Many, many, many thanks,

 

Juniper

 

#set working directory i.e. the folder you are working out of
setwd("H:/Eliel JBBS Sent")
#set default library path - where to look for packages, which are also called libraries
.libPaths("H:/R/win-library/3.4")

#load Venn Diagram package

library(VennDiagram)
library("RColorBrewer")
library(grid)
library(futile.logger)


#Venn <- read.xlsx("Venn.xlsx", sheetName = "Venn", header = TRUE, stringsAsFactors = FALSE)
Venn = dataset

draw.triple.venn(margin = 0.1, area1 = nrow(subset(Venn, Felony == "Yes")), area2 = nrow(subset(Venn, Misdemeanor == "Yes")), area3 = nrow(subset(Venn, Ordinance == "Yes")),
n12 = nrow(subset(Venn, Felony == "Yes" & Misdemeanor == "Yes")), n23 = nrow(subset(Venn, Misdemeanor == "Yes" & Ordinance == "Yes")),
n13 = nrow(subset(Venn, Felony == "Yes" & Ordinance == "Yes")), n123 = nrow(subset(Venn, Felony == "Yes" & Misdemeanor == "Yes" & Ordinance == "Yes")),
category = c("Felony Offenders", "Misdemeanor Offenders", "Ordinance Offenders"), lty = "blank", fill = brewer.pal(n = 3, name = "Dark2"))

Moderator boefraty
Moderator

Re: Venn Diagram

Hi @juniper

I  really need the PBIX to see  the problem  Smiley Sad

Sounds like, you added the unique ID to data table, but you did not add the ID column as input to R visual. Drag the ID column to R visual as you did with "Felony" and the rest.  

juniper Frequent Visitor
Frequent Visitor

Re: Venn Diagram

Hi Boefraty,

 

Thank you so much for your patience. Here is the PBIX file.

 

I dragged the unique ID column to the R visual, but got the same result.

 

Juniper