Democracy Hacked.

Democracies are being gamed. Authoritarian governments, moneyed elites and fringe hackers are exploiting our digital infrastructure and the vulnerabilities in our democratic system to influence our…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Installing R and Writing a Simple R Script to Create a Graph from Excel Data

CRAN Mirrors page

When you select your region, choose the version for whichever platform you plan to install R on. R can be installed on all majorly used operating systems (Linux, Mac, and Windows). If you plan on installing R on Linux, you should check your Linux package manager as well as the versions on the CRAN website.

It’s easy to install and there isn’t much configuration required in the installation wizard. I’m downloading R on Windows but make sure you install the correct version for your system environment.

R for Windows download page
RStudio Desktop download options

Aside from the colors, this is the default RStudio screen. If you’d like to change the appearance of your RStudio, go to the “Tools” tab in the toolbar and select “Global Options…”. In the screen that pops up, select “Appearance” and change the theme to your liking. I personally like having light text on a dark background.

Default RStudio window

To select your working directory, simple type setwd(“your desired directory path”) in the console on the bottom left corner of RStudio and hit enter. In the example below, I’ve created a “R Projects” directory on my secondary drive (D:).

Alternatively, you can also set the working directory bye clicking the three dots in the bottom right corner of RStudio in the “Files” tab.

Files tab
Creating a new R script

In the top left corner of the screen there is a white box with a green circle that has a white plus sign in it, click on it to create a new R Script. This is the area where you will write your script to analyze your data of choosing.

When you select the prompt to import data set from Excel, you will then be prompt to install the readxl and Rcpp packages. Accept the installation and wait for it to complete.

After installing the package, the Import Excel Data window will pop up. First, click the “Browse…” button in the top right, or type in the path or URL to the Excel spreadsheet. Note: if you plan on using the same Excel sheet as the one mentioned above, you need to removed the superscript from the years 2001 and 2015.

If you used the same Excel sheet as the one above, it should look something like this.

The next, and final step is to select the range of data that you wish to analyze. In the bottom left corner of the window, enter “A4:V24” in the “Range” text box. This will import the whole table of data into RStudio. You may also change the name of the imported data, this will create an object in your R script under the specified name.

If you wish to import this data set every time the script is run, click the clipboard icon or copy and paste the code preview from the bottom right corner of the “Import Excel Data” window into your script.

Creating a PNG File to Represent Your Data

Now that we have our data set imported, we are going to create a very simple graph that plots each year by its respective violent crime rate. The image below is what the script to create that graph will be. I will go more into detail below the image.

First, we need to specify what type of file we are going to produce in the R script. We are going to produce a PNG file using the png() function. The png() function can take multiple arguments but we are only going to use one, being the desired file path and file name.

Now we have our desired file to output our analysis into. To specify the data we want to plot into the PNG file, we use the plot() function. The arguments we are going to use within the plot() function are; x, y, xlab, ylab, and main. x defines what we want to have on the x-axis of our graph, and y defines what to put on the y-axis. xlab and ylab are the labels that we want to put to define our x-axis and y-axis respectively. main defines the main title of the graph that is going to be plotted.

Finally, to actually create the PNG file, we type dev.off() at the bottom of the code. Now all that is left to do is run the script and you now have a PNG file that should look like the one presented below. In my next blog post, I will go over more complex ways of plotting your analysis and ways to manipulate data.

Add a comment

Related posts:

1970 Ford Torino GT Gets A Simplified Update

These days when most people talk about classic Ford muscle cars, they’re referring to the Mustang as well as the Shelby models. For far too many the Torino has been largely skipped over, forgotten…

S90.08B Exam Questions Will Improve Your Study

You could come by by far the most raised results by utilizing S90.08B exam questions material and achieve each of the Certified SOA Architect exam assembling tries without any issue. All the…

Perspective in the Era of Fake News

It is crucial to take the time to participate in discourse with the people around us who hold opposing viewpoints. The following recounts an experience of mine earlier this year. The Women’s March…