Side by side wordclouds in R markdown - r

Trying to plot two WordClouds side by side for a Beamer presentation in R Markdown and I'm struggling to work out how to do it. I'm aware you can use grid.arrange() if you set the plots as objects but WordClouds cannot be set as object as far as I'm aware.
Many Thanks in Advance
Samuel

You may try par(mfrow=c(1,2)) from BASE R and do something like below,
I have tested it on RStudio beamer as well, it works:
library(wordcloud)
par(mfrow=c(1,2))
obj1 <- data.frame(words=c("a","b","c"),
freq=c(10,6,2))
obj2 <- data.frame(words=c("a","b","c"),
freq=c(15,11,5))
wordcloud(obj1$words, obj1$freq, min.freq =3,
scale=c(5, .2), random.order = FALSE, random.color = FALSE,
colors= c("red"))
wordcloud(obj2$words, obj2$freq, min.freq =3,
scale=c(5, .2), random.order = FALSE, random.color = FALSE,
colors= c("green"))

Take a screenshot and use your favourite picture editor

Use par(mfrow=c(x,y))
x denotes the splitting into rows and y denotes splitting into columns.
So if you want to display it side by side use par(mfrow=c(1,2))
And if you want to display it up and down use par(mfrow=c(2,1))

Related

Control plot layout in foodweb plot from mvbutils R-package

I would like to visualize how functions in my own R package depend on each other. For this purpose I use the foodweb() function from the mvbutils package.
I can get the right functional dependencies out without a problem but the plot looks a bit messy, with lines crossing each other and function names not aligned vertically or horizontally.
Is there a way to control the layout of the plot similar to the way this works in the igraph package?
Example
dirPath <- "~/dev/stackoverflow/46910042"
setwd(dirPath)
## Download example Package
urlPackage <- "https://github.com/kbroman/qtlcharts/archive/master.zip"
download.file(urlPackage, destfile = "master.zip")
unzip("./master.zip", exdir = dirPath, overwrite = TRUE)
## Install or load mcbutils
if (!require(mvbutils)) install.packages("mvbutils")
thefiles = list.files(path = "./qtlcharts-master/R/", full.names = TRUE)
thefiles
## Now we load all the package files into memory, so we can have
## foodweb generate a map of the package functions.
sapply(thefiles, source)
## Generate plot
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
Plot Output:
Michael,
One option is to look behind the curtains of foodweb. The mvbutils::foodweb function returns an object of (S3) class foodweb. This has three components:
funmat a matrix of 0s and 1s showing what (row) calls what (column). The dimnames are the function names.
x shows the x-axis location of the centre of each function’s name in the display, in par("usr") units
level shows the y-axis location of the centre of each function’s name in the display, in par("usr") units.
thus one approach we can take is to call foodweb but tell it not to create a plot rather return a foodweb object. This then allows us to manipulate the data directory or via graphics::plot() externally of the defaults provided by the mvbutils::foodweb() function.
Why? Well, to do what you suggest my sense is three options exist:
You can either play with mvbutils::foodweb() parameters.
Work with data structure returned with another plotting package.
Use graphics::par() and graphics::plot to manipulate the plot size and attributes of the foodweb structure returned.
It would be great to know your preference. Excluding, that my sense was to provide a base example:
Plot Package Example
In the case of using graphics::plot, you need to go look at how you manipulate graphics:par. par() allows you to set or query graphical parameters. For example, if we want to clean up the function plot you might choose to modify the grahics::par() fin parameter to increase the figure region dimensions, (width, height), in inches. A simple example but my sense it helps map out and demonstrate the options available to you.
## Generate plot
if (!require(qtlcharts)) install.packages("qtlcharts")
## Here we specify `asNamespace` to get the package internals
fw <- foodweb( where = asNamespace( "qtlcharts"),
plotting = FALSE,
)
#Display foodweb structure
str(fw)
# Expand plot figure region dimensions...
par(fin = c(9.9,7))
# Plot fw strucuture
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
Plot Output example
Note that the function names are not spaced out. Note I cut the top and bottom white of plot here. In this case, you can play with the par constraints such as margin to get the plot you want.
Pruning your plot
Another option within the constraints of mvbutils::foodweb is to use the prune and rprune option to simplify your plots. These are super poweful and useful especially the regular expression version.
if (!require(qtlcharts)) install.packages("qtlcharts")
fw <- foodweb( where = asNamespace( "qtlcharts"),
plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
fw <- foodweb( where = asNamespace( "qtlcharts"),
rprune = "convert_", ## search on `convert_` to negate use `~convert_`
plotting = FALSE)
str(fw)
par(fin = c(9.9,7))
plot(fw,
border = TRUE,
expand.xbox = 1,
boxcolor = "pink", lwd = 1.5, cex = 0.8)
Hoping the above information points you in the right direction.
T.
Because of the fact that there are many data, connections etc, the plot is squeezed in order to fit in the screen, hence it becomes messy.
What I would suggest is to save it in a PDF or PNG with big enough width and Height and then you can zoom in. This will save you a lot of time.
E.G.
## Generate plot
pdf( "mygraph.pdf", width = 50, height = 80 )
par(mar = rep(0.1, 4))
foodweb(border = TRUE, boxcolor = "pink", lwd = 1.5, cex = 0.8)
dev.off()
In addition, you can play with the plot options of foodweb.
Hope it helps.

how to make a biplot without label in R

I used
biplot(prcomp(data, scale.=T), xlabs=rep("·", nrow(data)))
but it did not work to omit the labels.
Even if I remove the labels my plot is so messy and ugly which can be seen below!
I also need to show the percentage of PCs on axes
I used the following command to plot the image
biplot(prcomp(data, scale.=T), xlabs=rep("·", nrow(data)), ylabs = rep("·", ncol(data)))
Try this one
\devtools::install_github("sinhrks/ggfortify")
library(ggfortify)
ggplot2::autoplot(stats::prcomp(USArrests, scale=TRUE), label = FALSE, loadings.label = TRUE)

R: plotting decision tree labels leaves text cut off

(I'm still learning how to handle images in R; this is sort of a continuation of rpart package: Save Decision Tree to PNG )
I'm trying to save a decision tree plot from rpart in PNG form, instead of the provided postscript. My code looks like this:
png("tree.png", width=1000, height=800, antialias="cleartype")
plot(fit, uniform=TRUE,
main="Classification Tree")
text(fit, use.n=TRUE, all=TRUE, cex=.8)
dev.off()
but cuts off a little of the labels for the edge nodes on both sides. this isn't a problem in the original post image, which I've converted to png just to check. I've tried using both oma and mar settings in par, which were recommended as solutions for label/text problems, and both added white space around the image but don't show anymore of the labels. Is there any way to get the text to fit?
The rpart.plot package plots rpart trees and automatically takes care of
the margin and related issues. Use rpart.plot (instead of plot and text in the rpart package). For example:
library(rpart.plot)
data(ptitanic)
fit <- rpart(survived~., data=ptitanic)
png("tree.png", width=1000, height=800, antialias="cleartype")
rpart.plot(fit, main="Classification Tree")
dev.off()
The default margin is 0. So if your text is a set of words or just a long word, try to put more margin in plot call. For example,
plot(fit, uniform=TRUE,margin=0.2)
text(fit, use.n=TRUE, all=TRUE, cex=.8)
Alternatively, you can adjust text font size by changing cex in text call. For example,
plot(fit, uniform=TRUE)
text(fit,use.n=TRUE, all=TRUE, cex=.7)
Of course, you can adjust both mar in plot call and cex in text call to get what you want.
On rpart man, at rpart() examples the author gives the solution, set par options with xpd = NA:
par(mfrow = c(1,2), xpd = NA)
otherwise on some devices the text is clipped
Problem tiwh titanic dataset is rplot will not join ages and fare to display a nive "age > 10" label. It will display them by extension, like:
age = 11,18,19,22,24,28,29,30,32,33,37,39,40,42,45.5,5,56,58,60...
That makes no room for labels (see the picture)
bad labels
Solution is here:
https://community.rstudio.com/t/rpart-result-is-too-small-to-see/60702/4
Basically, you have to mutate age and fare columns into numeric variables. Like:
clean_titanic <- titanic %>%
select(-c(home.dest, cabin, name, x, ticket)) %>%
mutate(
pclass = factor(pclass, levels = c(1, 2, 3), labels = c('Upper', 'Middle', 'Lower')),
survived = factor(survived, levels = c(0, 1), labels = c('No', 'Yes')),
# HERE. Also notice I'm removing dots from numbers
age = as.numeric(age),
fare = as.numeric(fare)
)
That will give you better labels, and room for them in the plot.
One more thing: you could get a warning when you force non numeric values with as.numeric, and there are a couple of ways to solve that, like replacing characters or ignoring the warning. Ignore like:
suppressWarnings(as.numeric(age)))
good plot

Principal Component Analysis in R data color

Hi everyone I have a simple question but for which i havent been able to get an answer in any tutorial. Ive done a simple principal component analysis on a set of data and then plot my data with biplot.
CP <- prcomp(dat, scale. = T)
summary(CP)
biplot(CP)
With this i get a scatter plot of my data in terms of the first and second component. I wish to separate my data by color, indicating R to paint my first 20 data in red and next 20 data in blue. I dont know how to tell R to color those two sets of data.
Any help will be very appreciated. thks!
(im very new to R)
Disclaimer: This is not a direct answer but can be tweak to obtain the desired output.
library(ggbiplot)
data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE))
Using plot() will provide you more flexibility - you may use it alone or with text() for text labels as belows (Thanks #flodel for useful comments):
col = rep(c("red","blue"),each=20)
plot(CP$x[,1], CP$x[,2], pch="", main = "Your Plot Title", xlab = "PC 1", ylab = "PC 2")
text(CP$x[,1], CP$x[,2], labels=rownames(CP$x), col = col)
However if you want to use biplot() try this code:
biplot(CP$x[1:20,], CP$x[21:40,], col=c("red","blue"))

How to put two 'vcd' grid graphics in a single plot?

I would like to place two (somewhat non-standard) grid graphics in a single plot in R.
Try:
require(vcd)
mosaic(Titanic)
assoc(Titanic)
The trouble is that these aren't lattice graphics, and to my knowledge do not come with a layout argument or similar. And since these are grid graphs, they're impervious to base graph tricks like par(mfrow=c(1,2)).
How can I place the two graphs above in a single plot, with both graphs on the same line?
I already tried the suggestions in How to plot grid plots on a same page?, but they don't seem to work for vcd plots. Ultimately I would like to obtain something similar to:
Neither plot seems to return any object and I cant see how to grab the grobs from looking at grid.ls(). So using the idea from this answer
library(vcd)
library(gridGraphics)
library(gridExtra)
mosaic(Titanic)
m <- grid.grab()
assoc(Titanic)
a <- grid.grab()
grid.newpage()
grid.arrange(m, a, ncol=2)
Im sure there will be a more grid-like approach but ...
Something similar to the solution in How to plot grid plots on a same page? can also be used for vcd displays. The difference is that you need to set newpage = FALSE (to prevent opening a new display) and you need to push and pop the viewport yourself (which can be handy when re-using vcd graphics in more complicated displays such as partykit trees).
The mosaic and association display for the Titanic data can be visualized as:
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))
mosaic(Titanic, newpage = FALSE)
popViewport()
pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
assoc(Titanic, newpage = FALSE)
popViewport()
yielding
Another option is vcd’s mplot() function (for details, see ?vcd::mplot):
library(vcd)
mplot(
mosaic(Titanic, return_grob = TRUE),
assoc(Titanic, return_grob = TRUE),
keep_aspect_ratio = FALSE
)

Resources