Skip to content

A collection of R scripts that may be useful for some people.

Notifications You must be signed in to change notification settings

RobertMyles/Various_R-Scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

Various R Scripts

In this repo are some R scripts and datasets that may be helpful. To run the scripts in R, you will need to set your working directory accordingly (to a downloaded version of this repo if you do that) or put the data somewhere you can find it easily.

List of scripts:

  • color_table_tidyverse.R. This is a tidyverse version of a handy color table (from this R script). I thought this was a useful little script, and a cool way to show what you can do in R, so I made a tidyverse version. The colors that have changing levels of hue go from left to right in terms of darkness. First comes the base colour, and then versions 1 to 4, with 4 being the darkest (the base can often be darker than 1 and 2). So if you want to use the darkest level of 'wheat' in an R plot, use 'wheat4'. A pdf is created from the script, you can see it in the data folder. It looks like this:

  • roll_call_matrix.R. This function takes vote data (in a certain format) as input and returns a matrix which can be used with MCMCpack, JAGS, or BUGS for ideal-point analyses. The function can also return a rollcall object, for use with the pscl and wnominate packages, or a vector (where NA have been deleted) for use with Stan. It can be used with the data file senate_example.csv in the data folder.

  • ideal_point_plots.R. This script contains a function that takes an MCMC summary object (from the coda package function summary()) and creates a ggplot ideal point graph from it, using either no colours, or colours determined by party or coalition membership. Since plots can be tricky things to get right to your exact taste, I recommend tweaking the function to get exactly what you want. The default (using government coalition membership to colour) looks like this:

  • ideal_point_example.R. This script shows a simple run-through of the functions above, using the senate_example data in the data folder.

  • faster_for_loops.R. People complain that for loops are slow in R. Firstly, R has many vectorized functions that do what you're probably trying to do much faster than you think, use those. If you need a for loop, and you know already the size of the desired resulting object, you can make the process faster by pre-allocating memory size (not a new trick, many have noted this before.)

  • Import&Bind_Multiple_DF.R. This function reads the files of a certain file type (in the example .csv), imports them into R and binds them together. I saw a slightly different version of this a few years ago, but I can't remember where, otherwise I'd cite the author.

  • stacked_percentage_barchart.R. Make a stacked bar chart showing percentages in different categories. Useful for showing how respondents vary over different categories. The percentages are shown inside the bars, with their position determined by the relative percentages in the category. It looks like this:

  • 538_baltimore_plot.R. This script replicates the figure from the fivethirtyeight article on black men in Baltimore, Maryland (available here). It is not the same because the data used are different, I was unable to find the exact data from the source fivethirtyeight cited. The general idea is to show how you can replicate high-quality publication-ready visualizations in R. For another example of doing this type of thing to replicate figures from The Economist, see my blog post here.

Data:

  • senate_example.csv: nominal voting example data.
  • black_2015_income.csv; population.csv; white_2015_income.csv: replication data for the fivethirtyeight plot.
  • color_chart_dplyr.pdf: colour table made from the script above.

About

A collection of R scripts that may be useful for some people.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages