R Programming Language

R is a programming language and free software environment for statistical computing and graphics supported by the R Core Team and the R Foundation for Statistical Computing. It is widely used among statisticians and data miners for developing statistical software and data analysis.

Show Only ...
Maps - Photos - Videos

Setting boundaries in ggplot2

I was trying to figure out how to set the boundaries of maps created in ggplot2 to focus on a region of the state or a few counties. It’s actually not that hard to do in ggplot2 and R.

Basically you create a bounding box based on a shape like a few counties.

county <- counties(state=’ny’, cb=TRUE)

bbox <- county %>% filter(NAME %in% c(‘Oswego’,’Madison’)) %>% st_bbox()

Then add this line to the ggplot statement to set the boundaries of the plot. Simple enough.

coord_sf(xlim=c(bbox[1], bbox[3]), ylim=c(bbox[2], bbox[4])) +

I’ve been brushing up on my statistics lately, and realizing it’s kind of important to include on my graphs with linear models the linear equation, p-value and r^2-value

I’ve been brushing up on my statistics lately, and realizing it’s kind of important to include on my graphs with linear models the linear equation, p-value and r^2-value. I was hoping to find a simple R library to produce this text, but I ended up using a StackOverflow answer, along with some R code I wrote myself

get_equation <- function(model) {
  pValue <- paste('p_value=',  format(summary(model)$coefficients[,4], scientific=T), sep='')  
  r2Value <- paste('r^2_value=', round(summary(model)$r.squared,4), sep='') 
  
  formula <- broom::tidy(model)[, 1:2] %>%
    mutate(sign = ifelse(sign(estimate) == 1, '+', '-')) %>% #coeff signs
    mutate_if(is.numeric, ~ abs(round(., 7))) %>% #for improving formatting
    mutate(a = ifelse(term == '(Intercept)', paste0('y=', estimate), paste0(sign, estimate, 'x'))) %>%
    summarise(formula = paste(a, collapse = '')) %>%
    as.character 
  
  paste(formula, pValue, r2Value, sep='n')
}

To add this code to the upper right-hand side of the graph, you can use this code in ggplot2:

annotate('text', x=Inf,y=Inf, hjust=1, vjust=1, label=get_formula(lm(comb$avg ~ comb$Value))[1] ) 

Here is a rather silly example:

Jenks breaks and ggplot

Here is how to create Jenks breaks classification for maps with ggplot2 with class labels that show only the top number in the class.

classes <- classIntervals(joined$Value, n = 9, style = "fisher")

joined <- joined %>% mutate(percent_class = cut(joined$Value, breaks=classes$brks, include.lowest = T,
labels=scales::comma(classes$brks[2:length(classes$brks)]),0) )

Lately I’ve been doing more and more maps with ggplot2 and R Statistics language

Lately I’ve been doing more and more maps with ggplot2 and R Statistics language. πŸ—Ί

R is good because everything is written in the script. No clicking through windows with the join, then having to fix things in the composer. Instead, it’s just a few lines of code, which you can reuse over and over again. And usually with ggplot2, things look pretty good right out of the box — and if you are unhappy with the output, tweak a line and re-run it in R’s interactive interpreter.  Plus, you get outputted those great SVG graphics, which usually are small and look good regardless of the size of the screens.

Point and click is great, especially when you don’t have the time to learn how to program or use an API but nothing really beats the speed of a good API, backed up by code that automates things puts out quality output with minimal tweaking.