
上QQ阅读APP看书,第一时间看更新
How to do it...
Following steps demonstrates an alternative way of setting colors with geom_dotplot():
- Pick the colors to fill the dots and store them into objects:
> color1 <- 'deepskyblue1'
> color2 <- 'darkred'
- Create and reorder a vector with colors representing the 'Male' and 'Female' values coming from Salaries$sex:
> library(car)
> color_fill <- ifelse(Salaries$sex == 'Male',color1,color2)
> color_fill <- color_fill[with(Salaries,order(rank,salary))]
- Assign color_fill to the fill parameter in order to properly color the dots:
> library(ggplot2)
> dot1 <- ggplot(Salaries, aes( x = rank, y = salary))
> dot1 + geom_dotplot(binaxis = 'y', dotsize = .32,
stackdir = 'center',colour = color_fill,
fill = color_fill)
Following illustration (Figure 3.5) demonstrates that colors are not overlaying one another anymore:

Figure 3.5: Another color solution.
Let's try to understand this code.