- R Data Visualization Recipes
- Vitor Bianchi Lanzetta
- 116字
- 2021-07-02 23:33:27
How to do it...
- With ggplot2, set potion = 'jitter' in order to obtain the jitter:
> set.seed(100)
> library(ggplot2)
> sca1 <- ggplot( iris, aes( x = Petal.Length, y = Petal.Width))
> sca1 + geom_point( position = 'jitter',
aes(shape = Species, colour = Species))
The jittered plot is shown by following image:

Figure 2.6 - Iris data set stacked with noise.
- Create some vectors to jitter the data set variables later:
> jx <- rnorm(length(iris$Petal.Length), mean = 0, sd = .2)
> jy <- rnorm(length(iris$Petal.Width), mean = 0, sd = .06)
- Apply these vectors when inputting x and y into the plot_ly() function, in order to achieve jitter using plotly:
> library(plotly)
> sca8 <- plot_ly(iris, x = ~Petal.Length + jx, y = ~Petal.Width + jy,
type = 'scatter', mode = 'markers', symbol = ~Species)
> sca8
- Does the same using ggvis:
> library(ggvis)
> sca3 <- ggvis( data = iris)
> sca3 %>% layer_points( x = ~Petal.Length + jx , y = ~Petal.Width + jy,
shape = ~Species, fill = ~Species)
While new vectors helped plotly and ggvis achieve jittering, all the methods avoided making any changes in the data.