density-plot

2d density plot for categories

丶灬走出姿态 提交于 2019-12-23 04:58:10
问题 I'm trying to make a 2d density plot where the density is displayed for each category. For example, in the image below, we have a density plot for each day, and all the daily densities are combined into the coloured plots. These types of plots are common in the scientific literature on atmospheric sciences and aerosol pollution studies. So far I've got this ggplot(dat, aes(y = `dN/dlogDp`, x = date)) + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) + scale_fill_gradient

Creating density plots from two different data-frames using ggplot2

左心房为你撑大大i 提交于 2019-12-22 09:59:51
问题 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

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

孤者浪人 提交于 2019-12-21 06:05:12
问题 This question already has answers here : Find the probability density of a new data point using “density” function in R (3 answers) Density Value for each Return (3 answers) Closed 2 years ago . 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)) ) 回答1: Use linear interpolation to

Adding a mean to geom_density_ridges

≡放荡痞女 提交于 2019-12-20 01:59:34
问题 I am trying to add means using geom_segment to a geom_density_ridges plot made in ggplot2. library(dplyr) library(ggplot2) library(ggridges) Fig1 <- ggplot(Figure3Data, aes(x = `hairchange`, y = `EffortGroup`)) + geom_density_ridges_gradient(aes(fill = ..x..), scale = 0.9, size = 1) ingredients <- ggplot_build(Fig1) %>% purrr::pluck("data", 1) density_lines <- ingredients %>% group_by(group) %>% filter(density == mean(density)) %>% ungroup() p <- ggplot(Figure3Data, aes(x = `hairchange`, y =

add vertical line of the mean to density plot

前提是你 提交于 2019-12-19 04:58:11
问题 Following the example from this website, we can generate a density plot for several factors within a data.frame library(sm) attach(mtcars) sm.density.compare(mpg, cyl, xlab="Miles Per Gallon") My question is very simple, how can be add a vertical line for each factor which represents the median or the mean ? 回答1: This is an example for the mean . To calculate the median , simply replace "FUN = mean" with "FUN = median" in the aggregate function. library(sm) attach(mtcars) sm.density.compare

The difference between geom_density in ggplot2 and density in base R

 ̄綄美尐妖づ 提交于 2019-12-18 08:57:26
问题 I have a data in R like the 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) is POSIXct . I wanted to find the density of bags at each

How to shade part of a density curve in ggplot (with no y axis data)

て烟熏妆下的殇ゞ 提交于 2019-12-18 05:43:34
问题 I'm trying to create a density curve in R using a set of random numbers between 1000, and shade the part that is less than or equal to a certain value. There are a lot of solutions out there involving geom_area or geom_ribbon , but they all require a yval , which I don't have (it's just a vector of 1000 numbers). Any ideas on how I could do this? Two other related questions: Is it possible to do the same thing for a cumulative density function (I'm currently using stat_ecdf to generate one),

How to overlay density plots in R?

Deadly 提交于 2019-12-17 04:13:25
问题 I would like to overlay 2 density plots on the same device with R. How can I do that? I searched the web but I didnt find any obvious solution (I am rather new to R). My idea would be to read data from a text file (columns) and then use plot(density(MyData$Column1)) plot(density(MyData$Column2), add=T) well something in this spirit... Thanks in advance 回答1: use lines for the second one: plot(density(MyData$Column1)) lines(density(MyData$Column2)) make sure the limits of the first plot are

create a manual legend for density plots in R (ggplot2)

寵の児 提交于 2019-12-13 03:24:50
问题 I want to add a legend to my graph. All solutions I found online use scale_color_manual - but it's not working for me. Where is the legend? Here is my code: library(ggplot2) ggplot() + geom_density(aes(x = rnorm(100)), color = 'red') + geom_density(aes(x = rnorm(100)), color = 'blue') + xlab("Age") + ylab("Density") + ggtitle('Age Densities') theme(legend.position = 'right') + scale_color_manual(labels = c('first', 'second'), values = c('red', 'blue')) 回答1: If for some reason you absolutely

R density plot y axis larger than 1

故事扮演 提交于 2019-12-12 09:23:19
问题 I want a density plot, and here is the code: d = as.matrix(read.csv(file = '1.csv')) plot(density(d)) my data is a list of number. What I don't understand is that the value of y axis large than 1 I think there is something wrong and search the internet, but i can't find any resource, Can you guys help me? enter image description here here is the data: link:http://pan.baidu.com/s/1hsE8Ony password:7a4z 回答1: There is nothing wrong with the density being greater than 1 at some points. The area