The difference between geom_density in ggplot2 and density in base R -


i have data in r following:

  bag_id location_type            event_ts 2     155        sorter 2012-01-02 17:06:05 3     305       arrival 2012-01-01 07:20:16 1     155      transfer 2012-01-02 15:57:54 4     692       arrival 2012-03-29 09:47:52 10    748      transfer 2012-01-08 17:26:02 11    748        sorter 2012-01-08 17:30:02 12    993       arrival 2012-01-23 08:58:54 13   1019       arrival 2012-01-09 07:17:02 14   1019        sorter 2012-01-09 07:33:15 15   1154      transfer 2012-01-12 21:07:50 

where class(event_ts) posixct.

i wanted find density of bags @ each location in different times.

i used command geom_density(ggplot2) , plot nice. wonder if there difference between density(base) , command. mean difference methods using or default bandwith using , like.

i need add densities data frame. if had used function density(base), knew how can use function approxfun add these values data frame, wonder if same when use geom_density(ggplot2) .

a quick perusal of ggplot2 documentation geom_density() reveals wraps functionality in stat_density(). first argument there references adjust parameter coming base function density(). so, direct question - built off of same function, though exact parameters used may different. have control on setting parameters, may not able have amount of flexibility want.

one alternative using geom_density() calculate density want outside of ggplot() , plot geom_line(). example:

library(ggplot2) #100 random variables x <- data.frame(x = rnorm(100)) #calculate own density, set parameters desire d <- density(x$x) x2 <- data.frame(x = d$x, y = d$y)  #using geom_density() ggplot(x, aes(x)) + geom_density() #using home grown density ggplot(x2, aes(x,y)) + geom_line(colour = "red") 

here, give identical plots, though may vary more data , settings.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -