├── paw.png ├── MaleCat.png ├── FemaleCat.png ├── sCATterplot.Rmd ├── s-cat-terplot.png └── README.md /paw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/blog_scatterplot/master/paw.png -------------------------------------------------------------------------------- /MaleCat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/blog_scatterplot/master/MaleCat.png -------------------------------------------------------------------------------- /FemaleCat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/blog_scatterplot/master/FemaleCat.png -------------------------------------------------------------------------------- /sCATterplot.Rmd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/blog_scatterplot/master/sCATterplot.Rmd -------------------------------------------------------------------------------- /s-cat-terplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/blog_scatterplot/master/s-cat-terplot.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sCATterplot 2 | Clara Schartner, Data Scientist 3 | 4 | 5 | 6 | It will come as no surprise that cats and ggplot are among our favourite things here at mango. And there is an easy way to combine both: 7 | 8 | ![](s-cat-terplot.png) 9 | 10 | Using the function `annotation_custom` of the popular ggplot2 package it is possible to display images on a plot as points of a scatterplot. This way data can be displayed in a more fun, creative way. 11 | In keeping with the cat theme I have chosen a data set about cats and a cat icon based on Mango the cat. The `MASS` package provides a data set called cats which contains the body weight, heart weight and sex of adult cats. 12 | 13 | 14 | However since the data has 144 observations only a subset is used for the plot. 15 | 16 | 17 | ```r 18 | library(MASS) 19 | data(cats) 20 | head(cats) 21 | set.seed(1234) 22 | cats <- cats[sample(1:144, size = 40),] 23 | ``` 24 | 25 | First a normal scatterplot is defined on which the images will be plotted later: 26 | 27 | 28 | ```r 29 | library(ggplot2) 30 | sCATter <-ggplot(data = cats, aes(x = Bwt, y = Hwt)) + 31 | geom_point(size = 0, aes(group = Sex, colour = Sex)) + 32 | theme_classic() + 33 | xlab("Body weight") + 34 | ylab("Heart weight") + 35 | ggtitle("sCATterplot") + 36 | theme(plot.title = element_text(hjust = 0.5)) + 37 | # create a legend 38 | scale_color_manual( 39 | values = c("#999999", "#b35900" ), 40 | name = "Cat", 41 | labels = c("Male cat", "Female cat") 42 | ) + 43 | guides(colour = guide_legend(override.aes = list(size = 10))) 44 | ``` 45 | 46 | Any png image can be used for the plot, however images with a transparent background are preferable. 47 | 48 | 49 | ```r 50 | library(png) 51 | library(grid) 52 | mCat <- readPNG("MaleCat.png") 53 | feCat<- readPNG("FemaleCat.png") 54 | ``` 55 | 56 | In the last step the cats are iteratively plotted onto the plot using `annotation_custom`. 57 | 58 | 59 | ```r 60 | for (i in 1:nrow(cats)) { 61 | # distinguishing the sex of the cat 62 | if (cats$Sex[i] == "F") { 63 | image <- feCat 64 | } else{ 65 | image <- mCat 66 | } 67 | sCATter = sCATter + 68 | annotation_custom( 69 | rasterGrob(image), 70 | xmin = cats$Bwt[i] - 0.6, 71 | xmax = cats$Bwt[i] + 0.6, 72 | ymin = cats$Hwt[i] - 0.6, 73 | ymax = cats$Hwt[i] + 0.6 74 | ) 75 | } 76 | ``` 77 | 78 | 79 | 80 | The cat´s paw trail is displaying a linear regression of heart on body weight. This can easily be added by computing a linear Regression, defining a grid to calculate the expected values and plotting cats on top of this data. 81 | 82 | 83 | ```r 84 | LmCat <- lm(Hwt~Bwt, data = cats) 85 | 86 | steps <- 20 87 | Reg <- data.frame(Bwt = 88 | seq(from = min(cats$Bwt), 89 | to = max(cats$Bwt), 90 | length.out = steps)) 91 | Reg$Hwt <- predict(LmCat, newdata = Reg) 92 | ``` 93 | 94 | 95 | ```r 96 | sCATter <- sCATter + 97 | geom_point(data = Reg, aes(Bwt, Hwt), size = 0) 98 | 99 | paw <- readPNG("paw.png") 100 | for (i in 1:nrow(Reg)) { 101 | sCATter = sCATter + 102 | annotation_custom( 103 | rasterGrob(paw), 104 | xmin = Reg$Bwt[i] - 0.6, 105 | xmax = Reg$Bwt[i] + 0.6, 106 | ymin = Reg$Hwt[i] - 0.6, 107 | ymax = Reg$Hwt[i] + 0.6 108 | ) 109 | } 110 | sCATter 111 | ``` 112 | --------------------------------------------------------------------------------