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

How to do it...

  1. After loading the package, primitives geom_point() and geom_path() can be stacked in order to plot lines with markers:
> library(ggplot2)
> plot1 <- ggplot( cars, aes(x = speed, y = dist))
> plot1 + geom_point() + geom_path()

The resulting output is shown by following figure:

Figure 1.4 - Lines with markers plot made by ggplot2's primitives.

  1. Same mission can be nailed by the ggvis package, relying on the following code:
> library(ggvis)
> ggvis(cars, x = ~speed, y = ~dist) %>% layer_points() %>% layer_paths()

Following figure 1.5 displays a representation of the resulting graphic (only default theme will look different):

Figure 1.5 - Similar lines and markers plot done by ggvis.

  1. Without using the translation function (ggplotly()) from plotly package, it's also possible to code a similar graphic from scratch relying only on plotly:
> library(plotly)
> plot_ly(cars, x = ~speed, y = ~dist, type = 'scatter', mode = 'lines+markers')

Following figure 1.6 exhibits a snapshot of the graphic brewed by the latest code:

Figure 1.6 - Similar lines and markers plot done by plotly.

Let's understand how these are unfolding.