How to spatially separate carpet graphics from different series

I am trying to graphically evaluate the distribution (bimodal or unimodal) of data sets in which the number of data points in a data set can vary widely. My problem is to indicate the number of data points using something like skate plots, but to avoid the problem with a series with many data points, throw a series on just a few points.

I am currently working in ggplot2 , combining geom_density and geom_rug as follows:

 # Set up data: 1000 bimodal "b" points; 20 unimodal "a" points set.seed(0); require(ggplot2) x <- c(rnorm(500, mean=10, sd=1), rnorm(500, mean=5, sd=1), rnorm(20, mean=7, sd=1)) l <- c(rep("b", 1000), rep("a", 20)) d <- data.frame(x=x, l=l) ggplot(d, aes(x=x, colour=l)) + geom_density() + geom_rug() 

enter image description here

It almost does what I want, but points "a" are overloaded with points "b".

I cracked the solution using geom_point instead of geom_rug :

 d$ypos <- NA d$ypos[d$l=="b"] <- 0 d$ypos[d$l=="a"] <- 0.01 ggplot() + geom_density(data=d, aes(x=x, colour=l)) + geom_point(data=d, aes(x=x, y=ypos, colour=l), alpha=0.5) 

enter image description here

However, this is unsatisfactory since the y positions must be manually adjusted. Is there a more automatic way to separate graphic cards from different series, for example, by adjusting the position?

+4
source share
1 answer

One way is to use two calls to geom_rug() - one for b , the other for a . Then for one geom_rug() set sides="t" to draw them on top.

 ggplot(d, aes(x=x, colour=l)) + geom_density() + geom_rug(data=subset(d,l=="b"),aes(x=x)) + geom_rug(data=subset(d,l=="a"),aes(x=x),sides="t") 

enter image description here

+11
source

Source: https://habr.com/ru/post/1479387/


All Articles