diff --git a/awesome_plot.png b/awesome_plot.png index c1d900f..d6fee3f 100644 Binary files a/awesome_plot.png and b/awesome_plot.png differ diff --git a/ch1_d.qmd b/ch1_d.qmd index a42b8cb..75e3303 100644 --- a/ch1_d.qmd +++ b/ch1_d.qmd @@ -59,3 +59,5 @@ Exercise: 1. Taking examples of any two USCRN stations across contrasting hydroclimates, compare and contrast any two recorded variables using time series plots, probability density distribution histograms and scatter plots. Select any year of your liking for the analysis. 2. Select two seasons for each elected variable and demonstrate the seasonal variability in the records for summer (MAMJJA) and winter (SONDJF) seasons using any two types of multivariate plots. + +3. \[EXTRA\]: For any chosen station, plot a time-series of soil moisture from all available layers with precipitation added as an inverted secondary axis. For inspiration, see Figure 4 in C*heng, et al. 2021. On change of soil moisture distribution with vegetation reconstruction in Mu Us sandy land of China, with newly designed lysimeter. Frontiers in Plant Science, 12, p.60952* at https://www.frontiersin.org/articles/10.3389/fpls.2021.609529/full diff --git a/docs/ch1.html b/docs/ch1.html index 1e59e09..c58c1ac 100644 --- a/docs/ch1.html +++ b/docs/ch1.html @@ -472,13 +472,13 @@

out[[2]] = data2 out[[1]] # Contains data1 at this location
-
 [1] 22.38551 24.93971 29.15338 26.53533 26.24764 29.15248 23.72007 28.29892
- [9] 23.29838 29.08426 22.02442 29.26941 27.68071 28.98836 28.34313 24.94511
-[17] 29.83468 26.48164 20.77258 29.79567 21.95838 27.25046 23.91091 20.62865
-[25] 22.87702 28.63880 29.07499 28.89268 21.30982 22.38500 26.12874 27.12475
-[33] 27.51490 22.06456 29.66929 23.34676 23.31295 26.03315 28.73698 21.87959
-[41] 23.33600 28.43788 28.80145 26.51936 22.79800 22.74102 24.00127 28.94563
-[49] 21.23255 25.34127
+
 [1] 27.54519 28.15198 26.94921 27.09713 24.83331 28.36166 29.98894 26.06856
+ [9] 27.07806 25.31515 24.46197 22.17432 29.66485 27.97959 29.03084 20.52997
+[17] 27.96831 28.90613 24.82964 25.20565 26.47424 20.04347 28.65534 21.74832
+[25] 21.55563 22.52076 23.84974 28.61952 26.31761 24.93412 25.72561 22.36199
+[33] 23.89496 28.57787 22.92200 26.27299 28.13778 25.86501 28.39688 22.37600
+[41] 26.70197 24.08437 28.94931 26.62575 26.85836 24.59127 29.43989 20.09708
+[49] 22.73285 24.40913
# Data frame
 out=data.frame(x=data1, y=data2)
@@ -831,12 +831,12 @@ 

# A tibble: 6 × 4 Year January Month Value <dbl> <dbl> <chr> <dbl> -1 1913 50.7 Jan -13.6 -2 1913 50.7 Feb -8.48 -3 1913 50.7 Mar -11.4 -4 1914 49.6 Jan -17.4 -5 1914 49.6 Feb -14.5 -6 1914 49.6 Mar -13.2

+1 1913 38.6 Jan -13.4 +2 1913 38.6 Feb -2.79 +3 1913 38.6 Mar -2.06 +4 1914 14.5 Jan -11.0 +5 1914 14.5 Feb -7.28 +6 1914 14.5 Mar -10.9

Line plot

@@ -1224,6 +1224,7 @@

  • Taking examples of any two USCRN stations across contrasting hydroclimates, compare and contrast any two recorded variables using time series plots, probability density distribution histograms and scatter plots. Select any year of your liking for the analysis.

  • Select two seasons for each elected variable and demonstrate the seasonal variability in the records for summer (MAMJJA) and winter (SONDJF) seasons using any two types of multivariate plots.

  • +
  • [EXTRA]: For any chosen station, plot a time-series of soil moisture from all available layers with precipitation added as an inverted secondary axis. For inspiration, see Figure 4 in Cheng, et al. 2021. On change of soil moisture distribution with vegetation reconstruction in Mu Us sandy land of China, with newly designed lysimeter. Frontiers in Plant Science, 12, p.60952 at https://www.frontiersin.org/articles/10.3389/fpls.2021.609529/full

  • diff --git a/docs/ch1_files/figure-html/Data types-1.png b/docs/ch1_files/figure-html/Data types-1.png index a404375..c4a4f96 100644 Binary files a/docs/ch1_files/figure-html/Data types-1.png and b/docs/ch1_files/figure-html/Data types-1.png differ diff --git a/docs/ch1_files/figure-html/Data types-2.png b/docs/ch1_files/figure-html/Data types-2.png index 2ec2276..7e668b8 100644 Binary files a/docs/ch1_files/figure-html/Data types-2.png and b/docs/ch1_files/figure-html/Data types-2.png differ diff --git a/docs/ch1_files/figure-html/Multivariate data frame-1.png b/docs/ch1_files/figure-html/Multivariate data frame-1.png index f3d62b0..758078e 100644 Binary files a/docs/ch1_files/figure-html/Multivariate data frame-1.png and b/docs/ch1_files/figure-html/Multivariate data frame-1.png differ diff --git a/docs/ch1_files/figure-html/Multivariate data frame-2.png b/docs/ch1_files/figure-html/Multivariate data frame-2.png index 66b4621..1faa858 100644 Binary files a/docs/ch1_files/figure-html/Multivariate data frame-2.png and b/docs/ch1_files/figure-html/Multivariate data frame-2.png differ diff --git a/docs/ch1_files/figure-html/Multivariate data frame-3.png b/docs/ch1_files/figure-html/Multivariate data frame-3.png index a63f1d7..cfa36dd 100644 Binary files a/docs/ch1_files/figure-html/Multivariate data frame-3.png and b/docs/ch1_files/figure-html/Multivariate data frame-3.png differ diff --git a/docs/ch1_files/figure-html/Multivariate data frame-4.png b/docs/ch1_files/figure-html/Multivariate data frame-4.png index 2cf8506..9faf853 100644 Binary files a/docs/ch1_files/figure-html/Multivariate data frame-4.png and b/docs/ch1_files/figure-html/Multivariate data frame-4.png differ diff --git a/docs/ch1_files/figure-html/Univariate data-1.png b/docs/ch1_files/figure-html/Univariate data-1.png index efc96b8..62da97c 100644 Binary files a/docs/ch1_files/figure-html/Univariate data-1.png and b/docs/ch1_files/figure-html/Univariate data-1.png differ diff --git a/docs/ch1_files/figure-html/Univariate data-2.png b/docs/ch1_files/figure-html/Univariate data-2.png index 96fef53..0f99559 100644 Binary files a/docs/ch1_files/figure-html/Univariate data-2.png and b/docs/ch1_files/figure-html/Univariate data-2.png differ diff --git a/docs/ch1_files/figure-html/data frame and list-1.png b/docs/ch1_files/figure-html/data frame and list-1.png index 2eca694..e7ac5d1 100644 Binary files a/docs/ch1_files/figure-html/data frame and list-1.png and b/docs/ch1_files/figure-html/data frame and list-1.png differ diff --git a/docs/ch1_files/figure-html/data frame and list-2.png b/docs/ch1_files/figure-html/data frame and list-2.png index 5f4ce49..dea8fa2 100644 Binary files a/docs/ch1_files/figure-html/data frame and list-2.png and b/docs/ch1_files/figure-html/data frame and list-2.png differ diff --git a/docs/ch1_files/figure-html/data frame and list-3.png b/docs/ch1_files/figure-html/data frame and list-3.png index d9249e5..35493ae 100644 Binary files a/docs/ch1_files/figure-html/data frame and list-3.png and b/docs/ch1_files/figure-html/data frame and list-3.png differ diff --git a/docs/ch1_files/figure-html/data frame and list-4.png b/docs/ch1_files/figure-html/data frame and list-4.png index 1680ada..872ee76 100644 Binary files a/docs/ch1_files/figure-html/data frame and list-4.png and b/docs/ch1_files/figure-html/data frame and list-4.png differ diff --git a/docs/ch1_files/figure-html/data frame and list-5.png b/docs/ch1_files/figure-html/data frame and list-5.png index 25e7c39..89d677e 100644 Binary files a/docs/ch1_files/figure-html/data frame and list-5.png and b/docs/ch1_files/figure-html/data frame and list-5.png differ diff --git a/docs/ch1_files/figure-html/data frame and list-6.png b/docs/ch1_files/figure-html/data frame and list-6.png index 5a0a717..7524d7b 100644 Binary files a/docs/ch1_files/figure-html/data frame and list-6.png and b/docs/ch1_files/figure-html/data frame and list-6.png differ diff --git a/docs/ch1_files/figure-html/density plot-1.png b/docs/ch1_files/figure-html/density plot-1.png index cb223ce..1ae3663 100644 Binary files a/docs/ch1_files/figure-html/density plot-1.png and b/docs/ch1_files/figure-html/density plot-1.png differ diff --git a/docs/ch1_files/figure-html/grid plotting and saving 1-1.png b/docs/ch1_files/figure-html/grid plotting and saving 1-1.png index 0d57d35..5faf8d8 100644 Binary files a/docs/ch1_files/figure-html/grid plotting and saving 1-1.png and b/docs/ch1_files/figure-html/grid plotting and saving 1-1.png differ diff --git a/docs/ch1_files/figure-html/histogram-1.png b/docs/ch1_files/figure-html/histogram-1.png index 524f831..d63247c 100644 Binary files a/docs/ch1_files/figure-html/histogram-1.png and b/docs/ch1_files/figure-html/histogram-1.png differ diff --git a/docs/ch1_files/figure-html/line plot-1.png b/docs/ch1_files/figure-html/line plot-1.png index 84ca4d4..45e9f68 100644 Binary files a/docs/ch1_files/figure-html/line plot-1.png and b/docs/ch1_files/figure-html/line plot-1.png differ diff --git a/docs/ch1_files/figure-html/load packages and data for plots-1.png b/docs/ch1_files/figure-html/load packages and data for plots-1.png index fc3badd..9397991 100644 Binary files a/docs/ch1_files/figure-html/load packages and data for plots-1.png and b/docs/ch1_files/figure-html/load packages and data for plots-1.png differ diff --git a/docs/ch1_files/figure-html/load packages and data for plots-2.png b/docs/ch1_files/figure-html/load packages and data for plots-2.png index cc52c8e..0873968 100644 Binary files a/docs/ch1_files/figure-html/load packages and data for plots-2.png and b/docs/ch1_files/figure-html/load packages and data for plots-2.png differ diff --git a/docs/ch1_files/figure-html/time series-1.png b/docs/ch1_files/figure-html/time series-1.png index fec9957..89b94a4 100644 Binary files a/docs/ch1_files/figure-html/time series-1.png and b/docs/ch1_files/figure-html/time series-1.png differ diff --git a/docs/ch1_files/figure-html/unnamed-chunk-19-1.png b/docs/ch1_files/figure-html/unnamed-chunk-19-1.png index bd02563..5bcc584 100644 Binary files a/docs/ch1_files/figure-html/unnamed-chunk-19-1.png and b/docs/ch1_files/figure-html/unnamed-chunk-19-1.png differ diff --git a/docs/ch1_files/figure-html/unnamed-chunk-19-2.png b/docs/ch1_files/figure-html/unnamed-chunk-19-2.png index ab424fc..98ffc0f 100644 Binary files a/docs/ch1_files/figure-html/unnamed-chunk-19-2.png and b/docs/ch1_files/figure-html/unnamed-chunk-19-2.png differ diff --git a/docs/ch1_files/figure-html/unnamed-chunk-19-3.png b/docs/ch1_files/figure-html/unnamed-chunk-19-3.png index e155bb9..301d293 100644 Binary files a/docs/ch1_files/figure-html/unnamed-chunk-19-3.png and b/docs/ch1_files/figure-html/unnamed-chunk-19-3.png differ diff --git a/docs/ch1_files/figure-html/using layout-1.png b/docs/ch1_files/figure-html/using layout-1.png index d64c8bf..31a821f 100644 Binary files a/docs/ch1_files/figure-html/using layout-1.png and b/docs/ch1_files/figure-html/using layout-1.png differ diff --git a/docs/ch1_files/figure-html/using layout-2.png b/docs/ch1_files/figure-html/using layout-2.png index 6331685..c50d39b 100644 Binary files a/docs/ch1_files/figure-html/using layout-2.png and b/docs/ch1_files/figure-html/using layout-2.png differ diff --git a/docs/search.json b/docs/search.json index 26e446e..efa7fb1 100644 --- a/docs/search.json +++ b/docs/search.json @@ -11,7 +11,7 @@ "href": "ch1.html#operators-and-data-types", "title": "2  Basics of R", "section": "2.1 Operators and data types", - "text": "2.1 Operators and data types\n\n2.1.1 Basic operators\nIn this section, we will learn about some basic R operators that are used to perform operations on variables. Some most commonly used operators are shown in the table below.\n\n\n\n\nR follows the conventional order (sequence) to solve mathematical operations, abbreviated as BODMAS: Brackets, Orders (exponents), Division, Multiplication, Addition, and Subtraction\n\n\n2+4+7 # Sum\n\n[1] 13\n\n4-5 # Subtraction\n\n[1] -1\n\n2*3 # Multiplication\n\n[1] 6\n\n1/2 # Division\n\n[1] 0.5\n\n# Order of operation\n1/2*3+4-5\n\n[1] 0.5\n\n1/2*(3+4-5)\n\n[1] 1\n\n1/(2*(3+4-5))\n\n[1] 0.25\n\n1/(2*3+4-5) \n\n[1] 0.2\n\n# Notice how output changes with the placement of operators\n\n# Other operators:\n2^3\n\n[1] 8\n\nlog(10)\n\n[1] 2.302585\n\nsqrt(4)\n\n[1] 2\n\npi\n\n[1] 3.141593\n\n# Clear the Environment\nrm(list=ls()) # rm is for remove,ls is short for list. The empty parenthesis i.e. () signifies all content. \n\n\n\n2.1.2 Basic data operations\nIn this section, we will create some vector data and apply built-in operations to examine the properties of a dataset.\n\n# The \"is equal to\" or \"assignment operator in R is \"<-\" or \"=\" \n\n# Generate sample data. Remember \"c\" comes from for \"concatenate\". \ndata<-c(1,4,2,3,9) # Try data = c(1,4,2,3,9). Is there any difference in data in both cases?\n\n# rbind combines data by rows, and hence \"r\"bind\n# cbind combines data by columns, and hence \"c\"bind\n\n# Checking the properties of a dataset. Note: the na.rm argument ignores NA values in the dataset.\ndata=rbind(1,4,2,3,9) \ndim(data) # [5,1]: 5 rows, 1 column\n\n[1] 5 1\n\ndata[2,1] # Show the value in row 2, column 1\n\n[1] 4\n\ndata[c(2:5),1] # Show a range of values in column 1\n\n[1] 4 2 3 9\n\nmean(data, na.rm=T) # Mean\n\n[1] 3.8\n\nmax(data) # Maximum\n\n[1] 9\n\nmin(data) # Minimum\n\n[1] 1\n\nsd(data) # Standard deviation\n\n[1] 3.114482\n\nvar(data) # Variance\n\n [,1]\n[1,] 9.7\n\nsummary(data) \n\n V1 \n Min. :1.0 \n 1st Qu.:2.0 \n Median :3.0 \n Mean :3.8 \n 3rd Qu.:4.0 \n Max. :9.0 \n\nstr(data) # Prints structure of data\n\n num [1:5, 1] 1 4 2 3 9\n\nhead(data) # Returns the 1st 6 items in the object\n\n [,1]\n[1,] 1\n[2,] 4\n[3,] 2\n[4,] 3\n[5,] 9\n\nhead(data, 2) # Print first 2\n\n [,1]\n[1,] 1\n[2,] 4\n\ntail(data, 2) # Print last 2\n\n [,1]\n[4,] 3\n[5,] 9\n\n# Do the same, but with \"c()\" instead of \"rbind\"\ndata=c(1,4,2,3,9) \ndim(data) # Note: dim is NULL\n\nNULL\n\nlength(data) # Length of a dataset is the number of variables (columns)\n\n[1] 5\n\ndata[2] # This should give you 4 \n\n[1] 4\n\n# Other operators work in the same way\nmean(data) # Mean\n\n[1] 3.8\n\nmax(data) # Maximum\n\n[1] 9\n\nmin(data) # Minimum\n\n[1] 1\n\nsd(data) # Standard deviation\n\n[1] 3.114482\n\nvar(data) # Variance\n\n[1] 9.7\n\n# Text data\ndata=c(\"LSU\",\"SPESS\",\"AgCenter\",\"Tigers\") \ndata # View\n\n[1] \"LSU\" \"SPESS\" \"AgCenter\" \"Tigers\" \n\ndata[1]\n\n[1] \"LSU\"\n\n# Mixed data\ndata=c(1,\"LSU\",10,\"AgCenter\") # All data is treated as text if one value is text\ndata[3] # Note how output is in quotes i.e. \"10\"\n\n[1] \"10\"\n\n\n\nFor help with a function in R, just type ? followed by the function to display information in the help menu. Try pasting ?sd in the console.\n\n\n\n2.1.3 Data types\nIn R, data is stored as an “array”, which can be 1-dimensional or 2-dimensional. A 1-D array is called a “vector” and a 2-D array is a “matrix”. A table in R is called a “data frame” and a “list” is a container to hold a variety of data types. In this section, we will learn how to create matrices, lists and data frames in R.\n\n\n\n\n# Lets make a random matrix\ntest_mat = matrix( c(2, 4, 3, 1, 5, 7), # The data elements \n nrow=2, # Number of rows \n ncol=3, # Number of columns \n byrow = TRUE) # Fill matrix by rows \n\ntest_mat = matrix( c(2, 4, 3, 1, 5, 7),nrow=2,ncol=3,byrow = TRUE) # Same result \ntest_mat\n\n [,1] [,2] [,3]\n[1,] 2 4 3\n[2,] 1 5 7\n\ntest_mat[,2] # Display all rows, and second column\n\n[1] 4 5\n\ntest_mat[2,] # Display second row, all columns\n\n[1] 1 5 7\n\n# Types of datasets\nout = as.matrix(test_mat)\nout # This is a matrix\n\n [,1] [,2] [,3]\n[1,] 2 4 3\n[2,] 1 5 7\n\nout = as.array(test_mat)\nout # This is also a matrix\n\n [,1] [,2] [,3]\n[1,] 2 4 3\n[2,] 1 5 7\n\nout = as.vector(test_mat)\nout # This is just a vector\n\n[1] 2 1 4 5 3 7\n\n# Data frame and list\ndata1=runif(50,20,30) # Create 50 random numbers between 20 and 30 \ndata2=runif(50,0,10) # Create 50 random numbers between 0 and 10 \n\n# Lists\nout = list() # Create and empty list\nout[[1]] = data1 # Notice the brackets \"[[ ]]\" instead of \"[ ]\"\nout[[2]] = data2\nout[[1]] # Contains data1 at this location\n\n [1] 22.38551 24.93971 29.15338 26.53533 26.24764 29.15248 23.72007 28.29892\n [9] 23.29838 29.08426 22.02442 29.26941 27.68071 28.98836 28.34313 24.94511\n[17] 29.83468 26.48164 20.77258 29.79567 21.95838 27.25046 23.91091 20.62865\n[25] 22.87702 28.63880 29.07499 28.89268 21.30982 22.38500 26.12874 27.12475\n[33] 27.51490 22.06456 29.66929 23.34676 23.31295 26.03315 28.73698 21.87959\n[41] 23.33600 28.43788 28.80145 26.51936 22.79800 22.74102 24.00127 28.94563\n[49] 21.23255 25.34127\n\n# Data frame\nout=data.frame(x=data1, y=data2)\n\n# Let's see how it looks!\nplot(out$x, out$y)\n\n\n\nplot(out[,1])\n\n\n\n\n\nFor a data frame, the dollar “$” sign invokes the variable selection. Imagine how one would receive merchandise in a store if you give $ to the cashier. Data frame will list out the variable names for you of you when you show it some $." + "text": "2.1 Operators and data types\n\n2.1.1 Basic operators\nIn this section, we will learn about some basic R operators that are used to perform operations on variables. Some most commonly used operators are shown in the table below.\n\n\n\n\nR follows the conventional order (sequence) to solve mathematical operations, abbreviated as BODMAS: Brackets, Orders (exponents), Division, Multiplication, Addition, and Subtraction\n\n\n2+4+7 # Sum\n\n[1] 13\n\n4-5 # Subtraction\n\n[1] -1\n\n2*3 # Multiplication\n\n[1] 6\n\n1/2 # Division\n\n[1] 0.5\n\n# Order of operation\n1/2*3+4-5\n\n[1] 0.5\n\n1/2*(3+4-5)\n\n[1] 1\n\n1/(2*(3+4-5))\n\n[1] 0.25\n\n1/(2*3+4-5) \n\n[1] 0.2\n\n# Notice how output changes with the placement of operators\n\n# Other operators:\n2^3\n\n[1] 8\n\nlog(10)\n\n[1] 2.302585\n\nsqrt(4)\n\n[1] 2\n\npi\n\n[1] 3.141593\n\n# Clear the Environment\nrm(list=ls()) # rm is for remove,ls is short for list. The empty parenthesis i.e. () signifies all content. \n\n\n\n2.1.2 Basic data operations\nIn this section, we will create some vector data and apply built-in operations to examine the properties of a dataset.\n\n# The \"is equal to\" or \"assignment operator in R is \"<-\" or \"=\" \n\n# Generate sample data. Remember \"c\" comes from for \"concatenate\". \ndata<-c(1,4,2,3,9) # Try data = c(1,4,2,3,9). Is there any difference in data in both cases?\n\n# rbind combines data by rows, and hence \"r\"bind\n# cbind combines data by columns, and hence \"c\"bind\n\n# Checking the properties of a dataset. Note: the na.rm argument ignores NA values in the dataset.\ndata=rbind(1,4,2,3,9) \ndim(data) # [5,1]: 5 rows, 1 column\n\n[1] 5 1\n\ndata[2,1] # Show the value in row 2, column 1\n\n[1] 4\n\ndata[c(2:5),1] # Show a range of values in column 1\n\n[1] 4 2 3 9\n\nmean(data, na.rm=T) # Mean\n\n[1] 3.8\n\nmax(data) # Maximum\n\n[1] 9\n\nmin(data) # Minimum\n\n[1] 1\n\nsd(data) # Standard deviation\n\n[1] 3.114482\n\nvar(data) # Variance\n\n [,1]\n[1,] 9.7\n\nsummary(data) \n\n V1 \n Min. :1.0 \n 1st Qu.:2.0 \n Median :3.0 \n Mean :3.8 \n 3rd Qu.:4.0 \n Max. :9.0 \n\nstr(data) # Prints structure of data\n\n num [1:5, 1] 1 4 2 3 9\n\nhead(data) # Returns the 1st 6 items in the object\n\n [,1]\n[1,] 1\n[2,] 4\n[3,] 2\n[4,] 3\n[5,] 9\n\nhead(data, 2) # Print first 2\n\n [,1]\n[1,] 1\n[2,] 4\n\ntail(data, 2) # Print last 2\n\n [,1]\n[4,] 3\n[5,] 9\n\n# Do the same, but with \"c()\" instead of \"rbind\"\ndata=c(1,4,2,3,9) \ndim(data) # Note: dim is NULL\n\nNULL\n\nlength(data) # Length of a dataset is the number of variables (columns)\n\n[1] 5\n\ndata[2] # This should give you 4 \n\n[1] 4\n\n# Other operators work in the same way\nmean(data) # Mean\n\n[1] 3.8\n\nmax(data) # Maximum\n\n[1] 9\n\nmin(data) # Minimum\n\n[1] 1\n\nsd(data) # Standard deviation\n\n[1] 3.114482\n\nvar(data) # Variance\n\n[1] 9.7\n\n# Text data\ndata=c(\"LSU\",\"SPESS\",\"AgCenter\",\"Tigers\") \ndata # View\n\n[1] \"LSU\" \"SPESS\" \"AgCenter\" \"Tigers\" \n\ndata[1]\n\n[1] \"LSU\"\n\n# Mixed data\ndata=c(1,\"LSU\",10,\"AgCenter\") # All data is treated as text if one value is text\ndata[3] # Note how output is in quotes i.e. \"10\"\n\n[1] \"10\"\n\n\n\nFor help with a function in R, just type ? followed by the function to display information in the help menu. Try pasting ?sd in the console.\n\n\n\n2.1.3 Data types\nIn R, data is stored as an “array”, which can be 1-dimensional or 2-dimensional. A 1-D array is called a “vector” and a 2-D array is a “matrix”. A table in R is called a “data frame” and a “list” is a container to hold a variety of data types. In this section, we will learn how to create matrices, lists and data frames in R.\n\n\n\n\n# Lets make a random matrix\ntest_mat = matrix( c(2, 4, 3, 1, 5, 7), # The data elements \n nrow=2, # Number of rows \n ncol=3, # Number of columns \n byrow = TRUE) # Fill matrix by rows \n\ntest_mat = matrix( c(2, 4, 3, 1, 5, 7),nrow=2,ncol=3,byrow = TRUE) # Same result \ntest_mat\n\n [,1] [,2] [,3]\n[1,] 2 4 3\n[2,] 1 5 7\n\ntest_mat[,2] # Display all rows, and second column\n\n[1] 4 5\n\ntest_mat[2,] # Display second row, all columns\n\n[1] 1 5 7\n\n# Types of datasets\nout = as.matrix(test_mat)\nout # This is a matrix\n\n [,1] [,2] [,3]\n[1,] 2 4 3\n[2,] 1 5 7\n\nout = as.array(test_mat)\nout # This is also a matrix\n\n [,1] [,2] [,3]\n[1,] 2 4 3\n[2,] 1 5 7\n\nout = as.vector(test_mat)\nout # This is just a vector\n\n[1] 2 1 4 5 3 7\n\n# Data frame and list\ndata1=runif(50,20,30) # Create 50 random numbers between 20 and 30 \ndata2=runif(50,0,10) # Create 50 random numbers between 0 and 10 \n\n# Lists\nout = list() # Create and empty list\nout[[1]] = data1 # Notice the brackets \"[[ ]]\" instead of \"[ ]\"\nout[[2]] = data2\nout[[1]] # Contains data1 at this location\n\n [1] 27.54519 28.15198 26.94921 27.09713 24.83331 28.36166 29.98894 26.06856\n [9] 27.07806 25.31515 24.46197 22.17432 29.66485 27.97959 29.03084 20.52997\n[17] 27.96831 28.90613 24.82964 25.20565 26.47424 20.04347 28.65534 21.74832\n[25] 21.55563 22.52076 23.84974 28.61952 26.31761 24.93412 25.72561 22.36199\n[33] 23.89496 28.57787 22.92200 26.27299 28.13778 25.86501 28.39688 22.37600\n[41] 26.70197 24.08437 28.94931 26.62575 26.85836 24.59127 29.43989 20.09708\n[49] 22.73285 24.40913\n\n# Data frame\nout=data.frame(x=data1, y=data2)\n\n# Let's see how it looks!\nplot(out$x, out$y)\n\n\n\nplot(out[,1])\n\n\n\n\n\nFor a data frame, the dollar “$” sign invokes the variable selection. Imagine how one would receive merchandise in a store if you give $ to the cashier. Data frame will list out the variable names for you of you when you show it some $." }, { "objectID": "ch1.html#plotting-with-base-r", @@ -25,13 +25,13 @@ "href": "ch1.html#plotting-with-ggplot2", "title": "2  Basics of R", "section": "2.3 Plotting with ggplot2", - "text": "2.3 Plotting with ggplot2\n\n2.3.1 Import libraries and create sample dataset\nFor this section, we will use the ggplot2, gridExtra, utils, and tidyr packages. gridExtra and cowplot are used to combine ggplot objects into one plot and utils and tidyr are useful for manipulating and reshaping the data. We will also install some packages here that will be required for the later sections. You will find more information in the sections to follow.\n\n###############################################################\n#~~~ Load required libraries\nlib_names=c(\"ggplot2\",\"gridExtra\",\"utils\",\"tidyr\",\"cowplot\", \"RColorBrewer\")\n\n# If you see a prompt: Do you want to restart R prior to installing: Select **No**. \n\n# Install all necessary packages (Run once)\n# invisible(suppressMessages\n# (suppressWarnings\n# (lapply\n# (lib_names,install.packages,repos=\"http://cran.r-project.org\",\n# character.only = T))))\n\n# Load necessary packages\ninvisible(suppressMessages\n (suppressWarnings\n (lapply\n (lib_names,library, character.only = T))))\n\nIn more day-to-day use, you will see yourself using a simpler version of these commands, such as, if you were to install the “ggplot2”,“gridExtra” libraries, you will type:\n\n# To install the package. Install only once\ninstall.packages(\"ggplot2\")\n# To initialize the package. Invoke every time a new session begins.\nlibrary(ggplot2)\n\nSimilarly, again for gridExtra ,\n\ninstall.packages(\"gridExtra\")\nlibrary(gridExtra)\n\nFor this exercise, let us generate a sample dataset.\n\n###############################################################\n#~~~ Generate a dataset containing random numbers within specified ranges\nYear = seq(1913,2001,1)\nJan = runif(89, -18.4, -3.2)\nFeb = runif(89, -19.4, -1.2)\nMar = runif(89, -14, -1.8)\nJanuary = runif(89, 1, 86)\ndat = data.frame(Year, Jan, Feb, Mar, January)\n\n\n\n2.3.2 Basics of ggplot\nWhereas base R has an “ink on paper” plotting paradigm, ggplot has a “grammar of graphics” paradigm that packages together a variety plotting functions. With ggplot, you assign the result of a function to an object name and then modify it by adding additional functions. Think of it as adding layers using pre-designed functions rather than having to build those functions yourself, as you would have to do with base R.\n\nl1 = ggplot(data=dat, aes(x = Year, y = Jan, color = \"blue\")) + # Tell which data to plot\n geom_line() + # Add a line\n geom_point() + # Add a points\n xlab(\"Year\") + # Add labels to the axes\n ylab(\"Value\")\n\n# Or, they can be specified for any individual geometry\nl1 + geom_line(linetype = \"solid\", color=\"Blue\") # Add a solid line\n\n\n\nl1 + geom_line(aes(x = Year, y = January)) # Add a different data set\n\n\n\n# There are tons of other built-in color scales and themes, such as scale_color_grey(), scale_color_brewer(), theme_classic(), theme_minimal(), and theme_dark()\n\n# OR, CREATE YOUR OWN THEME! You can group themes together in one list\ntheme1 = theme(\n legend.position = \"none\",\n panel.background = element_blank(),\n plot.title = element_text(hjust = 0.5),\n axis.line = element_line(color = \"black\"),\n axis.text.y = element_text(size = 11),\n axis.text.x = element_text(size = 11),\n axis.title.y = element_text(size = 11),\n axis.title.x = element_text(size = 11),\n panel.border = element_rect(\n colour = \"black\",\n fill = NA,\n size = 0.5\n )\n)\n\n\n\n2.3.3 Multivariate plots\nFor multivariate data, ggplot takes the data in the form of groups. This means that each data row should be identifiable to a group. To get the most out of ggplot, we will need to reshape our dataset.\n\nlibrary(tidyr)\n\n# There are two generally data formats: wide (horizontal) and long (vertical). In the horizontal format, every column represents a category of the data. In the vertical format, every row represents an observation for a particular category (think of each row as a data point). Both formats have their comparative advantages. We will now convert the data frame we randomly generated in the previous section to the long format. Here are several ways to do this:\n\n# Using the gather function (the operator %>% is called pipe operator)\ndat2 = dat %>% gather(Month, Value, -Year)\n\n# This is equivalent to: \ndat2 = gather(data=dat, Month, Value, -Year)\n\n# Using pivot_longer and selecting all of the columns we want. This function is the best!\ndat2 = dat %>% pivot_longer(cols = c(Jan, Feb, Mar), names_to = \"Month\", values_to = \"Value\") \n\n# Or we can choose to exclude the columns we don't want\ndat2 = dat %>% pivot_longer(cols = -c(Year,January), names_to = \"Month\", values_to = \"Value\") \n\nhead(dat2) # The data is now shaped in the long format\n\n# A tibble: 6 × 4\n Year January Month Value\n <dbl> <dbl> <chr> <dbl>\n1 1913 50.7 Jan -13.6 \n2 1913 50.7 Feb -8.48\n3 1913 50.7 Mar -11.4 \n4 1914 49.6 Jan -17.4 \n5 1914 49.6 Feb -14.5 \n6 1914 49.6 Mar -13.2 \n\n\nLine plot\n\n# LINE PLOT\nl = ggplot(dat2, aes(x = Year, y = Value, group = Month)) +\n geom_line(aes(color = Month)) +\n geom_point(aes(color = Month))\nl\n\n\n\n\nDensity plot\n\n# DENSITY PLOT\nd = ggplot(dat2, aes(x = Value))\nd = d + geom_density(aes(color = Month, fill = Month), alpha=0.4) # Alpha specifies transparency\nd\n\n\n\n\nHistogram\n\n# HISTOGRAM\nh = ggplot(dat2, aes(x = Value))\nh = h + geom_histogram(aes(color = Month, fill = Month), alpha=0.4,\n fill = \"white\",\n position = \"dodge\")\nh\n\n\n\n\nGrid plotting and saving files to disk\nThere are multiple ways to arrange multiple plots and save images. One method is using grid.arrange() which is found in the gridExtra package. You can then save the file using ggsave, which comes with the ggplot2 library.\n\n# The plots can be displayed together on one image using \n# grid.arrange from the gridExtra package\nimg = grid.arrange(l, d, h, nrow=3)\n\n\n\n# Finally, plots created using ggplot can be saved using ggsave\nggsave(\"grid_plot_1.png\", \n plot = img, \n device = \"png\", \n width = 6, \n height = 4, \n units = c(\"in\"), \n dpi = 600)\n\nAnother approach is to use the plot_grid function, which is in the cowplot library. Notice how the axes are now beautifally aligned.\n\nimg2=cowplot::plot_grid(l, d, h, nrow = 3, align = \"v\") # \"v\" aligns vertical axes and \"h\" aligns horizontal axes\n\nggsave(\"grid_plot_2.png\", \n plot = img2, \n device = \"png\", \n width = 6, \n height = 4, \n units = c(\"in\"), \n dpi = 600)\n\n\n\n2.3.4 Using patchwork for combining ggplots\nPatchwork works with simple operators to combine plots. The operator | arranges plots in a row. The plus sign + does the same but it will try to wrap the plots symmetrically as a square whenever possible. The division i.e. /operator layers a plot on top of another.\n\n#install.packages(\"patchwork\")\nlibrary(patchwork)\n\nl+d\n\n\n\nl/ (h+d)\n\n\n\n# Try: l/d/h or (l+d)/h \n\n# Make your own design for arranging plots (the # sign means empty space): \ndesign <- \"\n 111\n 2#3\n\"\nl + d + h + plot_layout(design = design)\n\n\n\n\n\nSome useful resources\nThe links below offer a treasure trove of examples and sample code to get you started.\n\nThe R Graph Gallery: https://www.r-graph-gallery.com/\nR charts: https://r-charts.com/\nExcellent resource for combining multiple ggplots: https://r-charts.com/ggplot2/combining-plots/" + "text": "2.3 Plotting with ggplot2\n\n2.3.1 Import libraries and create sample dataset\nFor this section, we will use the ggplot2, gridExtra, utils, and tidyr packages. gridExtra and cowplot are used to combine ggplot objects into one plot and utils and tidyr are useful for manipulating and reshaping the data. We will also install some packages here that will be required for the later sections. You will find more information in the sections to follow.\n\n###############################################################\n#~~~ Load required libraries\nlib_names=c(\"ggplot2\",\"gridExtra\",\"utils\",\"tidyr\",\"cowplot\", \"RColorBrewer\")\n\n# If you see a prompt: Do you want to restart R prior to installing: Select **No**. \n\n# Install all necessary packages (Run once)\n# invisible(suppressMessages\n# (suppressWarnings\n# (lapply\n# (lib_names,install.packages,repos=\"http://cran.r-project.org\",\n# character.only = T))))\n\n# Load necessary packages\ninvisible(suppressMessages\n (suppressWarnings\n (lapply\n (lib_names,library, character.only = T))))\n\nIn more day-to-day use, you will see yourself using a simpler version of these commands, such as, if you were to install the “ggplot2”,“gridExtra” libraries, you will type:\n\n# To install the package. Install only once\ninstall.packages(\"ggplot2\")\n# To initialize the package. Invoke every time a new session begins.\nlibrary(ggplot2)\n\nSimilarly, again for gridExtra ,\n\ninstall.packages(\"gridExtra\")\nlibrary(gridExtra)\n\nFor this exercise, let us generate a sample dataset.\n\n###############################################################\n#~~~ Generate a dataset containing random numbers within specified ranges\nYear = seq(1913,2001,1)\nJan = runif(89, -18.4, -3.2)\nFeb = runif(89, -19.4, -1.2)\nMar = runif(89, -14, -1.8)\nJanuary = runif(89, 1, 86)\ndat = data.frame(Year, Jan, Feb, Mar, January)\n\n\n\n2.3.2 Basics of ggplot\nWhereas base R has an “ink on paper” plotting paradigm, ggplot has a “grammar of graphics” paradigm that packages together a variety plotting functions. With ggplot, you assign the result of a function to an object name and then modify it by adding additional functions. Think of it as adding layers using pre-designed functions rather than having to build those functions yourself, as you would have to do with base R.\n\nl1 = ggplot(data=dat, aes(x = Year, y = Jan, color = \"blue\")) + # Tell which data to plot\n geom_line() + # Add a line\n geom_point() + # Add a points\n xlab(\"Year\") + # Add labels to the axes\n ylab(\"Value\")\n\n# Or, they can be specified for any individual geometry\nl1 + geom_line(linetype = \"solid\", color=\"Blue\") # Add a solid line\n\n\n\nl1 + geom_line(aes(x = Year, y = January)) # Add a different data set\n\n\n\n# There are tons of other built-in color scales and themes, such as scale_color_grey(), scale_color_brewer(), theme_classic(), theme_minimal(), and theme_dark()\n\n# OR, CREATE YOUR OWN THEME! You can group themes together in one list\ntheme1 = theme(\n legend.position = \"none\",\n panel.background = element_blank(),\n plot.title = element_text(hjust = 0.5),\n axis.line = element_line(color = \"black\"),\n axis.text.y = element_text(size = 11),\n axis.text.x = element_text(size = 11),\n axis.title.y = element_text(size = 11),\n axis.title.x = element_text(size = 11),\n panel.border = element_rect(\n colour = \"black\",\n fill = NA,\n size = 0.5\n )\n)\n\n\n\n2.3.3 Multivariate plots\nFor multivariate data, ggplot takes the data in the form of groups. This means that each data row should be identifiable to a group. To get the most out of ggplot, we will need to reshape our dataset.\n\nlibrary(tidyr)\n\n# There are two generally data formats: wide (horizontal) and long (vertical). In the horizontal format, every column represents a category of the data. In the vertical format, every row represents an observation for a particular category (think of each row as a data point). Both formats have their comparative advantages. We will now convert the data frame we randomly generated in the previous section to the long format. Here are several ways to do this:\n\n# Using the gather function (the operator %>% is called pipe operator)\ndat2 = dat %>% gather(Month, Value, -Year)\n\n# This is equivalent to: \ndat2 = gather(data=dat, Month, Value, -Year)\n\n# Using pivot_longer and selecting all of the columns we want. This function is the best!\ndat2 = dat %>% pivot_longer(cols = c(Jan, Feb, Mar), names_to = \"Month\", values_to = \"Value\") \n\n# Or we can choose to exclude the columns we don't want\ndat2 = dat %>% pivot_longer(cols = -c(Year,January), names_to = \"Month\", values_to = \"Value\") \n\nhead(dat2) # The data is now shaped in the long format\n\n# A tibble: 6 × 4\n Year January Month Value\n <dbl> <dbl> <chr> <dbl>\n1 1913 38.6 Jan -13.4 \n2 1913 38.6 Feb -2.79\n3 1913 38.6 Mar -2.06\n4 1914 14.5 Jan -11.0 \n5 1914 14.5 Feb -7.28\n6 1914 14.5 Mar -10.9 \n\n\nLine plot\n\n# LINE PLOT\nl = ggplot(dat2, aes(x = Year, y = Value, group = Month)) +\n geom_line(aes(color = Month)) +\n geom_point(aes(color = Month))\nl\n\n\n\n\nDensity plot\n\n# DENSITY PLOT\nd = ggplot(dat2, aes(x = Value))\nd = d + geom_density(aes(color = Month, fill = Month), alpha=0.4) # Alpha specifies transparency\nd\n\n\n\n\nHistogram\n\n# HISTOGRAM\nh = ggplot(dat2, aes(x = Value))\nh = h + geom_histogram(aes(color = Month, fill = Month), alpha=0.4,\n fill = \"white\",\n position = \"dodge\")\nh\n\n\n\n\nGrid plotting and saving files to disk\nThere are multiple ways to arrange multiple plots and save images. One method is using grid.arrange() which is found in the gridExtra package. You can then save the file using ggsave, which comes with the ggplot2 library.\n\n# The plots can be displayed together on one image using \n# grid.arrange from the gridExtra package\nimg = grid.arrange(l, d, h, nrow=3)\n\n\n\n# Finally, plots created using ggplot can be saved using ggsave\nggsave(\"grid_plot_1.png\", \n plot = img, \n device = \"png\", \n width = 6, \n height = 4, \n units = c(\"in\"), \n dpi = 600)\n\nAnother approach is to use the plot_grid function, which is in the cowplot library. Notice how the axes are now beautifally aligned.\n\nimg2=cowplot::plot_grid(l, d, h, nrow = 3, align = \"v\") # \"v\" aligns vertical axes and \"h\" aligns horizontal axes\n\nggsave(\"grid_plot_2.png\", \n plot = img2, \n device = \"png\", \n width = 6, \n height = 4, \n units = c(\"in\"), \n dpi = 600)\n\n\n\n2.3.4 Using patchwork for combining ggplots\nPatchwork works with simple operators to combine plots. The operator | arranges plots in a row. The plus sign + does the same but it will try to wrap the plots symmetrically as a square whenever possible. The division i.e. /operator layers a plot on top of another.\n\n#install.packages(\"patchwork\")\nlibrary(patchwork)\n\nl+d\n\n\n\nl/ (h+d)\n\n\n\n# Try: l/d/h or (l+d)/h \n\n# Make your own design for arranging plots (the # sign means empty space): \ndesign <- \"\n 111\n 2#3\n\"\nl + d + h + plot_layout(design = design)\n\n\n\n\n\nSome useful resources\nThe links below offer a treasure trove of examples and sample code to get you started.\n\nThe R Graph Gallery: https://www.r-graph-gallery.com/\nR charts: https://r-charts.com/\nExcellent resource for combining multiple ggplots: https://r-charts.com/ggplot2/combining-plots/" }, { "objectID": "ch1.html#exercise-1", "href": "ch1.html#exercise-1", "title": "2  Basics of R", "section": "2.4 Exercise #1", - "text": "2.4 Exercise #1\nThe U.S. Climate Reference Network (USCRN) is a systematic and sustained network of climate monitoring stations. USCRN has sites across Contiguous U.S. along with some in Alaska, and Hawaii. These stations are instrumented to measure meteorological information such as temperature, precipitation, wind speed, along with other relevant hydrologic variables such as soil moisture at uniform depths (5, 10, 20, 50, 100 cm) at sub-hourly, daily and monthly time scales. Users can access daily data set from all station suing the following link: Index of /pub/data/uscrn/products/daily01 (noaa.gov)\nLet us extract sample data from a USCRN site in Lafayette, LA, USA for 2021.\n\n# Yearly data from the sample station\nCRNdat = read.csv(url(\"https://www.ncei.noaa.gov/pub/data/uscrn/products/daily01/2021/CRND0103-2021-LA_Lafayette_13_SE.txt\"), header=FALSE,sep=\"\")\n\n# Data headers\nheaders=read.csv(url(\"https://www.ncei.noaa.gov/pub/data/uscrn/products/daily01/headers.txt\"), header=FALSE,sep=\"\")\n\n# Column names as headers from the text file\ncolnames(CRNdat)=headers[2,1:ncol(CRNdat)]\n\n# Replace fill values with NA\nCRNdat[CRNdat == -9999]=NA\nCRNdat[CRNdat == -99]=NA\nCRNdat[CRNdat == 999]=NA\n\n# View data sample\nlibrary(kableExtra)\ndataTable = kbl(head(CRNdat,6),full_width = F)\nkable_styling(dataTable,bootstrap_options = c(\"striped\", \"hover\", \"condensed\", \"responsive\"))\n\n\n\n\n\nWBANNO\nLST_DATE\nCRX_VN\nLONGITUDE\nLATITUDE\nT_DAILY_MAX\nT_DAILY_MIN\nT_DAILY_MEAN\nT_DAILY_AVG\nP_DAILY_CALC\nSOLARAD_DAILY\nSUR_TEMP_DAILY_TYPE\nSUR_TEMP_DAILY_MAX\nSUR_TEMP_DAILY_MIN\nSUR_TEMP_DAILY_AVG\nRH_DAILY_MAX\nRH_DAILY_MIN\nRH_DAILY_AVG\nSOIL_MOISTURE_5_DAILY\nSOIL_MOISTURE_10_DAILY\nSOIL_MOISTURE_20_DAILY\nSOIL_MOISTURE_50_DAILY\nSOIL_MOISTURE_100_DAILY\nSOIL_TEMP_5_DAILY\nSOIL_TEMP_10_DAILY\nSOIL_TEMP_20_DAILY\nSOIL_TEMP_50_DAILY\nSOIL_TEMP_100_DAILY\n\n\n\n\n53960\n20210101\n2.622\n-91.87\n30.09\n14.0\n5.2\n9.6\n11.0\n0.0\n12.16\nC\n18.7\n4.6\n11.6\n92.8\n49.3\n72.0\n0.401\n0.372\n0.380\n0.405\n0.381\n16.2\n15.3\n15.5\n15.7\n15.5\n\n\n53960\n20210102\n2.622\n-91.87\n30.09\n10.4\n1.9\n6.1\n6.5\n0.0\n8.95\nC\n15.3\n0.4\n7.3\n98.6\n61.6\n78.4\n0.396\n0.370\n0.377\n0.406\n0.376\n14.4\n13.3\n14.1\n15.2\n15.0\n\n\n53960\n20210103\n2.622\n-91.87\n30.09\n16.3\n-0.1\n8.1\n7.9\n0.0\n13.93\nC\n24.3\n-0.9\n9.5\n100.0\n42.1\n76.3\n0.392\n0.368\n0.374\n0.404\n0.373\n12.8\n11.8\n12.8\n14.4\n14.2\n\n\n53960\n20210104\n2.622\n-91.87\n30.09\n22.2\n3.7\n12.9\n12.5\n0.0\n11.56\nC\n26.4\n2.6\n13.2\n98.9\n47.7\n80.2\n0.389\n0.366\n0.370\n0.400\n0.372\n13.0\n12.2\n12.7\n14.0\n14.0\n\n\n53960\n20210105\n2.622\n-91.87\n30.09\n20.7\n4.5\n12.6\n11.4\n0.0\n14.37\nC\n28.9\n3.1\n13.3\n100.0\n27.7\n71.0\n0.388\n0.364\n0.368\n0.399\n0.369\n13.0\n12.1\n12.7\n13.9\n14.0\n\n\n53960\n20210106\n2.622\n-91.87\n30.09\n19.4\n4.9\n12.2\n12.6\n20.7\n9.79\nC\n23.1\n3.5\n12.8\n98.5\n54.7\n78.9\n0.390\n0.363\n0.369\n0.399\n0.370\n12.8\n12.1\n12.5\n13.7\n13.7\n\n\n\n\n\n\n\n\nNotice the variables provided in the dataset. As an example, we can plots soil moisture data from a depth of 20 cm for this station for our reference:\n\n# Sample plot for soil moisture\nx=CRNdat$SOIL_MOISTURE_20_DAILY\n\n# Plot time series and density distribution \nplot(x, type=\"l\", ylab=\"Soil moisture (v/v)\", \n col=\"cyan4\", lwd=3)\nplot(density(na.omit(x)), main=\" \", xlab=\"\", \n col=\"cyan4\", lwd=3)\n\n\n\n\n\n\n\n(a) Time series of SM\n\n\n\n\n\n\n\n(b) SM kernel density\n\n\n\n\nFigure 2.1: Soil moisture values at the selected USCRN station\n\n\n\nExercise:\n\nTaking examples of any two USCRN stations across contrasting hydroclimates, compare and contrast any two recorded variables using time series plots, probability density distribution histograms and scatter plots. Select any year of your liking for the analysis.\nSelect two seasons for each elected variable and demonstrate the seasonal variability in the records for summer (MAMJJA) and winter (SONDJF) seasons using any two types of multivariate plots." + "text": "2.4 Exercise #1\nThe U.S. Climate Reference Network (USCRN) is a systematic and sustained network of climate monitoring stations. USCRN has sites across Contiguous U.S. along with some in Alaska, and Hawaii. These stations are instrumented to measure meteorological information such as temperature, precipitation, wind speed, along with other relevant hydrologic variables such as soil moisture at uniform depths (5, 10, 20, 50, 100 cm) at sub-hourly, daily and monthly time scales. Users can access daily data set from all station suing the following link: Index of /pub/data/uscrn/products/daily01 (noaa.gov)\nLet us extract sample data from a USCRN site in Lafayette, LA, USA for 2021.\n\n# Yearly data from the sample station\nCRNdat = read.csv(url(\"https://www.ncei.noaa.gov/pub/data/uscrn/products/daily01/2021/CRND0103-2021-LA_Lafayette_13_SE.txt\"), header=FALSE,sep=\"\")\n\n# Data headers\nheaders=read.csv(url(\"https://www.ncei.noaa.gov/pub/data/uscrn/products/daily01/headers.txt\"), header=FALSE,sep=\"\")\n\n# Column names as headers from the text file\ncolnames(CRNdat)=headers[2,1:ncol(CRNdat)]\n\n# Replace fill values with NA\nCRNdat[CRNdat == -9999]=NA\nCRNdat[CRNdat == -99]=NA\nCRNdat[CRNdat == 999]=NA\n\n# View data sample\nlibrary(kableExtra)\ndataTable = kbl(head(CRNdat,6),full_width = F)\nkable_styling(dataTable,bootstrap_options = c(\"striped\", \"hover\", \"condensed\", \"responsive\"))\n\n\n\n\n\nWBANNO\nLST_DATE\nCRX_VN\nLONGITUDE\nLATITUDE\nT_DAILY_MAX\nT_DAILY_MIN\nT_DAILY_MEAN\nT_DAILY_AVG\nP_DAILY_CALC\nSOLARAD_DAILY\nSUR_TEMP_DAILY_TYPE\nSUR_TEMP_DAILY_MAX\nSUR_TEMP_DAILY_MIN\nSUR_TEMP_DAILY_AVG\nRH_DAILY_MAX\nRH_DAILY_MIN\nRH_DAILY_AVG\nSOIL_MOISTURE_5_DAILY\nSOIL_MOISTURE_10_DAILY\nSOIL_MOISTURE_20_DAILY\nSOIL_MOISTURE_50_DAILY\nSOIL_MOISTURE_100_DAILY\nSOIL_TEMP_5_DAILY\nSOIL_TEMP_10_DAILY\nSOIL_TEMP_20_DAILY\nSOIL_TEMP_50_DAILY\nSOIL_TEMP_100_DAILY\n\n\n\n\n53960\n20210101\n2.622\n-91.87\n30.09\n14.0\n5.2\n9.6\n11.0\n0.0\n12.16\nC\n18.7\n4.6\n11.6\n92.8\n49.3\n72.0\n0.401\n0.372\n0.380\n0.405\n0.381\n16.2\n15.3\n15.5\n15.7\n15.5\n\n\n53960\n20210102\n2.622\n-91.87\n30.09\n10.4\n1.9\n6.1\n6.5\n0.0\n8.95\nC\n15.3\n0.4\n7.3\n98.6\n61.6\n78.4\n0.396\n0.370\n0.377\n0.406\n0.376\n14.4\n13.3\n14.1\n15.2\n15.0\n\n\n53960\n20210103\n2.622\n-91.87\n30.09\n16.3\n-0.1\n8.1\n7.9\n0.0\n13.93\nC\n24.3\n-0.9\n9.5\n100.0\n42.1\n76.3\n0.392\n0.368\n0.374\n0.404\n0.373\n12.8\n11.8\n12.8\n14.4\n14.2\n\n\n53960\n20210104\n2.622\n-91.87\n30.09\n22.2\n3.7\n12.9\n12.5\n0.0\n11.56\nC\n26.4\n2.6\n13.2\n98.9\n47.7\n80.2\n0.389\n0.366\n0.370\n0.400\n0.372\n13.0\n12.2\n12.7\n14.0\n14.0\n\n\n53960\n20210105\n2.622\n-91.87\n30.09\n20.7\n4.5\n12.6\n11.4\n0.0\n14.37\nC\n28.9\n3.1\n13.3\n100.0\n27.7\n71.0\n0.388\n0.364\n0.368\n0.399\n0.369\n13.0\n12.1\n12.7\n13.9\n14.0\n\n\n53960\n20210106\n2.622\n-91.87\n30.09\n19.4\n4.9\n12.2\n12.6\n20.7\n9.79\nC\n23.1\n3.5\n12.8\n98.5\n54.7\n78.9\n0.390\n0.363\n0.369\n0.399\n0.370\n12.8\n12.1\n12.5\n13.7\n13.7\n\n\n\n\n\n\n\n\nNotice the variables provided in the dataset. As an example, we can plots soil moisture data from a depth of 20 cm for this station for our reference:\n\n# Sample plot for soil moisture\nx=CRNdat$SOIL_MOISTURE_20_DAILY\n\n# Plot time series and density distribution \nplot(x, type=\"l\", ylab=\"Soil moisture (v/v)\", \n col=\"cyan4\", lwd=3)\nplot(density(na.omit(x)), main=\" \", xlab=\"\", \n col=\"cyan4\", lwd=3)\n\n\n\n\n\n\n\n(a) Time series of SM\n\n\n\n\n\n\n\n(b) SM kernel density\n\n\n\n\nFigure 2.1: Soil moisture values at the selected USCRN station\n\n\n\nExercise:\n\nTaking examples of any two USCRN stations across contrasting hydroclimates, compare and contrast any two recorded variables using time series plots, probability density distribution histograms and scatter plots. Select any year of your liking for the analysis.\nSelect two seasons for each elected variable and demonstrate the seasonal variability in the records for summer (MAMJJA) and winter (SONDJF) seasons using any two types of multivariate plots.\n[EXTRA]: For any chosen station, plot a time-series of soil moisture from all available layers with precipitation added as an inverted secondary axis. For inspiration, see Figure 4 in Cheng, et al. 2021. On change of soil moisture distribution with vegetation reconstruction in Mu Us sandy land of China, with newly designed lysimeter. Frontiers in Plant Science, 12, p.60952 at https://www.frontiersin.org/articles/10.3389/fpls.2021.609529/full" } ] \ No newline at end of file diff --git a/grid_plot_1.png b/grid_plot_1.png index c40c43a..ad85dd3 100644 Binary files a/grid_plot_1.png and b/grid_plot_1.png differ diff --git a/grid_plot_2.png b/grid_plot_2.png index 4b5ec4f..a29aa02 100644 Binary files a/grid_plot_2.png and b/grid_plot_2.png differ