Below are the solutions to these exercises on “Data Visualization With Ggiraph.”
#################### # # # Exercise 1 # # # #################### library(ggplot2) library(ggiraph) g <- ggplot(mpg, aes( x = displ, y = cty, color = hwy) ) #################### # # # Exercise 2 # # # #################### library(ggplot2) library(ggiraph) g <- ggplot(mpg, aes( x = displ, y = cty, color = hwy) ) my_gg <- g + geom_point_interactive(aes(tooltip = model), size = 2) ggiraph(code = print(my_gg) ) #################### # # # Exercise 3 # # # #################### library(ggplot2) library(ggiraph) g <- ggplot(mpg, aes( x = displ, y = cty, color = hwy) ) my_gg <- g + geom_point_interactive( aes(tooltip = model, data_id = model), size = 2) ggiraph(code = print(my_gg), hover_css = "cursor:pointer;fill:red;stroke:red;") #################### # # # Exercise 4 # # # #################### crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) #################### # # # Exercise 5 # # # #################### crimes$onclick <- sprintf("window.open(\"%s%s\")", "http://en.wikipedia.org/wiki/", as.character(crimes$state) ) #################### # # # Exercise 6 # # # #################### crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) # create an 'onclick' column crimes$onclick <- sprintf("window.open(\"%s%s\")", "http://en.wikipedia.org/wiki/", as.character(crimes$state) ) #################### # # # Exercise 7 # # # #################### crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) # create an 'onclick' column crimes$onclick <- sprintf("window.open(\"%s%s\")", "https://en.wikipedia.org/wiki/U.S._state", as.character(crimes$state) ) #################### # # # Exercise 8 # # # #################### crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) # create an 'onclick' column crimes$onclick <- sprintf("window.open(\"%s%s\")", "http://en.wikipedia.org/wiki/", as.character(crimes$state) ) gg_crime <- ggplot(crimes, aes(x = Murder, y = Assault, color = UrbanPop )) + geom_point_interactive( aes( data_id = state, tooltip = state, onclick = onclick ), size = 3 ) + scale_colour_gradient(low = "#999999", high = "#FF3333") #################### # # # Exercise 9 # # # #################### crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) # create an 'onclick' column crimes$onclick <- sprintf("window.open(\"%s%s\")", "http://en.wikipedia.org/wiki/", as.character(crimes$state) ) gg_crime <- ggplot(crimes, aes(x = Murder, y = Assault, color = UrbanPop )) + geom_point_interactive( aes( data_id = state, tooltip = state, onclick = onclick ), size = 3 ) + scale_colour_gradient(low = "#999999", high = "#FF3333") ggiraph(code = print(gg_crime), hover_css = "fill-opacity:.3;cursor:pointer;") #################### # # # Exercise 10 # # # #################### crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests) # create an 'onclick' column crimes$onclick <- sprintf("window.open(\"%s%s\")", "http://en.wikipedia.org/wiki/", as.character(crimes$state) ) gg_crime <- ggplot(crimes, aes(x = Murder, y = Assault, color = UrbanPop )) + geom_point_interactive( aes( data_id = state, tooltip = state, onclick = onclick ), size = 3 ) + scale_colour_gradient(low = "#999999", high = "#FF3333") ggiraph(code = print(gg_crime), hover_css = "fill:orange;")
Leave a Reply