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

How to do it...

  1. Initialize a ggplot and then give it the point geometry:
> library(ggplot2)
> sca1 <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width))
> sca1 + geom_point() +
ggtitle('Simple ggplot2 scatterplot')

Last lines gives the following illustration (figure 2.1):

Figure 2.1 - Simple ggplot2 scatterplot.

  1. plotly can reach very similar results:
> library(plotly)
> sca2 <- plot_ly(data = iris, x = ~Petal.Length, y = ~Petal.Width,
type = 'scatter', mode = 'markers')
> sca2 %>% layout(title = 'Simple plotly scatterplot')
  1. Using ggvis, call layer_points() to add a geometry:
> library(ggvis)
> sca3 <- iris %>% ggvis(x = ~Petal.Length, y = ~Petal.Width)
> sca3 %>% layer_points()

Ahead lay the explanations.