how to make a biplot without label in R - 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)

Related

how can i show two histogram in same window but different plots in R?

I want to show the effect of removing outliers on my histogram, so I have to plot both hists together.
boxplot(Costs, Costs1,
xlab=" Costs and Costs after removig outliers",
col=topo.colors(2))
so i tried this:
hist(Costs,Costs1,main="Histogram of Maintenance_cost ",col="blue",
border="darkblue",xlab="Total_cost",ylab=" ",yaxt = 'n',
#ylim=c(0,3000),
#xlim=c(0,max(My_Costs)),
breaks=60)
the first code give me to box plot, but I tried it for hist it doesn't work
can anyone tell me how to do it in R?
For a base R solution, use par with mfrow.
set.seed(1234)
Costs = rnorm(5000, 100, 20)
OUT = which(Costs %in% boxplot(Costs, plot=FALSE)$out)
Costs1 = Costs[-OUT]
par(mfrow=c(1,2), mar=c(5,1,2,1))
hist(Costs,main="Histogram of Maintenance_cost ",col="blue",
border="darkblue",xlab="Total_cost",ylab=" ",yaxt = 'n',
breaks=60, xlim=c(30,170))
hist(Costs1,main="Maintenance_cost without outliers",col="blue",
border="darkblue",xlab="Total_cost",ylab=" ",yaxt = 'n',
breaks=60, xlim=c(30,170))
For multiple plots, you should use ggplot2 with facet_wrap. Here is an example:
Plot several histograms with ggplot in one window with several variables

Side by side wordclouds in R markdown

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))

r: decisoin tree label doesn't show completly

When I am using rpart to draw the decision tree, there is a small problem, the label of the plot does not works well.
The plot only have a half text on the top and bottom of the plot. How can I fix this.
Here is the code:
library(rpart)
iris.rpart = rpart(Species ~ ., data = iris)
plot(iris.rpart) #Plot the tree
text(iris.rpart) #Show the labels
When you read the documentation of plot.rpart, there are two options mentioned: use of the par option xpd, or using the parameter margin of the plot.rpart function.
1)
The margin parameter adds an extra portion of white space
library(rpart)
iris.rpart = rpart(Species ~ ., data = iris)
plot(iris.rpart, margin = .2) # margin added
text(iris.rpart, use.n = T)
2)
Looking at your picture, it could be that xpd has been set to FALSE (see ?par). From the documentation:
xpd: A logical value or NA. If FALSE, all plotting is clipped to the plot
region, if TRUE, all plotting is clipped to the figure region, and if
NA, all plotting is clipped to the device region. See also clip.
You can see the setting of xpd by typing:
par()xpd
in that case xpd is not TRUE, the solution is as follows:
opar <- par() # to reset later
par(xpd=TRUE)
plot(iris.rpart)
text(iris.rpart, use.n = T)
par <- opar # restore old setting
Please, let me know whether this solved your problem.

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"))

Resources