R: ecdf with bars instead of line - r

Is it possible to plot the
ecdf function in R with bars instead of a line ore steps?
Ore is there another way to plot the cumulative histogram in ggplot with cumulative densities on the y-axis instead of frequencys?

ggplot has a stat_ecdf() function out of the box, but it doesn't support geom = "bar". We can use stat_bin() in combination with the special variable ..density.. to arrive at a bar plot version of ecdf:
library(ggplot2)
ggplot(df, aes(x)) + stat_ecdf(col = "red") +
stat_bin(aes(y = cumsum(..density..)/
sum(..density..)),
alpha = 0.8)

Related

Duplicate legends in overlayed density plots using ggplot2

I am trying to generate density plot with two overlaid distributions using ggplot2. My data looks like:
diag_elements <- data.frame(x = c(diag(Am.dent), diag(Am.flint)),
group=rep(c("Dent", "Flint"), c(length(diag(Am.dent)), length(diag(Am.flint)))))
And my call to ggplot is:
ggplot(diag_elements) +
geom_density(aes(x=x, colour=group, fill=group), alpha=0.5) +
labs(x = "Diagonal elements of the matrix", y = "Density", fill = "Heterotic Group") +
theme(legend.position = c(0.85, .75))
However, instead of simply renaming the legend with the more complete name specified in fill, this generates a second legend:
Does anyone have any suggestions for getting this same graph, but without the improperly formatted legend?
Thanks!
The other option is guides which allows specific removal of certain legneds. You simply add to your ggplot
+guides(color=FALSE)

ggplot doesn't plot two geom in one figure R

I try to plot both geom_histogram and geom_density in one figure. When I plot the two separate from each other I get for each the output I want (histogram and density plot) but when I try combining them, only the histogram is showed (regardless of which order of the histogram/density in the code).
My code looks like this:
ggplot(data=Stack_time, aes(x=values))+geom_density(alpha=0.2, fill="#FF6666")+
geom_histogram(binwidth = 50, colour="black", fill="#009454")
I do not receive any error message, but the geom_density is never shown in combination with the geom_histogram.
Since you did not provide any data here a solution based on mtcars:
Your code is nearly correct. You need to add an alpha value to your histogram, so you can see the density. But also you need to scale your data, since the density plot is between the range of 0 and 1. If you got data values larger then 1, the density plot can be tiny and you can't see it. With the function scale_data as defined as follows, i scale my data to the range of 0-1
df=mtcars
scale_data <- function(x){(x-min(x))/(max(x)-min(x))}
df$mpg2 <- scale_data(df$mpg)
library(ggplot2)
ggplot(data=df, aes(x=mpg2))+geom_density(alpha=0.2, fill="#FF6666")+
geom_histogram(binwidth = 50, colour="black", fill="#009454", alpha = 0.1)
this gives the expected output:
you can adjust this solution to your needs. Just scale the data or the density plot to the data
This should do the job, approximately:
data.frame(x=rnorm(1000)) %>% ggplot(aes(x, ..density..)) + geom_histogram(binwidth = 0.2, alpha=0.5) + geom_density(fill="red", alpha=0.2)

R: Density plot with colors by group?

I have data from 2 populations.
I'd like to get the histogram and density plot of both on the same graphic.
With one color for one population and another color for the other one.
I've tried this (example):
library(ggplot2)
AA <- rnorm(100000, 70,20)
BB <- rnorm(100000,120,20)
valores <- c(AA,BB)
grupo <- c(rep("AA", 100000),c(rep("BB", 100000)))
todo <- data.frame(valores, grupo)
ggplot(todo, aes(x=valores, fill=grupo, color=grupo)) +
geom_histogram(aes(y=..density..), binwidth=3)+ geom_density(aes(color=grupo))
But I'm just getting a graphic with a single line and a single color.
I would like to have different colors for the the two density lines. And if possible the histograms as well.
I've done it with ggplot2 but base R would also be OK.
or I don't know what I've changed and now I get this:
ggplot(todo, aes(x=valores, fill=grupo, color=grupo)) +
geom_histogram( position="identity", binwidth=3, alpha=0.5)+
geom_density(aes(color=grupo))
but the density lines were not plotted.
or even strange things like
I suggest this ggplot2 solution:
ggplot(todo, aes(valores, color=grupo)) +
geom_histogram(position="identity", binwidth=3, aes(y=..density.., fill=grupo), alpha=0.5) +
geom_density()
#skan: Your attempt was close but you plotted the frequencies instead of density values in the histogram.
A base R solution could be:
hist(AA, probability = T, col = rgb(1,0,0,0.5), border = rgb(1,0,0,1),
xlim=range(AA,BB), breaks= 50, ylim=c(0,0.025), main="AA and BB", xlab = "")
hist(BB, probability = T, col = rgb(0,0,1,0.5), border = rgb(0,0,1,1), add=T)
lines(density(AA))
lines(density(BB), lty=2)
For alpha I used rgb. But there are more ways to get it in. See alpha() in the scales package for instance. I added also the breaks parameter for the plot of the AAs to increase the binwidth compared to the BB group.

Graphing an average of existing data in R

The the graph I wish to emulate is this:
The graph I have now is this:
What kind of geom would I use to emulate the black line in the first chart?
I am currently using geom_smooth but am aware I might be on the wrong track:
p <- ggplot(df_test1, aes(time, reading))
p + geom_point(alpha = 1/4, colour = "#7F0019")+geom_smooth(colour = "black")+
scale_x_date(breaks="month", labels=date_format("%b"))
Using geom_line results in the following chart:

Plot density with ggplot2 without line on x-axis

I use ggplot2::ggplot for all 2D plotting needs, including density plots, but I find that when plotting a number of overlapping densities with extreme outliers on a single space (in different colors) the line on the x-axis becomes a little distracting.
My question is then, can you remove the bottom section of the density plot from being plotted? If so, how?
You can use this example:
library(ggplot2)
ggplot(movies, aes(x = rating)) + geom_density()
Should turn out like this:
How about using stat_density directly
ggplot(movies, aes(x = rating)) + stat_density(geom="line")
You can just draw a white line over it:
ggplot(movies, aes(x = rating)) +
geom_density() +
geom_hline(color = "white", yintercept = 0)

Resources