arules grouped matrix plot and labels in r - 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))

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.

Is there a way to remove points from a Mclust classification plot in R?

I am trying to plot the GMM of my dataset using the Mclust package in R. While the plotting is a success, I do not want points to show in the final plot, just the ellipses. For a reference, here is the plot I have obtained:
GMM Plot
But, I want the resulting plot to have only the ellipses, something like this:
GMM desired plot
I have been looking at the Mclust plot page in: https://rdrr.io/cran/mclust/man/plot.Mclust.html and looking at the arguments of the function, I see there is a scope of adding other graphical parameters. Looking at the documentation of the plot function, there is a parameter called type = 'n' which might help to do what I want but when I write it, it produces the following error:
Error in plot.default(data[, 1], data[, 2], type = "n", xlab = xlab, ylab = ylab, :
formal argument "type" matched by multiple actual arguments
For reference, this is the code I used for the first plot:
library(mclust)
Data1_2 <- Mclust(Data, G=15)
summary(Data1_2, parameters = TRUE, classification = TRUE)
plot(Data1_2, what="classification")
The code I tried using for getting the graph below is:
Data1_4 <- Mclust(Data, G=8)
summary(Data1_4, parameters = TRUE, classification = TRUE)
plot(Data1_4, what="classification", type = "n")
Any help on this matter will be appreciated. Thanks!
If you look under the source code of plot.Mclust, it calls plot.Mclust.classification which in turn calls coordProj for the dot and ellipse plot. Inside this function, the size is controlled by the option CEX= and shape PCH=.
So for your purpose, do:
library(mclust)
clu = Mclust(iris[,1:4], G = 3, what="classification")
plot(clu,what="classification",CEX=0)

How do I change the labels of a dendogram using the fviz_dend function from the "factoextra" package in R?

I am creating a shiny app that showcases different clustering techniques such as hierarchical and k-means clustering using a cereal dataset as an example. I am using the fviz_dend function from the "factoextra" package to create my dendogram. However, when I do this the dendogram does not show the names of the cereals as the labels, rather it shows the numerical representation instead. Is there a way to change the numerical values to labels? I am attaching below a picture of my current dendogram using the fviz_dend function and a picture of a dendogram I made using the plot function in base R. Note that the dendogram in created by the plot function has the labels of the cereals as I need them (what I am trying to achieve).
Dendogram Created Using fviz_dend:
### Code for dendogram using fvizdend
hc <- hclust(dist(scale(xv), method = input$dmeth), method = input$meth)
fviz_dend(hc, k = input$clustgroup, cex = 0.5, k_colors = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
color_labels_by_k = T, rect = T, show_labels = T)
Dendogram Created Using plot function:
hc <- hclust(dist(scale(xv), method = input$dmeth), method = input$meth)
plot(hc, labels = xv$Brand)
Have you tried setting the row.names of xv with the labels?
rownames(xv) <- xv$Brand
Change the labels of the hclust object(hc in your case) and then plot the dendrogram.
hc$labels <- xv$Brand
library("factoextra")
fviz_dend(hc, cex = 0.5)

How to display association rules using wordcloud in 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.

How to plot an nmds with coloured/symbol points based on SIMPROF

Hi so i am trying to plot my nmds of a assemblage data which is in a bray-curtis dissimilarity matrix in R. I have been able to apply ordielipse(),ordihull() and even change the colours based on group factors created by cutree() of a hclst()
e.g using the dune data from the vegan package
data(dune)
Dune.dis <- vegdist(Dune, method = "bray)
Dune.mds <- metaMDS(Dune, distance = "bray", k=2)
#hierarchical cluster
clua <- hclust(Dune.dis, "average")
plot(clua, hang = -1)
# set groupings
rect.hclust(clua, 4)
grp <- cutree(clua, 4)
#plot mds
plot(Dune.mds, display = "sites", type = "text", cex = 1.5)
#show groupings
ordielipse(Dune.mds, group = grp, border =1, col ="red", lwd = 3)
or even colour the points just by the cutree
colvec <- c("red2", "cyan", "deeppink3", "green3")
colvec[grp]
plot(Dune.mds, display = "sites", type = "text", cex = 1.5) #or use type = "points"
points(P4.mds, col = colvec[c2], bg =colvec[c2], pch=21)
However what i really want to do is use the SIMPROF function using the package "clustsig" to then colour the points based on significant groupings - this is more of a technical coding language thing - i am sure there is a way to create a string of factors but i am sure there is a more efficient way to do it
heres my code so far for that:
simp <- simprof(Dune.dis, num.expected = 1000, num.simulated = 999, method.cluster = "average", method.distance = "braycurtis", alpha = 0.05, sample.orientation = "row")
#plot dendrogram
simprof.plot(simp, plot = TRUE)
Now i am just not sure how do the next step to plot the nmds using the groupings defined by the SIMPROF - how do i make the SIMPROF results a factor string without literally typing it my self it myself?
Thanks in advance.
You wrote you know how to get colours from an hclust object with cutree. Then read the documentation of clustsig::simprof. This says that simprof returns an hclust object within its result object. It also returns numgroups which is the suggested number of clusters. Now you have all information you need to use the cutree of hclust you already know. If your simprof result is called simp, use cutree(simp$hclust, simp$numgroups) to extract the integer vector corresponding to the clustsig::simprof result, and use this to colours.
I have never used simprof or clustsig, but I gathered all this information from its documentation.

Resources