################################################################################ # # # ggplot2 and loon # # The loon.ggplot package # ################################################################################ # # # # ggplot2 is one of the most popular R packages # # - produces high-quality presentation graphics # - uses a pipeline style to build visualizations # - based on a variation on the original grammar of graphics (hence the gg) # - part of the tidyverse (with magrittr, dplyr, purrr, ...) # # # Packages used here # library(loon) library(loon.data) library(ggplot2) library(magrittr) library(dplyr) library(loon.ggplot) ######## # # # ggplot2 # # # ggplots are built up from a series of components # that can sometimes be thought of as computational parts, # pIris <- ggplot(data = iris, mapping = aes(x = Sepal.Width, y = Sepal.Length, col = Species)) + geom_point(size = 5) # # which is not displayed until printed. pIris # # You can add to it later # pIris + ggtitle("Iris data") # It has some natural layers for statistics # pIris + geom_density2d() # # Histograms # hIris <- ggplot(data = iris, mapping = aes(x = Sepal.Width, col = Species, fill = Species)) + geom_histogram(binwidth = 0.5) hIris # # One of its most useful features is facetting pIris + geom_density2d() + facet_wrap(vars(Species)) # # It can get complicated # # SAheart from Hastie and Tibshirani "Elements of Statistical Learning" # Here from loon.data # data("SAheart") pSAheart <- ggplot(data = SAheart, mapping = aes(group = famhist, col = famhist)) + geom_point(mapping = aes(x = age, y = chd)) + geom_smooth(mapping = aes(x = age, y = chd)) + xlab("Age in years") + ylab("Heart disease") pSAheart # # It is integral to the tidyverse and works # nicely with pipes # # Here the 1974 Motor Trends car data mtcars %>% rename(transmission = am, weight = wt) %>% mutate(lp100km = (100 * 3.785411784) / (1.609344 * mpg)) %>% select(weight, lp100km) %>% ggplot(aes(x = weight, y = lp100km)) + geom_point(size = 5, col = "steelblue") + geom_smooth(method='lm', formula= y~x) + ylab("Litres per 100 kilometres") + ggtitle("Gas usage") -> # Note assignment occurs here pMTcars pMTcars ######## # # loon.ggplot # # # It would be nice if these were interactive plots though. # # That's what loon.ggplot() function is for. # l_pIris <- loon.ggplot(pIris, linkingGroup = "iris") l_hIris <- loon.ggplot(hIris, linkingGroup = "iris") l_pSAheart <- loon.ggplot(pSAheart, size = 10, linkingGroup = "SAheart") l_pMTcars <- loon.ggplot(pMTcars, itemLabel = row.names(mtcars), color = mtcars$am, showItemLabels = TRUE, linkingGroup = "mtcars") l_pIrisDens <- loon.ggplot(pIris + geom_density2d(), linkingGroup = "iris") # And to grid # plot(l_pIrisDens) # # facets l_pIrisDens <- loon.ggplot(pIris + geom_density2d() + facet_wrap(vars(Species)), linkingGroup = "iris") # # loon.ggplot() is bi-directional # p1 <- with(mtcars, l_plot(mpg, qsec, itemLabel = row.names(mtcars), showItemLabels = TRUE, linkingGroup = "mtcars") ) p1["selected"] <- mtcars$am == 1 loon.ggplot(p1) loon.ggplot(p1) + geom_density2d() loon.ggplot(loon.ggplot(p1) + geom_density2d(), glyph = "ctriangle") # Which makes identifying which pointa are highlighted difficult! loon.ggplot(l_facet(p1, by = mtcars$am)) ######## # # gg_pipe() # # It would be nice to just add the loon conversion # to a pipeline. # # Unfortunately this fails. # loon.ggplot() needs a REALIZED ggplot to transform to loon # NOT the PROMISE of one. # ggplots are only a promise until they are printed. # # That's the job of gg_pipe. # # For example mtcars %>% rename(transmission = am, weight = wt) %>% mutate(lp100km = (100 * 3.785411784) / (1.609344 * mpg)) %>% select(weight, lp100km) %>% # Have to wrap the ggplot in gg_pipe # which forces the ggplot to be realized. gg_pipe( { ggplot(aes(x = weight, y = lp100km)) + geom_point(size = 5, col = "steelblue") + geom_smooth(method='lm', formula= y~x) + ylab("Litres per 100 kilometres") + ggtitle("Gas usage") } ) %>% loon.ggplot(linkingGroup = "mtcars") -> l_pMTcars2 # # Which admittedly seems less than ideal. # ######## # # l_ggplot() # # Wouldn't be nicer if we could just use the # grammar of ggplot2 without an actual ggplot? # # This seems like a fast way to get an interactive # plot to play with. # # Simply use l_ggplot() in place of ggplot() # mtcars %>% rename(transmission = am, weight = wt) %>% mutate(lp100km = (100 * 3.785411784) / (1.609344 * mpg)) %>% select(weight, lp100km) %>% # use l_ggplot() in place of ggplot() l_ggplot(aes(x = weight, y = lp100km)) + geom_point(size = 5, col = "steelblue") + geom_smooth(method='lm', formula= y~x) + ylab("Litres per 100 kilometres") + ggtitle("Gas usage") -> l_pMTcars3 # which, just like ggplot, produces no plot until it is printed l_pMTcars3 # # To which we could add stuff # l_pMTcars3 + geom_density2d() # # ######## # # NOTE # # loon.ggplot is still being polished and further fleshed out. # # There is lots more and in some cases some attention needs # to be paid to multiple data layers and linking. # # And more to come. # # And then there is loon.shiny ... another time. # ################################################################################