R Data Visualization Recipes
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. 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.

  1. 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)
  1. 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
  1. 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.