R- Basics
Class III

Authors
Affiliations

P K Parida

CRFM and ICAR

June Masters

CRFM

Published

August 21, 2024

1 Shortcuts in R

Today we will discuss 6 nos of shortcuts in R-studio

  • Clear the consol : ctrl/control + L both in Windows and Mac

  • Move the cursor to Consol : ctrl/control + 2 both in Windows and Mac

  • Piping operator in tidyverse : ctrl + shift + M in Windows and cmd + shift + M in case of Mac

  • assign operator in R : alt + - in Windows and option + - in Mac

  • Move cursor to beginning of the line : home in Windows and Cmd + Left arrow in Mac

  • Move the cursor to end of the line: end in Windows and cmd + Right arrow in Mac

2 How to change the the colour of the code in the source pane

We can change the colour in the source pane of R Studio by using Tools then Global Options and then Appearance then Editor theme and select the schema you are interested in, I am using Textmate.

for rainbow colour , click in the Global option , then click code , then click on display and then tick mark and then in indention guide select Rainbow fills  and in syntax, tick the rainbow parentheses like the screenshot below 

3 How to bring the data to R

Suppose your file is in excel and you want to bring the excel file to RStudio for further analysis

  1. First create a project in R-Studio by clicking file, then New Project.., then New Directory, then New Project, then browse and create a new folder test2 in mydocument folder and select the folder then create the project. Now you can see a test2.Rproj file in the test2 folder.

  2. Now copy your excel file in the test2 folder .

  3. Now click on the project file and open and open a new Rscript in the source pane by click on on file then on New File and then on R Script .

  4. Let us import the excel file form the folder.

  5. Now click on the import button on the Environment pane (2nd row and third button), and select from Excel.. , then click on browse and select the folder and the excel file, then click on open . you will find a page like below - (fig. 1)

fig -1: Importing a excel file
  • Now we change the name from the Name section, we can give any name, here we will give my_data

  • In the Sheet, default is the default sheet, generally the first sheet. However, one can change the sheet they are interested in.

  • In the Range section, one can select the range of the data, he/she interested in .

  • In the max row section, one can provide the maximum rows one is interested in.

  • In skip section, one can provide the number of rows to be skipped (like1 or 2 or 3 etc..)

  • In NA section any character to be named as NA, has to be given, for example in this data set we have 2 number of ** , so we will provide 2 stars ** in the that section to change all the starts into NA.

  • Below the code preview section, one can click to import or copy the codes and paste in the R script in the source pane.

library(tidyverse)# must call tidyverse package
Warning: package 'ggplot2' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl) # this is a package to import excel files and if the package is not installed yet, then kindly install it by - install.packages ("readxl")

my_data<- read_excel("Number of Fishers by site.xlsx",sheet = "2023", col_types = c("text", "numeric", "numeric", "numeric"))
Warning: Expecting numeric in B3 / R3C2: got '**'
Warning: Expecting numeric in D8 / R8C4: got '**'
Warning: Expecting numeric in D12 / R12C4: got '**'
summary(my_data)
 Operating SITE       Full Time        Part Time        Temporary  
 Length:17          Min.   :  4.00   Min.   : 0.000   Min.   :0.0  
 Class :character   1st Qu.:  5.50   1st Qu.: 0.000   1st Qu.:0.0  
 Mode  :character   Median : 20.00   Median : 1.500   Median :0.0  
                    Mean   : 35.87   Mean   : 2.625   Mean   :0.4  
                    3rd Qu.: 34.50   3rd Qu.: 2.000   3rd Qu.:0.5  
                    Max.   :269.00   Max.   :21.000   Max.   :3.0  
                    NA's   :2        NA's   :1        NA's   :2    

Now you can see the summary statistics of the data. it will provide the Minimum value, maximum value, 1st quartile, median , mean, 3rd quartile value and number of NAs in the data.

4 To make a bardiagram of full time fisher

my_data %>% 
  ggplot(aes(x= `Operating SITE`, y = `Full Time`)) +
  geom_col() +
  labs(y = "Full time Fishers", x = "Sites") +
  theme(axis.text = element_text(face="bold")) +
  theme(axis.text.x = element_text(angle = 90))
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_col()`).

5 How to make the graph colourful

5.1 Fill the bars with clour “steelblue”

my_data %>% 
  ggplot(aes(x= `Operating SITE`, y = `Full Time`)) +
  geom_col(fill ="steelblue") +
  labs(y = "Full time Fishers", x = "Sites") +
  theme(axis.text = element_text(face="bold")) +
  theme(axis.text.x = element_text(angle = 90))
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_col()`).

5.2 Colour the boarder of the bars

5.2.1 only boarder of bars with “steelblue” and inside the bars filled with white colour

my_data %>% 
  ggplot(aes(x= `Operating SITE`, y = `Full Time`)) +
  geom_col(colour = "steelblue", fill = "white") +
  labs(y = "Full time Fishers", x = "Sites") +
  theme(axis.text = element_text(face="bold")) +
  theme(axis.text.x = element_text(angle = 90))
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_col()`).