Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram with a jittery rug

The following script

library(ggplot2)
dat<-rnorm(80)
dat<-data.frame(dat)
p<-ggplot(dat, aes(x=dat))+geom_histogram()
p<-p+geom_rug(sides="b", colour="blue")
p

makes this pretty picture:

Histogram with a rug

But many of those blue lines overlap. I'd like to add some jitter! I've tried using:

p<-p+geom_rug(sides="b", position="jitter", colour="blue")

But I'm given an error message:

stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this. Error: position_jitter requires the following missing aesthetics: y

The y coordinate for a histogram is the count, which the histogram should make automagically.

How can I get my jitters?

like image 739
Richard Avatar asked Sep 08 '15 23:09

Richard


People also ask

What is rug in histogram?

A rug plot is a plot of data for a single quantitative variable, displayed as marks along an axis. It is used to visualise the distribution of the data. As such it is analogous to a histogram with zero-width bins, or a one-dimensional scatter plot.

What does rug function do in R?

ticksize. The length of the ticks making up the 'rug'. Positive lengths give inwards ticks. side. On which side of the plot box the rug will be plotted.

What is Geom_rug?

geom_rug.Rd. A rug plot is a compact visualisation designed to supplement a 2d display with the two 1d marginal distributions. Rug plots display individual cases so are best used with smaller datasets.

What is rug plot in Python?

MatplotlibPythonData Visualization. Rug plots are used to visualize the distribution of data. It is a plot of data for a single variable, displayed as marks along an axis. To make a rug plot in Matplotlib, we can take the following steps − Set the figure size and adjust the padding between and around the subplots.


1 Answers

You can simply give a y of 0 in an aes call, and it will plot everything ok:

p + geom_rug(sides = "b", aes(y = 0), position = "jitter", colour = "blue")

using some more obvious data:

dat <- c(rep(1, 50), rep(2, 50))
dat <- data.frame(dat)

without jitter:

enter image description here

With jitter:

enter image description here

like image 154
jeremycg Avatar answered Oct 13 '22 00:10

jeremycg