12/27/2023 0 Comments R subplot par mf![]() We can also change the axis labels using the xlab and ylab commands: plot(drinks ~ partyHr, data=survey, pch=19, ylab="Drinks/week", This can be changed by setting the pch parameter. If we set las=2, all labels are perpendicular to the axes: barplot(percent_religion,ylim=c(0,60), ylab="percent",main="Barplot of Religion",las=2,īy default, each point is represented by an open circle. If we set las=1, the labels in the x-axis remains parallel to the x-axis, but the labels in the y-axis is perpendicular to the y-axis. The default is las=0, meaning all labels are parallel to the axes. Another method is to use the las parameter to change the orientation of labels. ![]() The first is to decrease the font size of the names using the cex.names parameter: barplot(percent_religion,ylim=c(0,60), ylab="percent",main="Barplot of Religion",īy changing the font size to 60% of the original, all names can show up. As another example, let’s create a barplot for ‘religion’: percent_religion <- table(survey$religion)/n * 100īarplot(percent_religion,ylim=c(0,60), ylab="percent",main="Barplot of Religion") We can create bar plots for other categorical variables. The names are created by the table() function. The characters are taken from the names of percent_gender: names(percent_gender) "female" "male" Note that the characters “female” and “male” are displayed on the horizontal axis instead of numbers. Here we have used the ylim parameter to set the plot range of the vertical axis, and the main=“Barplot of Gender” option is used to display the title of the barplot. We can create a barplot of percent_gender using the command barplot(percent_gender,ylim=c(0,70), ylab="percent",main="Barplot of Gender") By putting the command percent_gender <- table(survey$gender)/n * 100 inside a bracket, we not only assign a vector to the variable percent_gender but also print out its content. (percent_gender <- table(survey$gender)/n * 100) We can convert these numbers to percentages: n <- nrow(survey) # Number of students Shows that there are 754 females and 383 males. Recall that the table() command gives a summary statistics for categorical variables. The detail description of each column can be found on the survey data page on the data program website. "good_well" "parentRelationship" "workHr" "hoursCallParents" "socialMedia" "texts" "relationships" "firstKissAge" "favPeriod" To remind us of what is in the data, we type names(survey) "gender" "genderID" "greek" We see that the data frame survey is now on the work space. ![]() rda file of the Stat 100 Survey 2, Fall 2015 (combined) data we worked on previously and saved: load('Stat100_Survey2_Fall2015.rda') To illustrate the use of barplot(), we reload the. The only difference is that it is used to plot categorical data. We have already introduced two plotting functions: hist() (histogram) and curve(). We will not cover advanced topics on plotting in this introductory R course. You can learn more about plotting in R from resources listed at the end of this note. Because of the complexity of R’s plotting system, we can only scratch the surface on this topic in this short note. Then we will briefly mention the lattice graphics. We will introduce bar plots, box plots and scatter plots in the base graphics system. This note is a brief introduction to plotting in R. One example to showcase the plotting capability of R is this graph, which was created with about 150 lines of R code by Paul Butler, showing the facebook friendship connections around the world. ![]() Plotting in R can be very sophisticated and can be a very useful tool to visualize data. There are several packages in R that handle plotting.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |