Load the ggplot2() and reshape2() packages first. To prepare the data for plotting, the reshape2() package with the melt function is used. Plotting correlation plots in R using ggplot2 takes a bit more work than with corrplot. The ggplot2 package is a very good package in terms of utility for data visualization in R. Using ggplot2 To Create Correlation Plots In version five, I change the background colour from white to gray.Ĭorrplot(corr_gamb, tl.col = "blue", bg = "gray", tl.srt = 35, Version four shows how to change the colours. Title = "\n\n Correlation Plot Of Gambling Data \n",Īdding labels does help in assessing correlation strengths with variable pairs. # Version Two (Lower Triangular Of Correlation Matrix):Ĭorrplot(corr_gamb, tl.col = "red", tl.srt = 45, bg = "White",Ĭorrplot(corr_gamb, tl.col = "red", bg = "White", tl.srt = 35, The second version is a lower triangular version of the correlation matrix. Since the correlation matrix is symmetric, the lower or upper triangular form of the full matrix is enough. Also, it is not necessary to show the full matrix. The plot looks okay but it could use labels. Title = "\n\n Correlation Plot Of Gambling Data", # tl.cex = size of text label, bg = background colourĬorrplot(corr_gamb, tl.col = "brown", tl.srt = 30, bg = "White", # tl.col = text label colour, tl.srt = string rotation for text (The title is somewhat messed up and the image that produces is too zoomed in. Other variations do exist as you can change the arguments in terms of titles, fonts, colours and so on. I present five different correlation plots which I have come with in R. This corr_gamb variable is needed into the corrplot() function in the corrplot package. Making simple correlation plots using corrplot is not very difficult.Īt the end of the previous section, the correlation table is saved into a variable called corr_gamb. This section will deal with creating correlation table plots using the corrplot package. As an example, the correlation of status and income (row 2, column 3) is -0.2750340 which is the same as the correlation of income and status (row 3, column 2) which is also -0.2750340.Ĭorrelation Plots Using The corrplot Package The transpose of a symmetric matrix is the same matrix as before. Notice that the correlation matrix is a symmetric matrix. Correlation of status and status is one). One could show (by hand) that the correlation of two identical random variables is one. In a correlation matrix, the numeric entries along the main diagonal from top left to bottom right are ones. In base R, a correlation table can be created by using the cor() function.Ĭorr_gamb # Sex Status Income Verbal Gamble This can be done by using the colnames() function in R.Ĭolnames(gamb_data) <- c("Sex", "Status", "Income", "Verbal", "Gamble") The column names could be fixed by capitalizing them. Summary(gamb_data) # sex status income verbal One could further examine the data by using the summary() and str() functions. # 6 1 61 3.47 6 0.1 tail(gamb_data) # sex status income verbal gamble Head(gamb_data) # sex status income verbal gamble Using the head() and tail() functions, I can preview the data by looking at the first 6 rows and the last 6 rows of the data. I save this teengamb data into a new variable (copy) called gamb_data. This dataset is about teen gambling and more information on this dataset can be found by typing in ?teengamb. (The faraway package is a dataset package.)įrom the faraway package, there is a dataset called teengamb. Before looking at the data, I first load the faraway and corrplot packages into R.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |