Log histogram and distribution on ggplot

In this image you can see several distributions and a histogram

What I am trying to do is create a log plot (on the y axes) of this one where both the distributions and the histogram follows the log scale. I tried different method to make a log graph of this one, but none of them worked.
Can anyone help?
This is the code that i used

library(ggplot2)
library(VarianceGamma)
library(ghyp)

#csv imported
AAPL <- read.csv("AAPL1.csv")
MySeq <- seq(-0.2, 0.2, length.out = 100000)

#fitting the different tistributions to the data
vg1<-fit.VGuv(AAPL$Lret)
fitDF_vg <- data.frame(x = MySeq,
                       y =  dghyp(MySeq, vg1))

vg2 <- vgFit(AAPL$Lret)
fitDF_vg1 <- data.frame(x = MySeq,
                        y =  dvg(MySeq, param = vg2$param))

nig<-fit.NIGuv(AAPL$Lret)
fitDF_nig <- data.frame(x = MySeq,
                        y =  dghyp(MySeq, nig))

ghyp<-fit.ghypuv(AAPL$Lret)
fitDF_ghyp <- data.frame(x = MySeq,
                         y =  dghyp(MySeq, ghyp))

hyp<-fit.hypuv(AAPL$Lret)
fitDF_hyp <- data.frame(x = MySeq,
                        y =  dghyp(MySeq, hyp))

td<-fit.tuv(AAPL$Lret)
fitDF_td <- data.frame(x = MySeq,
                       y =  dghyp(MySeq, td))
#legend
legenda <- c("vg1"="red", "vg2"="yellow", "NIG"="green", "ghyp"="blue", "Student-t"="brown", "hip"="orange")
#ggplot
ggplot(AAPL, aes(x=Lret)) +
  geom_histogram(aes(y= stat(density)), bins = 100, color="white")+
  stat_function(fun=dnorm, args = list(mean=mean(AAPL$Lret), sd=sd(AAPL$Lret)), lwd=1, col="darkgray") +
  #fitted function plot
  geom_line(mapping = aes(x = x, y = y, col="vg1"), data = fitDF_vg, lwd=1) +
  geom_line(mapping = aes(x = x, y = y, col="vg2"), data = fitDF_vg1, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="NIG"), data = fitDF_nig, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="ghyp"), data = fitDF_ghyp, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="Student-t"), data = fitDF_td, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="hip"), data = fitDF_hyp, lwd=1)+
  xlim(-0.15,0.15)+
  scale_color_manual(values = legenda)+
  labs(color = "Legend")+
  theme(plot.title = element_text(size = 15))

I may not be understanding your problem correctly. Have you tried adding scale_y_log10() to the plotting code?

Yes i have tried but that’s the result, I’m no expert but I do not think the red marked area are correct

I think the plot is fine. All of the columns have their base at a y value of zero. That is the reasonable way to draw a column. The tick marks are labeled 1e+01, 1e-01, etc. but those values are really 1, -1, etc. on the log scale.

1 Like

well thanks! i thought that was wrong, but i was wrong lol

do you know how I can get a scatterplot because chainging the from geom_histogram to geom_point returns an error:

Error: Aesthetics must be valid computed stats. Problematic aesthetic(s): y = stat(density). 
Did you map your stat in the wrong layer? 

This is what i'm trying to achieve

ggplot(AAPL, aes(x=Lret)) +
#here i changed from geom_histogram to geom_point
  geom_point(aes(y= stat(density)), color="white")+
  stat_function(fun=dnorm, args = list(mean=mean(AAPL$Lret), sd=sd(AAPL$Lret)), lwd=1, col="darkgray") +
  #fitted function plot
  geom_line(mapping = aes(x = x, y = y, col="vg1"), data = fitDF_vg, lwd=1) +
  geom_line(mapping = aes(x = x, y = y, col="vg2"), data = fitDF_vg1, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="NIG"), data = fitDF_nig, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="ghyp"), data = fitDF_ghyp, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="Student-t"), data = fitDF_td, lwd=1)+
  geom_line(mapping = aes(x = x, y = y, col="hip"), data = fitDF_hyp, lwd=1)+
  xlim(-0.15,0.15)+
  scale_color_manual(values = legenda)+
  labs(color = "Legend")+
  theme(plot.title = element_text(size = 15))+
  scale_y_log10()

You can use the density() function to generate the points of a density histogram. There is probably a way to do this purely within ggplot but I do not know how to do that. Below is an example of using both geom_histogram and geom_point. I suppose the lack of exact agreement is due to slightly different settings for the density calculation.

library(ggplot2)
DF <- data.frame(X=rnorm(1000))

Den <- density(DF$X, n = 50)
DF2 <- data.frame(x = Den$x, y = Den$y)

ggplot()+
  geom_histogram(aes(x = X, y= stat(density)), data = DF, bins = 50, color="white") +
  geom_point(aes(x = x, y = y), data = DF2)

Created on 2021-05-29 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.