Use the following R code to construct a clustered bar chart with legend. Main = "Titanic Survival Rates by Passenger Class",Īgain, using the titanic3 data from section II, we will examine the relationship between passenger sex and survival rates. # STACKED BAR CHART WITH COLORS AND LEGENDĬounts = table(titanic3$survived, titanic3$pclass) Using the titanic3 data from section II, use the following R code to construct a stacked bar chart with legend. Main = "Passenger Count by Class on Titanic", barplot(class.table)įinally, let's improve the bar graph by adding some labels and color to make the graphs more understandable and presentable. Once we have the table defined as t, we can then construct our bar graph using the barplot() function. > # LETS VIEW THE TABLE TO SEE WHAT IT LOOKS LIKE Next, lets go ahead and create a table of the pclass variable using the table() function: > # LETS CREATE A TABLE OF plass DATA ONLY Harry male 48.0000 0 0 19952ġ 211.3375 B5 Southampton 2 NA St Louis, MOĢ 151.5500 C22 C26 Southampton 11 NA Montreal, PQ / Chesterville, ONģ 151.5500 C22 C26 Southampton NA Montreal, PQ / Chesterville, ONĤ 151.5500 C22 C26 Southampton 135 Montreal, PQ / Chesterville, ONĥ 151.5500 C22 C26 Southampton NA Montreal, PQ / Chesterville, ONĦ 26.5500 E12 Southampton 3 NA New York, NY Elisabeth Walton female 29.0000 0 0 24160Ģ 1st 1 Allison, Master. Pclass survived name sex age sibsp parch ticketġ 1st 1 Allen, Miss. > # LARGE DATASET - LETS JUST VIEW FIRST 6 ROWS ONLY > # LOAD PASWR LIBRARY THAT CONTAINS THE titanic3 DATASET Note: This dataset is relatively large, so the head() function is great when you just want to get a feel for the data, but don't want to see all of the data. Once the library is loaded, we can then use the head() function to view just the first six rows of the dataset. Lets begin by loading the PASWR library first, as mentioned above, the titanic3 dataset is found in the PASWR library. The titanic3 data frame does not contain information for the crew, but it does contain actual and estimated ages for almost 80% of the passengers. For this problem we will be constructing a bar graph of Titanic passengers by class using the titanic3 dataset found in the PASWR library. The titanic3 data frame describes the survival status of individual passengers on the Titanic. This will require us to summarize the raw data into a table of counts by category. Now, we are going to construct a bar graph from raw data. Main = "Preferred Communication Methods") # CONSTRUCT BAR GRAPH WITH COLOR AND LABELS # DEFINE COLORSĬolors = c("blue", "purple", "green", "orange", "red") Now, let's improve the bar graph by adding some labels and color to make the graphs more understandable and presentable. Method.names = c("Facebook", "Twitter", "Text", "Snapchat", "Whatsapp")īarplot(height = count, names.arg = method.names) # ENTER THE PREFERRED METHODS TO COMMUNICATE Lets go ahead and create a bar graph for the following data for a study on preferred method to stay connected with friends by seniors at Tulane University: Method To construct a bar graph, one needs to use the barplot() function in R.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |