Par() function in R,plot not shown - r

Hey there stackoverflow geeks, I am working with a dataset in R language using R studio. I want to display my pareto charts side by side so better analysis can be done.
When I use par() function , i only get the first plot with an empty white space on right, can anyone please help me with what the issue is?
library(qcc)
stateRegion <- table(state.region)
stateDivision <- table(state.division)
par(mfrow=c(1,2))
pareto.chart(stateRegion) #only this one appears
pareto.chart(stateDivision) #this one is not plotting
EDIT
The code is working with all other plots except pareto chart/plot.
Please help.

Related

par() function acting up in Rstudio - plotting weird results or no results at all

I've found a couple of similar posts but unfortunately not one reply seems to fix my problem.
Data = working on the uscrime dataset.
Essentially, I'm having trouble my plots when using par() in RStudio. It's not the first time it happens that all of the sudden after re-rerunning a chunk in R, the plot is no longer displayed inline nor in the Plots window.
My code is the following:
par(mfrow=c(3,5))
for(i in colnames(crime[,1:15])){
plot(crime[,i],crime[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime[,i],crime[,16]), col="red")
abline(lm(crime[,16]~crime[,i]), col="blue")
}
The first time I ran the chunk the plots showed up:
Now, as soon as I tried to plot something different (same data but with a transformed column) using the same approach, nothing appeared inline. Here's my code:
par(mfrow=c(3,5))
for(i in colnames(crime2[,1:15])){
plot(crime2[,i],crime2[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime2[,i],crime2[,16]), col="red")
abline(lm(crime2[,16]~crime2[,i]), col="blue")
}
I tried restarting R and running everything again and ow I'm getting a weird result where some of the information I passed to my first par() call is getting displayed in my charts (see how I have two blue curves when I should have a blue and a red one).
Now, I've been reading about 'devices' and plots but since I'm an R noob I can't figure out what's wrong on my own. It looks like I have to call dev.off() at some point but I don't quite get it. My expectation is that there's something wrong (or an additional step to take) with my par() calls but I haven't found the replies in R Documentation.
Here's a view of my Markdown config

How can I do a multiple plot with a somSC type graph?

I'm sure this is an easy one, but I dont know why this code cannot plot two graphs in the same window.
nfil=5
ncol=5
set.seed(850)
som <- trainSOM(x.data=datasom[3:8],
dimension=c(nfil,ncol),
nb.save = 100,
verbose=TRUE)
# SUPERCLUSTERS
num.grupos=4
som.sc <- superClass(som, k=num.grupos)
### PLOTEO SOM
par(mfrow=c(1,2))
plot(som.sc, plot.var=FALSE)
# observaciones de cada neurona
counts<-as.vector(som.sc$som$clustering)
kounts<-c(0)
for (i in 1:(nfil*ncol)) {
kounts[i]=0
}
for (i in 1:(length(counts))) {
kounts[counts[i]]=kounts[counts[i]]+1
}
plot(som.sc, type="grid",names=kounts)
As far I read in google, with the par(mfrow=c(1,2)) line should be fine, but it doesn't work. I ran the code and the first plot used the first half of the window, as it should be. But, the second plot use the entire window. Please, help me!. Thanks.
P.S.: The review of this post didn't allow me to write the entire code. I'm using the SOMbrero package.
Heatmaps will probably not work with par(). Maybe these will help:
R: arranging multiple plots together using gridExtra
to display two heatmaps in same pdf side by side in R

Difference in plot when shown in R Studio and exported with ggsave()

I want to add an additional tick to my plot in ggplot2, and used Solution 2 as described in the post Annotate ggplot with an extra tick and label.
It worked fine for me, giving the following result in R Studio:
But when I try to save the result using ggsave() to create the a .pdf, .ps, or .png file, the red number is cut off half like this:
I have the feeling that the inner plot is printed first and later the margins are plotted on top of this.
Anybody has a hint?
Thank you Z. Lin! I just had a grid.draw(g) instead of g <- grid.draw(g). This dot in R always activates my python brain region :)

Stacked stripchart in R getting cut off?

I am trying to create a dotplot for some sampling distributions. I have created one for the medians of random samples of a uniform distribution. However the chart is getting truncated erroneously at the top. I have tried to reset with a ylim vector to no avail.
B <- replicate(500,median(sample(c(0:9),20,replace=T)))
stripchart(B, method="stack",pch=16,offset =0.5,at=0)
dotplot
Any suggestions?
I could not make it work using stripchart and I don't know why. I hope barplot delivers what you need:
barplot(table(B))
Hopefully you can work with that.

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.

Resources