Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 208715

Overlay two stat_density plots

$
0
0

I'd like to overlay two density plot from two one-dimensional array. I need them both in log-log scale. The files contain just the list of numbers (1.000.000) both generated from very similar specific distributions with a small peak around 4000, thus the log scale on one or both axes.

> prova
   [1]    1.106172    2.617064    1.778360    1.372890    5.334664    2.019493
   [7]    1.174879    1.371897    4.018011    1.220845    3.631309    1.437586 ...

This is what I use for just one array:

#Packages
library(tidyverse)
library(plotly)
library(ggplot2)
library(scales)
library(ggforce)

prova <-readRDS("probcond1.rds")
prova1 <-readRDS("probpoly.rds")
dfGamma <-data.frame(prova)

g <- ggplot(dfGamma, aes(x=prova)) +
  stat_density(aes(y=..count..), color="black", fill="blue", alpha=0.3) + scale_x_continuous(breaks=c(0,1,2,3,4,5,10,30,100,300,1000,2000,3000,4000,5000), trans="log1p", expand=c(0,0)) + scale_y_continuous(expand=c(0,0), trans="log1p") + theme_bw()

I tried to copy a solution seen in another answer but in this case weirdly the graph is not in log-log scale.

x <- data.frame(v1=prova,v2=prova1)
data<- melt(x)
ggplot(data,aes(x=value, fill=variable)) + stat_density(alpha=0.3) +
scale_x_continuous(breaks=c(0,1,2,3,4,5,10,30,100,300,1000,2000,3000,4000,5000), trans="log1p", expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0), trans="log1p") +
  theme_bw()

I'm very new to R and I'm not sure how can I build a data.frame from my two lists and how can I manipulate it. Thank you fro your patience.


Viewing all articles
Browse latest Browse all 208715

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>