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

How to do it...

  1. Change the shape and colour arguments to get a better result:
> library(ggplot2)
> sca1 <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width))
> sca1 + geom_point(aes(shape = Species, colour = Species))

Now each iris species is designated by a unique combination of shapes and colors:

Figure 2.3 - Adding shapes and colors to a scatter plot.

  1. plotly can also handle such a task:
> library(plotly)
> sca4 <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width,
type = 'scatter', mode = 'markers', symbol = ~Species)
> sca4
  1. Following code changes shapes and colors using ggvis:
> library(ggvis)
> sca3 <- ggvis(data = iris, x = ~Petal.Length, y = ~Petal.Width)
> sca3 >%> layer_points(shape = ~Species, fill = ~Species)

Explanations can be found on the next section.