Skip to content

Instantly share code, notes, and snippets.

@klmr
Last active August 29, 2015 14:06
Show Gist options
  • Save klmr/3dfe370dd8a5ee9be506 to your computer and use it in GitHub Desktop.
Save klmr/3dfe370dd8a5ee9be506 to your computer and use it in GitHub Desktop.
Create a valplot in R
Create two bimodal toy distributions.
```{r}
a = c(rnorm(100, 5, 2), rnorm(100, 15, 3))
b = c(rnorm(100, 3, 3), rnorm(100, 14, 1))
```
Set up the graphics environment.
We draw a density curve. `polygon` draws a filled curve, but you don’t seem to be able to control the border width.
```{r}
# These values need to be fiddled with.
xlim = c(0, 20)
ylim = c(0, 0.2)
plot(0, type = 'n', xlim = xlim, ylim = ylim, bty = 'n',
axes = FALSE, ylab = '')
Axis(side = 1)
# Shift coordinate system upwards.
par(usr = c(xlim, -0.05, 0.15))
polygon(density(a), col = 'black', border = 'white')
# Shift coordinate system back downwards.
par(usr = c(xlim, ylim))
polygon(density(b), col = 'black', border = 'white')
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment