Figuring out cause of Graphical differences between ggplot and base R plotting - r

I have plotted my data in both base R and ggplot methods to see how the plots look different, and my graph from ggplot() form looks wrong. It should look like it does when I graph it in base R. Shown below is my base R code and my ggplot code, and the graphs that each produce.
Base R code:
em is though.

Use geom_path() instead of geom_line() to preserve the ordering of the dataset. This is documented in ?geom_line

Related

How do I plot a scatterplot graph with a repeated-measures variable and a continuous variable in r?

I have a four levels repeated measures variable (let's call it RM) and I have a continuous variable predictor (let's call it C).
I want to plot a scatterplot graph with C on the X-Axis and RM on the Y-Axis with different lines within the plot for each level of RM.
Is this possible to do with ggplot or a similar package?
Thanks in advance!
Utilizing ggplot2, you should be able to achieve this type of graphical output. Viewing a portion of your data that you wish to plot would be beneficial to provide a sufficient answer.
I have attached a link to a summary of ggplot2 graphical functions here. This link provides some background on ggplot2 and the components necessary to create a graph. Three components are needed in ggplot2 to make a graph, the data, the ggplot2 function, and mapping your variables with the aesthetic.
Because you don't have a representation of some of your data, providing a sufficient answer is difficult, but it might look something like this.
ggplot(data=yourdata, aes(x= C(continuous_variable, y = RM(repeated_measures)) +
geom_point()
You may also map geom_line for each RM variable in addition to this example. Hope this helps!

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.

similar to ggplot options in googlevis in R

Example from R graphics cookbook pg 55.
ggplot(tg, aes(x=factor(dose), y=length, colour=supp, group=supp)) + geom_line()
Looking for a similar plotting options in googlevis, on how to specify the fill options or color option which is a factor variable.
Please take a look at my answer for ggplot2 equivalent of 'factorization or categorization' in googleVis in R.
It has diagrams and examples.
What you are looking for is called roles in goooglevis and involving appending data columns with set names and linked to your variables. For example, if your variable is py you will add a column py.style where you set the fill colours.
#mages has this documented on this webpage, which shows features not in demo(googleVis):
http://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html
All the best, micstr

how to use ggplot2 make recursive partitioning survival tree

can everyone tell me how to use ggplot2 make recursive partitioning survival tree?
I know " party " package can make a plot in R, however the plot is not looking good, and all the plots in the rest of my report are made by ggplot2.
for my plot, I printsreen and added labels manually. if there is a way to input data and plot in R also works, Thanks!!

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