Stacked stripchart in R getting cut off? - r

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.

Related

Histogram not displaying properly

I am working on a diametric class distribution data, with over 10,000 individuals, and I wanted to make a histogram to show the results, but I am having a problem when I try to plot it.
When I tried using the histogram function
histogram(data)
I get a histogram that is calculating the percentage of individuals
Histogram using the histogram function
Then I tried using qplot function
qplot(data, geom = "histogram")
And got a histogram like the one you can see in image 2
qplot function
So I thought maybe is a problem with the y-axis scale so I tried using ylim() and the plot I got was only the axis with no data
Could someone please tell me what I'm doing wrong?
I'm trying to get a histogram like the one in image 3Histogram goal
I really appreciate your help to point out if I am not adding other codes or maybe redirect me to a cheat sheet that can clarify some things
thanks
Try to use the ggplot2 package, for example:
library(ggplot2)
ggplot(data = [YOUR_DATA], aes([YOUR_VARIABLE])) +
geom_histogram()
EDIT:
Looking at your desired result, I think you want to use barplot() instead of histogram().

Failure of producing a histogram in R if using breaks=a numeric vector

Is there any difference between using breaks=c(1,2,3,4,5) and breaks=c(1:5)?
I am using hist(...) function to get a histogram for a dataset.
If I use hist(MyDataFile$V3, breaks=c(1,2,3,4,5)), then I get the correct histogram.
If I use hist(MyDataFile$V3, breaks=c(1:5)) or
hist(MyDataFile$V3, breaks=seq(1,5,1)), then the histogram does not show correct rectangles, and it only gives me a lot of vertical parallel lines at some breaks instead, but not the correct histogram.
I cannot figure out what is wrong with my the value of my breaks.
[UPDATA] It is a silly mistake. The problem is solved if set freq=FALSE. Thank you all for your time.

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.

R - Adding series to multiple plots

I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')

R statistics: Drawing multiple plots in RGL

Does anyone know how to draw multiple 3d plots in one picture using RGL in R Statistics.
I have three variables and each of those variables belong to two groups. I want each group to have a different color so I can visualize it. In regular R stats, I just use subset and then use par(new=T). I haven't seen anything equivalent for the 3d plot. Does anyone have any suggestion?
Thanks!
try plot3d(x, y, z, add=TRUE)
Admittedly I was a bit surprised when it worked, I thought it would throw an error on the first plot, but i guess it creates an existing plot if none exists and otherwise adds the points to the existing plot

Resources