Created
July 24, 2018 01:24
-
-
Save ghettocounselor/70d736852bba60291afef087c09bd3f1 to your computer and use it in GitHub Desktop.
google bubble chart example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # data <- readRDS("~/Data/shinny/googlebubble71820/healthexp.Rds") | |
| # data <- readRDS("/Users/markloessi/Data/shinny/googlebubble71820/healthexp.Rds") | |
| # data <- readRDS("healthexpcsv.Rds") | |
| # data <- readRDS("healthexp.Rds") | |
| convert("healthexpxlsx.xlsx", "healthexpxlsx.Rds") | |
| data <- readRDS("healthexpxlsx.Rds") | |
| head(data) | |
| # data$Region <- factor(data$Region) # less specific | |
| data$Region <- as.factor(data$Region) | |
| # if you edit the XLXS then use this to convert file to RDS | |
| # convert("healthexpxlsx.xlsx", "healthexpxlsx.Rds") | |
| # pull data in from XLSX and convert to RDS | |
| # library(rio) # need rio for import of rds and csv | |
| # convert("healthexp.Rds", "healthexpxlxs.xlsx") # one fell swoop convert to XLXS | |
| # convert("healthexpxlsx.xlsx", "healthexpxlsx.Rds") # in dir change file type | |
| # other junk | |
| # y4 <- import("healthexpcsv4.xlsx") # pull in file | |
| # export(y4, "healthexpxlsx4.Rds") # kick out as RDS | |
| # or multi step process | |
| # y <- import("healthexp.Rds") | |
| # y[,1] = NULL; to remove the 1st column | |
| # export(y, "healthexpcsv.Rds") # name whatever you want | |
| # data <- read.csv("healthexpCSV.csv") # didn't work ?? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| setwd("~/Data/shinny/googlebubble71820") | |
| library(dplyr) | |
| library(rio) | |
| shinyServer(function(input, output, session) { | |
| # Provide explicit colors for regions, so they don't get recoded when the | |
| # different series happen to be ordered differently from year to year. | |
| # http://andrewgelman.com/2014/09/11/mysterious-shiny-things/ | |
| defaultColors <- c("#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477") | |
| series <- structure( | |
| lapply(defaultColors, function(color) { list(color=color) }), | |
| names = levels(data$Region) | |
| ) | |
| yearData <- reactive({ | |
| # Filter to the desired year, and put the columns | |
| # in the order that Google's Bubble Chart expects | |
| # them (name, x, y, color, size). Also sort by region | |
| # so that Google Charts orders and colors the regions | |
| # consistently. | |
| # HOVERCARD is defined by the 1st 4 options in the list | |
| df <- data %>% | |
| filter(Year == input$year) %>% | |
| select(Country, Health.Expenditure, Life.Expectancy, | |
| Region, Population, Percent.ExpChange, Unit.ExpChange ) %>% | |
| arrange(Region) | |
| }) | |
| output$chart <- reactive({ | |
| # Return the data and options | |
| list( | |
| data = googleDataTable(yearData()), | |
| options = list( | |
| title = sprintf( | |
| "TITLE OF PLAYGROUND GRAPH", | |
| input$year), | |
| series = series | |
| ) | |
| ) | |
| }) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # More info: | |
| # https://github.com/jcheng5/googleCharts | |
| # Install: | |
| # devtools::install_github("jcheng5/googleCharts") | |
| library(googleCharts) | |
| # Use global max/min for axes so the view window stays | |
| # constant as the user moves between years | |
| xlim <- list( | |
| min = min(data$Unit.ExpChange) - 50, # Health.Expenditure | |
| max = max(data$Unit.ExpChange) + 100 # Health.Expenditure | |
| ) | |
| ylim <- list( | |
| min = min(data$Life.Expectancy), # Life.Expectancy | |
| max = max(data$Life.Expectancy) + 3 # Life.Expectancy | |
| ) | |
| shinyUI(fluidPage( | |
| # This line loads the Google Charts JS library | |
| googleChartsInit(), | |
| # Use the Google webfont "Source Sans Pro" | |
| tags$link( | |
| href=paste0("http://fonts.googleapis.com/css?", | |
| "family=Source+Sans+Pro:300,600,300italic"), | |
| rel="stylesheet", type="text/css"), | |
| tags$style(type="text/css", | |
| "body {font-family: 'Source Sans Pro'}" | |
| ), | |
| h2("Google Charts demo"), | |
| googleBubbleChart("chart", | |
| width="100%", height = "475px", | |
| # Set the default options for this chart; they can be | |
| # overridden in server.R on a per-update basis. See | |
| # https://developers.google.com/chart/interactive/docs/gallery/bubblechart | |
| # for option documentation. | |
| options = list( | |
| fontName = "Source Sans Pro", | |
| fontSize = 13, | |
| # Set axis labels and ranges | |
| hAxis = list( | |
| title = "Unit.ExpChange", # "Health expenditure, per capita ($USD)" | |
| viewWindow = xlim | |
| ), | |
| vAxis = list( | |
| title = "Life expectancy (years)", # "Life expectancy (years)" | |
| viewWindow = ylim | |
| ), | |
| # The default padding is a little too spaced out | |
| chartArea = list( | |
| top = 50, left = 75, | |
| height = "75%", width = "75%" | |
| ), | |
| # Allow pan/zoom | |
| explorer = list(), | |
| # Set bubble visual props | |
| bubble = list( | |
| opacity = 0.4, stroke = "none", | |
| # Hide bubble label | |
| textStyle = list( | |
| color = "none" | |
| ) | |
| ), | |
| # Set fonts | |
| titleTextStyle = list( | |
| fontSize = 16 | |
| ), | |
| tooltip = list( | |
| textStyle = list( | |
| fontSize = 12 | |
| ) | |
| ) | |
| ) | |
| ), | |
| fluidRow( | |
| shiny::column(4, offset = 4, | |
| sliderInput("year", "Year", | |
| min = min(data$Year), max = max(data$Year), | |
| value = min(data$Year), animate = TRUE) | |
| ) | |
| ) | |
| )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment