How to display association rules using wordcloud in R? - r

I was trying to display the frequent association rules with tag cloud in R.
I have the association rules sorted in desc order and exported the results to a csv file with each rule in the first column. However, after I imported the csv file and tried to form a wordcloud, I got an error message: " Error in input$supoort : $ operator not defined for this S4 class"
I don't know what this means and how to get the wordcloud of frequent association rules, if possible.
Below is my code:
myData = read.transactions("data.csv", format = "basket",sep= ",", cols = 1, skip = 1)
rules <- apriori(myData, parameter = list(supp = 0.010, conf = 0.5, minlen = 2))
rules_supp <- sort(rules, by = "support", descreasing = TRUE)
inspect(rules_supp)
write(rules_supp,file = "rules_supp.csv", sep = ",", row.names = FALSE)
word <- read.csv(file = "rules_supp.csv")
wordcloud(words = word$rules, freq = word$support)

Try something like wordcloud(labels(rules)). However, wordcloud() will break the rule labels into words, and I am not sure that this is what you want.

There is wordcloud package you can use to make a word cloud graphics but there is a better way to display association rules in R.
You can use the R package arulesViz that you find here : https://cran.r-project.org/web/packages/arulesViz/vignettes/arulesViz.pdf
I have been using it for my paper I've published here https://link.springer.com/article/10.1007/s12652-017-0665-3
It helps you visualize the rules as correlation matrix or as a graph with nodes and oriented edges. Here are some of the graphics you can make.

Related

Suppress graph output of a function [duplicate]

I am trying to turn off the display of plot in R.
I read Disable GUI, graphics devices in R but the only solution given is to write the plot to a file.
What if I don't want to pollute the workspace and what if I don't have write permission ?
I tried options(device=NULL) but it didn't work.
The context is the package NbClust : I want what NbClust() returns but I do not want to display the plot it does.
Thanks in advance !
edit : Here is a reproducible example using data from the rattle package :)
data(wine, package="rattle")
df <- scale (wine[-1])
library(NbClust)
# This produces a graph output which I don't want
nc <- NbClust(df, min.nc=2, max.nc=15, method="kmeans")
# This is the plot I want ;)
barplot(table(nc$Best.n[1,]),
xlab="Numer of Clusters", ylab="Number of Criteria",
main="Number of Clusters Chosen by 26 Criteria")
You can wrap the call in
pdf(file = NULL)
and
dev.off()
This sends all the output to a null file which effectively hides it.
Luckily it seems that NbClust is one giant messy function with some other functions in it and lots of icky looking code. The plotting is done in one of two places.
Create a copy of NbClust:
> MyNbClust = NbClust
and then edit this function. Change the header to:
MyNbClust <-
function (data, diss = "NULL", distance = "euclidean", min.nc = 2,
max.nc = 15, method = "ward", index = "all", alphaBeale = 0.1, plotetc=FALSE)
{
and then wrap the plotting code in if blocks. Around line 1588:
if(plotetc){
par(mfrow = c(1, 2))
[etc]
cat(paste(...
}
and similarly around line 1610. Save. Now use:
nc = MyNbClust(...etc....)
and you see no plots unless you add plotetc=TRUE.
Then ask the devs to include your patch.

arules grouped matrix plot and labels in r

I'm currently using the arulesViz package in order to create some clusters within my rules.
You can have an exemple of the clustering method provided by the arulesViz packages.
The only thing which is bothering are the clusters labels which are not accessible throught the attr(group_plot$mAggr, "dimnames").
looking at the arulesViz code, I found that the labels are displayed through a grid.text() function.
Is there any way to have access to those labels since I cannot find any gPath argument to provide to the grid.get() function.
library(arules)
library(arulesViz)
data("Groceries")
rules <- apriori(Groceries,
parameter = list(supp = 0.01, conf = 0.1))
group_plot <- plot(rules,
method = "grouped",
measure = "support",
shading = "lift",
control = list(k=5,
lhs_items =3))

Why can't the pdf file created by gage (a R packge) be opened

I am trying to use Gage package implemented in R to analyze my RNA-seq data. I followed the tutorial and got my data.kegg.p file and I used the following script to generate the heatmap for the top gene set
for (gs in rownames(data.kegg.p$greater)[1]) {
outname = gsub(" |:|/", "_", substr(gs, 10, 100))
geneData(genes = kegg.gs[[gs]], exprs = essData, ref = 1,
samp = 2, outname = outname, txt = T, heatmap = T,
Colv = F, Rowv = F, dendrogram = "none", limit = 3, scatterplot = T)
}
I did get a pdf file named "NOD-like_receptor_signaling_pathway.geneData.heatmap.pdf", but when I open this file with acrobat reader or photoshop, it gives the error information that this file has been disrupted and cannot be recovered. Could anyone help check this file (https://www.dropbox.com/s/wrsml6n1pbrztnm/NOD-like_receptor_signaling_pathway.geneData.heatmap.pdf?dl=0) to see whether it is really disrupted and is it possible to find a way to recover it?
I also attached the R workspace file (https://www.dropbox.com/s/6n5m9x5hyk38ff1/A549.RData?dl=0). The object "a4" is the data with the format ready for gage analysis. It contains the data of the reference sample (nc) the treated sample (a549). It can be accepted by gage for analysis but generate the heatmap pdf file which cannot be opened (above). Would you mind helping me check whether these data can be properly used to generated the correct gage result?
Best regards.
I'm running into a similar problem myself. Not 100% sure but I think this problem occurs when there is no heatmap to plot. In my case, I was doing as.group comparison with ref and sample selections. I think the software treats this circumstance as a sample n of 1 and can't really show a differential heatmap. When I tried using 1ongroup setting, I was able to visualize the pdf file.

'not a graph obect' error when performing degree() from igraph package

Since I need normalized scores, I wanted to call the degree() function on my adjacency matrix I got from a text file I loaded into R using read.delim. That went perfectly fine with the sna package.
When I run
K3_T2_ACAD <- diag.remove(read.delim("K3_T2_ACAD.txt", header = TRUE,
sep = "\t", row.names = 1), remove.val=0)
and then
K3_T2_ACAD_indeg <- degree(K3_T2_ACAD, g=1, nodes=NULL, gmode="digraph",
diag=FALSE, tmaxdev=FALSE, cmode="indegree")
it works!
I tried detaching the sna functions because I thought that was the problem. However, when I run the igraph degree() function, it does not work:
K3_T2_ACAD_indeg2 <- degree(K3_T2_ACAD, mode ="in", loops = FALSE, normalized = TRUE)
returns
Error in degree(K3_T2_ACAD, mode = "in", loops = FALSE, normalized =
TRUE) : Not a graph object
The first column and row each contain the participant codes. Is it possible, that igraph cannot work with that, whereas sna can?
The sna package uses adjacency matrices, igraph does not not. You need to create an igraph object to work on. See e.g. http://igraph.org/r/doc/aaa-igraph-package.html

Extracting values from a graph

I have a graph that is created by complex numbers from the function below. I would like to extract the resulting data points which correpond with the line from the data plot as to be able to work with a vector of data.
library(multitaper)
NW<-10
K<-5
x<-c(2,3,1,3,4,6,7,8,5,4,3,2,4,5,7,8,6,4,3,2,4,5,7,8,6,4,5,3,2,5,7,8,6,4,5,3,6,7,8,8,9,7,6,5,4,7)
resSpec <- spec.mtm(as.ts(x), k= K, nw=NW, nFFT = length(x),
centreWithSlepians = TRUE, Ftest = TRUE,
jackknife = FALSE, maxAdaptiveIterations = 100,
plot =FALSE, na.action = na.fail)
plot(resSpec)
What would be the best procedure. I have tried saving the plot in emf. I wanted to use package ReadImages which was I believe the right package. (however this was not available for R versiĆ³n 3.02 so I could not use it). What would be the correct procedure of saving and extracting and are there other packages and in what file types could I save the graph (as far as I can see R (OS windows) only permist emf.)
Any help welcomed

Resources