开发者

Density plot in ggplot2 using log scale

开发者 https://www.devze.com 2023-03-12 07:29 出处:网络
I\'d like to use ggplot2 density geometry using a log transformation for the x scale: qplot(rating, data=movies, geom=\"density\", log=\"x\")

I'd like to use ggplot2 density geometry using a log transformation for the x scale:

qplot(rating, data=movies, geom="density", log="x")

This, however, produces a chart with probabilities larger than 1. O开发者_如何学JAVAne solution that seems to work is to scale the dataset before calling qplot:

qplot(rating, data=transform(movies, rating=log(rating))

But then the x axis doesn't look nice. What is the correct way to handle this?


It seems that my question doesn't not, in fact, make sense. It seems that it is OK that probability densities are larger than one, as per [2]. What is important is that the integral over the entire space is equal to one [3].


This gives the right answer.

qplot(rating, y = ..scaled.., data=movies, geom="density", log="x")

stat_density produces new values, one of them is ..scaled.. which is the density scaled from 0 to 1.

Density plot in ggplot2 using log scale

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消