Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

r - How to plot stacked point histograms?

What's the ggplot2 equivalent of "dotplot" histograms? With stacked points instead of bars? Similar to this solution in R:

Plot Histogram with Points Instead of Bars

Is it possible to do this in ggplot2? Ideally with the points shown as stacks and a faint line showing the smoothed line "fit" to these points (which would make a histogram shape.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ggplot2 does dotplots Link to the manual.

Here is an example:

library(ggplot2)

set.seed(789); x <- data.frame(y = sample(1:20, 100, replace = TRUE))

ggplot(x, aes(y)) + geom_dotplot()

In order to make it behave like a simple dotplot, we should do this:

ggplot(x, aes(y)) + geom_dotplot(binwidth=1, method='histodot')    

You should get this:

first plot

To address the density issue, you'll have to add another term, ylim(), so that your plot call will have the form ggplot() + geom_dotplot() + ylim()

More specifically, you'll write ylim(0, A), where A will be the number of stacked dots necessary to count 1.00 density. In the example above, the best you can do is see that 7.5 dots reach the 0.50 density mark. From there, you can infer that 15 dots will reach 1.00.

So your new call looks like this:

ggplot(x, aes(y)) + geom_dotplot(binwidth=1, method='histodot') + ylim(0, 15)

Which will give you this:

second plot

Usually, this kind of eyeball estimate will work for dotplots, but of course you can try other values to fine-tune your scale.

Notice how changing the ylim values doesn't affect how the data is displayed, it just changes the labels in the y-axis.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...