library(dplyr) library(grid) cars12 <- read.csv("data/cars12.csv") # # cars <- read.csv("cars.csv") # cars12 <- cars %>% filter(Year == 2012) # # write.csv(cars12, file = "cars12.csv") library(loon) lp1 <- with(cars12, l_plot(x = Torque, y = City.mpg, title = "2012 cars", linkingGroup = "2012 cars", itemLabel = paste0( "Name:", ID, "\n", "Driveline:", Driveline, "\n", "Height:", Height, "\n", "Width:", Width, "\n", "Length:", Length), showLabels = TRUE) ) lh1 <- with(cars12, l_hist(x = Horsepower, yshows = "density", showScales = TRUE, showLabels = TRUE, showGuides = TRUE) ) d <- density(cars12$Torque, bw = "sj", adjust = 3) l_layer.density(lh1, d, color="steelblue", linewidth=3) # add layer l_scaleto_world(lh1) # scale to world lp1["color"] <- cars12$Number.of.Forward.Gears p3d <- with(l_scale3D(cars12), l_plot3D(City.mpg, Torque, Horsepower, showGuides = TRUE, linkingGroup = "2012 cars")) Vars <- c("City.mpg", "Highway.mpg", "Height", "Length", "Width") l_pairs(cars12[, Vars], showHistograms = TRUE, linkingGroup = "2012 cars") ## Aesthetic # size lp1['size'] <- 6 # color fuelType <- as.character(unique(cars12$Fuel.Type)) cols <- c("lightblue", "steelblue", "blue") for(i in 1:length(fuelType)) { lp1['color'][cars12$Fuel.Type == fuelType[i]] <- cols[i] } # glyph lp1['glyph'][which(cars12$Classification == "Automatic transmission")] <- "ctriangle" lp1['glyph'][which(cars12$Classification == "Manual transmission")] <- "ccircle" ## Scaling # only show 'Diesel fuel cars' lp1['active'] <- lp1['color'] == l_hexcolor("blue") l_scaleto_active(lp1) lp1["title"] <- "Diesel fuel cars" # show all of them lp1["active"] <- TRUE l_scaleto_plot(lp1) lp1["title"] <- "All points again" # linking l_configure(lp1, linkingGroup = "cars12", sync = "push") l_configure(lh1, linkingGroup = "cars12", sync = "push") ##### # # ggplot # library(ggplot2) p1 <- ggplot(cars12, mapping = aes(x = Horsepower, y = City.mpg)) + geom_point() p1 p2 <- ggplot(cars12, aes(x = Torque, y = Horsepower, color = Driveline)) + geom_point() + geom_smooth() + facet_wrap(~Driveline) p2 library("loon.ggplot") lp1 <- loon.ggplot(p1) lp2 <- loon.ggplot(p2, linkingGroup = "ggplot 2012 cars") # Layers: histogram + density cars12 %>% mutate(city.lp100km = 100/(City.mpg *0.4251)) %>% ggplot(aes(x = city.lp100km, y = ..density..)) + geom_histogram(binwidth = 1, fill = "grey", color = "black") + geom_density(color = "firebrick", fill = "white") # Now using gg_pipe() and loon.ggplot() in the pipeline cars12 %>% mutate(city.lp100km = 100/(City.mpg *0.4251)) %>% gg_pipe( ggplot(aes(x = city.lp100km, y = ..density..)) + geom_histogram(binwidth = 1, fill = "grey", color = "black") + geom_density(color = "firebrick", fill = "white") ) %>% loon.ggplot() # # Active layer lowHP <- cars12$Horsepower <= 250 midHP <- cars12$Horsepower > 250 & cars12$Horsepower <= 400 highHP <- cars12$Horsepower > 400 library("RColorBrewer") cols <- brewer.pal(5, "Blues")[3:5] pLayers <- ggplot(mapping = aes(x = Horsepower, y = City.mpg, cex = 2)) + geom_point(data = cars12[lowHP,], col = cols[1]) + geom_point(data = cars12[midHP,], col = cols[2]) + geom_point(data = cars12[highHP,], col = cols[3]) + theme(legend.position = "none") pLayers lp1["title"] <- "Original: All active" carsLinkingKey <- lp1["linkingKey"] lp_lowHP <- loon.ggplot(pLayers, title = "Low HP: First group active", linkingKey = carsLinkingKey[lowHP]) lp_midHP <- loon.ggplot(pLayers, activeGeomLayers = 2, title = "Mid HP: Second group active", linkingKey = carsLinkingKey[midHP], linkingGroup = "ggplot 2012 cars") lp_highHP <- loon.ggplot(pLayers, activeGeomLayers = c(2,3), title = "Mid-High HP: Second and third groups active", linkingKey = carsLinkingKey[c(which(midHP), which(highHP))], linkingGroup = "ggplot 2012 cars") p2 <- ggplot(cars12, mapping = aes(x = Torque, y = Horsepower, color = Driveline)) + geom_point() + geom_smooth() + theme(legend.position = "none") + facet_wrap(~ Driveline) p2