General questions about Principal Component Analysis (PCA) in R - r

I would like to produce some nice PCA plots in R. As usual, in R, there are several ways to perform a principal component analysis. I found so far 3 different ways of how to calculate your components and 3 ways of plotting them. I was wondering whether people who are familiar with these functions can give me some advise on the best combination of functions to produce the following plots:
Scores Plot
Loadings Plot
Histogram / Bar chart of the variances explained by each principal component
My research on functions and plots used for PCA in R resulted in:
Functions:
pca.xzy()
prcomp()
princomp()
dudi.pca()
Plot:
plot.pca (this one seems to belong to the function pca.xzy())
ggplot2
plot
biplot
I also found the following webpage:
http://pbil.univ-lyon1.fr/ade4/ade4-html/dudi.pca.html
And I was wondering if you can draw those circles and lines starting from each of the circle centers with one of the other functions mentioned above as the function dudi.pca from the ade4 package seems to be the most complicated one.

One question per question, please! There's psych package by William Revelle, see this and this. There's also a good tutorial here. Anyway...
for scores/loadings plot see pairs
histogram: see hist
So once again, what's your question actually? =)

Related

How to do “Main Effects” Plots, “Interaction” Plots, and “Residual” Plots after PERMANOVA as mentioned in Anderson(2017) in R?

I have done a PERMANOVA and gotten significant results. I would like to do “Main Effects” Plots, “Interaction” Plots, and “Residual” Plots to show the relative importance of factors. However, I couldn't find any function or package in R that would do it. I couldn't find any codes for doing it. I am new to both PERMANOVA and R. Can anyone help me?

R GRMetric: How to personalize the plot design (themes, colors, legends, etc.)

I am relatively new to R and interested in Plotting Dose-Response curves. I have discovered GRMetrics which I found is very useful.
Example (as given in the documentary) will produce a scatter blot and plots the approximated function above them - perfect but how to persolanize the plot design with respect to the color of the points and curves, axis labels, thickness, etc.
In the documentaiton I found it is compatible with ggplot2 but I didn't found any example and didn't manage to integrate the output of GRMetric in a ggplot environment to edit the graph.
I appreciate any suggestion how to solve this issue.
The installation and code for GRMetrics:
if (!requireNamespace("BiocManager", quietly=TRUE))
install.packages("BiocManager")
BiocManager::install("GRmetrics")
install.packages("foreign")
browseVignettes("GRmetrics") #link to documentation
library(GRmetrics)
data(inputCaseA)
head(inputCaseA)
drc_output = GRfit(inputCaseA, groupingVariables =c('cell_line','agent'))
GRdrawDRC(drc_output)
In case someone is interested: one solution I found for this problem is....
1) export the curve parameters and data points from GRdrawDRC. The GRmetrics packages uses sigmoidal fitting and the function is given in its documentation
2) copy/insert the curve and all parameters in ggplot (e.g. as stat_function + geom_point()).
Then you are off to go and can format the graph easily.

changing default colours when using the plot function of the R package mixtools

I have a plotting problem with curves when using mixtools
Using the following R code
require(mixtools)
x <- c(rnorm(10000,8,2),rnorm(10000,18,5))
xMix <- normalmixEM(x, lambda=NULL, mu=NULL, sigma=NULL)
plot(xMix, which = 2, nclass=25)
I get a nice histogram, with the 2 normal curves estimated from the model superimposed.
The problem is with the default colours (i.e. red and green), which I need to change for a publication to be black and grey.
One way I thought to doing this was first to produce the histogram
hist(xMix$x, freq=FALSE, nclass=25)
and then add the lines using the "curve" function.
....... but I lost my way, and couldn't solve it
I would be grateful for any pointers or the actual solution
thanks
PS. Note that there is an alternative work-around to this problem using ggplot:
Any suggestions for how I can plot mixEM type data using ggplot2
but for various reasons I need to keep using the base graphics
You can also edit the colours directly using the col2 argument in the mixtools plotting function
For example
plot(xMix, which = 2, nclass=25, col2=c("dimgrey","black"))
giving the problem a bit more thought, I managed to rephrase the problem and ask the question in a much more direct way
Using user-defined functions within "curve" function in R graphics
this delivered two nice solutions of how to use the "curve" function to draw normal distributions produced by the mixture modelling.
the overall answer therefore is to use the "hist" function to draw a histogram of the raw data, then the "curve" function (incorporating the sdnorm function) to draw each normal distribution. This gives total control of the colours (and potentially any other graphic parameter).
And not to forget - this is where I got the code for the sdnorm function - and other useful insights
Any suggestions for how I can plot mixEM type data using ggplot2
Thanks as always to StackOverflow and the contributors who provide such helpful advice.

How to reproduce this graphical explanation (a scatter plot) of how covariance works?

I found this graphical intuitive explanation of covariance:
32 binormal points drawn from distributions with the given covariances, ordered from most negative (bluest) to most positive (reddest)
The whole material can be found at:
https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean
I would like to recreate this sort of graphical illustration in R, but I'm not sufficiently familiar with R's plotting tools. I don't even know where to start in order to get those colored rectangles between each pair of data points, let alone make them semi-transparent.
I think this could make a very efficient teaching tool.
The cor.rect.plot function in the TeachingDemos package makes plots similar to what is shown. You can modify the code for the function to make the plot even more similar if you desire.

Can I use shingles from lattice in ggplot2 in R

It is possible to use the shingles to define specific ranges in ggplot2. As far as i understand shingles are a way to generate groups. Can we create such shingles and use them in ggplot2 facet_grid to obtain graphs?
Following up from the comments, ggplot can't draw shingles (in the way lattice draws shingles with special indicators in the strip) and by default doesn't have a means of producing the overlapping groups.
However, I cam across this excellent PDF document which aims to produce a gpplot2 version of every figure in Depayan's excellent Lattice book (Lattice: Multivariate Data Visualization with R).
Page 31 contains a custom function fn() which replicates the behaviour of equal.count(), as far as I can tell, to provide the correct data structure to plot with overlapping shingles. The PDF contains plenty of examples of "shingles" in ggplot that you can play with.
So not sure if this answers the Q - but at least it appears one can fudge ggplot into producing plots that use the shingle concept.

Resources