Using more suitable colors for geom_dotplot
Speaking about ggplot2
dot plots, there is a sort of a hacking solution to avoid colored points to overlay one another: manually setting up a vector of colors. However, creating such a vector wouldn't be enough, it has to be ordered properly or else a very wrong result shall be outputted. This recipe sticks with the Salaries
data set framework in order to demonstrate how dots can be colored this way.
Getting ready
If you are not sure that you have car
package installed, run the following code:
> if( !require(car)){ install.packages('car')}
Time to get hands dirty.
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 fromSalaries$sex
:
> library(car) > color_fill <- ifelse(Salaries$sex...