scores <- nyc_regents_scores |># Adjusting the data to pivot longer so its easier to usepivot_longer(cols =2:6, # grabs coulmns 2-6 and forms them into one columnnames_to ="subject", # names columns 2-6 into subjectvalues_to ="count") # seperates the score counts into one column
scores <- scores |># removing "_" from subjects so it is presented better in the visualizationmutate(subject =str_replace_all(subject, "_", " "),subject =str_to_title(subject)) #make the columns uppercase so its presentable
ggplot(scores, aes(score, subject, fill = count)) +geom_rect(aes(xmin =65, xmax =102, ymin =-Inf, ymax =Inf), fill ="lightgreen") +# found this code by researching how to fill background color based on data. Source: https://www.geeksforgeeks.org/r-language/using-geomrect-for-time-series-shading-in-r/ geom_tile(color ="white", width =1.5, height = .95) +scale_x_continuous(expand=c(0,0)) +scale_fill_gradientn(colors =brewer.pal(9, "PuBu"), trans ="sqrt") +geom_vline(xintercept=65, col ="lightgreen", linewidth =1.8) +# Added a line to show the passing grade of the examtheme_minimal(base_family ="Courier") +# changed fontlabs(y ="Subject",x ="Score" ,fill ="Count",title ="NYC Regent Test Scores by Subject",caption ="Source: dslab's nyc_regents_scores dataset Passing score is 65 (Represented by green outline)")
Warning: Removed 5 rows containing missing values or values outside the scale range
(`geom_tile()`).
For this assignment, I used the dataset “NYC_Regents_Scores” to create a heat map of the score results. I took inpsiration from the heat map example used for the disease dataset in this week’s tutorial. To start, I used pivot_longer to organize the subjects into one column and the counts into another column. When I first rendered my heatmap, I did not like the “_” in the names such as ”global_history” so I used str_replace_all and str_to_title to remove the underscores and make the first letter uppercase. I found the heatmap boring to look at so after researching the NYC regents exam’s passing score, I added a green outline for the passing scores so it can catch the viewer’s attention. I kept the xintercept line because it makes the green fill look like more of an outline compared to without it.