density-plot

Creating density plots from two different data-frames using ggplot2

白昼怎懂夜的黑 提交于 2019-12-05 20:03:09
My goal is to compare the distribution of various socioeconomic factor such as income over multiple years to see how the population has evolved in particular region in say, over 5 years. The primary data for this comes from the Public Use Microdata Sample . I am using R + ggplot2 as my preferred tool. When comparing two years worth of data (2005 and 2010) I have two data frames hh2005 and hh2010 with the household data for the two years. The income data for the two years are stored in the variable hincp in both data frames. Using ggplot2 I am going about creating the density plot for

ggplot Poisson density curve: why zigzag lines?

为君一笑 提交于 2019-12-05 13:45:57
I would like to plot the density function of a Poisson distribution. I am not sure why I get a jaggy line (in blue). On the sample plot, the normal density curve (in red) looks smooth. It is because the reason the Poisson density function doesn't accept decimal values? How to eliminate the zigzag in the Poisson density plot? Thanks very much for any help. library(ggplot2) ggplot(data.frame(X = seq(5, 30)), aes(x = X)) + stat_function(fun=dpois, geom="line", size=2, color="blue3", args = list(lambda = 15)) + stat_function(fun=dnorm, geom="line", size=2, color="red4", args = list(mean=20, sd=2))

Plot density and cumulative density function in one combined plot using ggplot2

試著忘記壹切 提交于 2019-12-05 07:00:35
问题 I would like to get a plot that combines the density of observations and the cdf. The usual problem with that is that the scales of the two are way off. How can this be remedied, i.e., two scales be used or, alternatively, one of the data series be rescaled (preferably within ggplot, as I would like to separate computation and display of data). Here's the code so far: >dput(tmp) yields structure(list(drivenkm = c(8, 11, 21, 4, 594, 179, 19, 7, 10, 36)), .Names = "drivenkm", class = c("data

R: Weighted Joyplot/Ridgeplot/Density Plot?

五迷三道 提交于 2019-12-04 05:22:31
问题 I am trying to create a joyplot using the ggridges package (based on ggplot2 ). The general idea is that a joyplot creates nicely scaled stacked density plots. However, I cannot seem to produce one of these using weighted density. Is there some way of incorporating sampling weights (for weighted density) in the calculation of the densities in the creation of a joyplot? Here's a link to the documentation for the ggridges package: https://cran.r-project.org/web/packages/ggridges/ggridges.pdf I

Plot density and cumulative density function in one combined plot using ggplot2

不羁岁月 提交于 2019-12-03 21:46:02
I would like to get a plot that combines the density of observations and the cdf. The usual problem with that is that the scales of the two are way off. How can this be remedied, i.e., two scales be used or, alternatively, one of the data series be rescaled (preferably within ggplot, as I would like to separate computation and display of data). Here's the code so far: >dput(tmp) yields structure(list(drivenkm = c(8, 11, 21, 4, 594, 179, 19, 7, 10, 36)), .Names = "drivenkm", class = c("data.table", "data.frame" ), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x223cb78>) then I do p =

Exact kernel density value for any point in R [duplicate]

ε祈祈猫儿з 提交于 2019-12-03 20:39:00
This question already has answers here : Closed 2 years ago . Find the probability density of a new data point using “density” function in R (3 answers) Density Value for each Return (3 answers) I was wondering if there is a R base way to obtain the exact kernel density at any point desired? As an example, how can I get the exact kernel density at the 3 following points -2, 0, +2 on X-Axis in a plot like below? set.seed(2937107) plot( density(rnorm(1e4)) ) Use linear interpolation to find it. d <- density(rnorm(10000)) approx(d$x, d$y, xout = c(-2, 0, 2)) The precision of interpolation can be

Get rid of gray line in R density plot density at y = 0

烂漫一生 提交于 2019-12-02 12:16:22
问题 I have a density plot that I want to save in an eps file. My density plot is something like setEPS() postscript("myfile.eps") plot(density(rnorm(1000))) dev.off() just with my data and a little bit more complex (changing labels and margins, more lines on top of it, etc). My problem is that I get an annoying, horizontal thin gray line on top of my density plot at y = 0, and I'd like to get rid of it. When plotting directly to the X device I don't get this horizontal gray line, but in

Draw a line in a pm3d map

一曲冷凌霜 提交于 2019-12-02 01:49:31
问题 I have a and I want to overplot on it a . I define the line as a 3d line with constant height and I thought that with this method I have the possibility to overplot them one to another but, unfortunately, I failed. I realized in fact that the density map routine in gnuplot take the points of the line too. This is my script: reset #set terminal pngcairo size 800,500 enhanced font 'Verdana,14' #set output "map.png" set title 'Map' m_star=1.054 m_1=0.0093866 m_3=0.0078635 fact1 = (m_star+m_1)**0

calculate area of overlapping density plot by ggplot using R

醉酒当歌 提交于 2019-12-01 19:50:23
How can I get the area under overlapping density curves? How can I solve the problem with R? (There is a solution for python here: Calculate overlap area of two functions ) set.seed(1234) df <- data.frame( sex=factor(rep(c("F", "M"), each=200)), weight=round(c(rnorm(200, mean=55, sd=5), rnorm(200, mean=65, sd=5))) ) (Source: http://www.sthda.com/english/wiki/ggplot2-density-plot-quick-start-guide-r-software-and-data-visualization ) ggplot(df, aes(x=weight, color=sex, fill=sex)) + geom_density(aes(y=..density..), alpha=0.5) "The points used in the plot are returned by ggplot_build(), so you can

Plot median values on top of a density distribution

左心房为你撑大大i 提交于 2019-12-01 08:05:38
I'm trying to plot the median values of some data on a density distribution using the ggplot2 R library. I would like to print the median values as text on top of the density plot. You'll see what I mean with an example (using the "diamonds" default dataframe): I'm printing three itmes: the density plot itself, a vertical line showing the median price of each cut, and a text label with that value. But, as you can see, the median prices overlap on the "y" axis (this aesthetic is mandatory in the geom_text() function). Is there any way to dynamically assign a "y" value to each median price, so