Add a curve to a log/log scatterplot - r

I have a scatterplot in a log/log space
plot(a,b,log="xy")
or in ggplot2
qplot(a,b,data="time",log="xy")
Now I would like to impose upon this scatter plot the curve f(x)=x*x+2. Butthe function woudl need to be plotted in the logarithmic space as well. How would I do this? Is there an way to do this in ggplot2?

As you guessed, curve is the command that you're looking for in base graphics.
#Make up some data
set.seed(0)
a <- 1:10
b <-(a^2+2)*exp(0.1*rnorm(10))
plot(a,b,log='xy')
curve(x^2+2,add=TRUE)
in ggplot2 world:
qplot(a,b,data=time)+stat_function(fun=function(x){x^2+2}) + coord_trans(xtrans = "log10",ytrans="log10")
from Plotting in R using stat_function on a logarithmic scale seems to do what you're after.

Related

Beeswarm with logarithmic X axis

I am trying to plot a "beeswarm" which is essentially a single-dimensional scatterplot
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
What I want to achieve is logarithmic transformation of the X axis.
Obviously, I can do beeswarm(log(breast$time_survival),horizontal=TRUE,method="hex") but it plots logarithmically transformed data and X axis no longer reperesents survival time numerically. Is there a way to directly affect the X axis? In regular scatterplot, I would do plot(breast$time_survival,log="x") but not sure how to behave with beeswarm
option for beeswarm is log=TRUE, not log="x"
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE, log=T)

How to make a ggplot2 contour plot analogue to lattice:filled.contour()?

I've been learning ggplot2, and hope to use it for all my R graphing. However, I've yet to find a way to make a contour plot that looks analogous to a conventional contour plot, like what can be obtained using lattice:filled.contour(). For example:
#define data
x<-seq(1,11,1)
y<-seq(1,11,1)
xyz.func<-function(x,y) {-10.4+6.53*x+6.53*y-0.167*x^2-0.167*y^2+0.0500*x*y}
#contour plot using lattice graphics and R Color Brewer
library(lattice) #for filled.contour()
library(RColorBrewer) #for brewer.pal()
z.lattice<-outer(x,y,xyz.func)
filled.contour(x,y,z.lattice,nlevels=6,col=brewer.pal(6,"YlOrRd"))
This gives me a nice contour plot.
Now, let's try the same thing in ggplot2. The best I can come up with, based on everything I've read (particularly Drawing labels on flat section of contour lines in ggplot2) is:
#contour plot using ggplot2
library(ggplot2)
library(reshape2) #for melt()
z.molten<-melt(z.lattice)
names(z.molten) <- c("x", "y", "z")
v<-ggplot(z.molten, aes(x,y,z=z))+
geom_tile(aes(fill=z))+
stat_contour(bins=6,aes(x,y,z=z), color="black", size=0.6)+
scale_fill_gradientn(colours=brewer.pal(6,"YlOrRd"))
v
This graph has the same basic idea as filled.contour(), but the colored tiles don't conform to the contours very well.
I haven't been successful with changing the sizes of the tiles, either.
Any suggestions on how to make ggplot2's output closer to filled.contour()'s output?
The essence of your question, it seems, is how to produce a contour plot in ggplot with discrete filled contours, rather than continuous contours as you would get using the conventional geom_tile(...) approach. Here is one way.
x<-seq(1,11,.03) # note finer grid
y<-seq(1,11,.03)
xyz.func<-function(x,y) {-10.4+6.53*x+6.53*y-0.167*x^2-0.167*y^2+0.0500*x*y}
gg <- expand.grid(x=x,y=y)
gg$z <- with(gg,xyz.func(x,y)) # need long format for ggplot
library(ggplot2)
library(RColorBrewer) #for brewer.pal()
brks <- cut(gg$z,breaks=seq(0,100,len=6))
brks <- gsub(","," - ",brks,fixed=TRUE)
gg$brks <- gsub("\\(|\\]","",brks) # reformat guide labels
ggplot(gg,aes(x,y)) +
geom_tile(aes(fill=brks))+
scale_fill_manual("Z",values=brewer.pal(6,"YlOrRd"))+
scale_x_continuous(expand=c(0,0))+
scale_y_continuous(expand=c(0,0))+
coord_fixed()
The use of, e.g., scale_x_continuos(...) is just to get rid of the extra space ggplot puts around the axis limits; fine for most things but distracting in contour plots. The use of coord_fixed(...) is just to set the aspect ratio to 1:1. These are optional.

Minor ticks at density log plot in lattice (R)

I need to create a density plot using the lattice package of R. The x-axis has a logarithmic scale. Minimal example:
densityplot( ~ mpg, data=mtcars, scales=list(x=list(log=TRUE)))
Is it possible to put minor tick marks between the major ticks? In xyplot this is possible with xscale.components=xscale.components.log10ticks, but this does not work with densityplots or histograms.
I found out that this needs
library("lattice")
library("latticeExtra")
Now xscale.components=xscale.components.log10ticks works (for all lattice plots I think). I did not know that the latticeExtra is needed.

Making a wireframe plot from an x,y,z data.frame

I have a data.frame of x/y/z points. I know how to make a 3d scatterplot using the rgl package but I would like to connect each point in the scatterplot to make a wireframe or surface plot.
This code returns the scatter plot
library(rgl)
Data <- expand.grid(x=seq(0,10),y=seq(0,10))
Data$z <- Data$x^2+Data$y^2
plot3d(Data)
While this code returns a blank graph:
plot3d(Data,type='wire')
I can make the plot I want with lattice:
library(lattice)
wireframe(z~x+y,Data)
I can even make it rotate:
library(TeachingDemos)
rotate.wireframe(z~x+y,Data)
But I prefer rgl over lattice because it renders much quicker and lets you rotate the plot with the mouse.
Whats the proper way to make a wireframe plot in rgl?
The surface drawing plot function in rgl is persp3d and like base::persp, it needs a matrix as input to the z argument
zmat <- matrix(Data$z, 11,11)
persp3d(x=seq(0,10), y=seq(0,10), z=zmat)
I did spin this graphic a bit before capturing it with a screen grabbing program I use:

Problem with axis limits when plotting curve over histogram [duplicate]

This question already has an answer here:
How To Avoid Density Curve Getting Cut Off In Plot
(1 answer)
Closed 6 years ago.
newbie here. I have a script to create graphs that has a bit that goes something like this:
png(Test.png)
ht=hist(step[i],20)
curve(insert_function_here,add=TRUE)
I essentially want to plot a curve of a distribution over an histogram. My problem is that the axes limits are apparently set by the histogram instead of the curve, so that the curve sometimes gets out of the Y axis limits. I have played with par("usr"), to no avail. Is there any way to set the axis limits based on the maximum values of either the histogram or the curve (or, in the alternative, of the curve only)?? In case this changes anything, this needs to be done within a for loop where multiple such graphs are plotted and within a series of subplots (par("mfrow")).
Inspired by other answers, this is what i ended up doing:
curve(insert_function_here)
boundsc=par("usr")
ht=hist(A[,1],20,plot=FALSE)
par(usr=c(boundsc[1:2],0,max(boundsc[4],max(ht$counts))))
plot(ht,add=TRUE)
It fixes the bounds based on the highest of either the curve or the histogram.
You could determine the mx <- max(curve_vector, ht$counts) and set ylim=(0, mx), but I rather doubt the code looks like that since [] is not a proper parameter passing idiom and step is not an R plotting function, but rather a model selection function. So I am guessing this is code in Matlab or some other idiom. In R, try this:
set.seed(123)
png("Test.png")
ht=hist(rpois(20,1), plot=FALSE, breaks=0:10-0.1)
# better to offset to include discrete counts that would otherwise be at boundaries
plot(round(ht$breaks), dpois( round(ht$breaks), # plot a Poisson density
mean(ht$counts*round(ht$breaks[-length(ht$breaks)]))),
ylim=c(0, max(ht$density)+.1) , type="l")
plot(ht, freq=FALSE, add=TRUE) # plot the histogram
dev.off()
You could plot the curve first, then compute the histogram with plot=FALSE, and use the plot function on the histogram object with add=TRUE to add it to the plot.
Even better would be to calculate the the highest y-value of the curve (there may be shortcuts to do this depending on the nature of the curve) and the highest bar in the histogram and give this value to the ylim argument when plotting the histogram.

Resources