This first chunk of code includes three graphs that compare the distribution of letter frequency for the book Harry Potter and the Sorcerer’s Stone to the distribution of letter frequency for English texts (according to wikipedia). The graph titled “Relative Letter Frequency in Harry Potter compared to English Texts” is my final visualization, but the other two graphs helped me create the final product. These other two graphs are also useful to look at because I used geom_text to write in the exact percentages. However, the final visualization really shows a side-by-side comparison of text frequencies.
In this visualization, it is easy to see that letters “I”, “E”, “T” and “A” have the most significant percentage differences. “I” has the biggest discrepancy by far. “I” makes up 11.55% of Harry Potter and the Sorcerer’s Stone and only makes up 7.0% of English Texts. I infer that this is because Harry Potter is a novel, and there are a lot of statement using “I” as a pronoun. Also, from this data, I assume that most of the English texts are non-fiction and have less “I” statements. I haven’t tested this, but I believe that this could be a plausible explanation.
“E”, “T” and “A” are all more common in English Texts than in Harry Potter in the Sorcerer’s Stone. The rest of the letters are all around the same, with a difference less than about 1%. I found it super interesting that “B” has a 1.5% frequency for both Harry Potter and the Sorcerer’s Stone and English Texts. I don’t really know why this is, but it is cool!
Finally, there is bound to be some error in this data. I downloaded a text file of Harry Potter and the Sorcerer’s Stone from GitHub, and the text was “processed a bit”. I don’t think there were huge changes, but I don’t know who converted the data, so there could be a little bit of error. Also, the data for English Text is rounded by wikipedia. Since I scraped this data from the web, I didn’t do any of the calculations to ensure that it is fully accurate. Even though there is some small error, I do think that this is pretty close to an accurate comparison!
### OBTAINING HARRY POTTER LETTER FREQUENCY
colnames(harry_potter)[1] = "text"
alphabet_count_hp <- harry_potter %>%
summarize(A = sum(str_count(text, "A|a")),
B = sum(str_count(text, "B|b")),
C = sum(str_count(text, "C|c")),
D = sum(str_count(text, "D|d")),
E = sum(str_count(text, "E|e")),
F = sum(str_count(text, "F|f")),
G = sum(str_count(text, "G|g")),
H = sum(str_count(text, "H|h")),
I = sum(str_count(text, "I|e")),
J = sum(str_count(text, "J|j")),
K = sum(str_count(text, "K|k")),
L = sum(str_count(text, "L|l")),
M = sum(str_count(text, "M|m")),
N = sum(str_count(text, "N|n")),
O = sum(str_count(text, "O|o")),
P = sum(str_count(text, "P|p")),
Q = sum(str_count(text, "Q|q")),
R = sum(str_count(text, "R|r")),
S = sum(str_count(text, "S|s")),
T = sum(str_count(text, "T|t")),
U = sum(str_count(text, "U|u")),
V = sum(str_count(text, "V|v")),
W = sum(str_count(text, "W|w")),
X = sum(str_count(text, "X|x")),
Y = sum(str_count(text, "Y|y")),
Z = sum(str_count(text, "Z|z"))) %>%
pivot_longer(everything(), names_to = "Letter", values_to = "Count") %>%
mutate(Total_letters = sum(Count)) %>% #I don't want this to be in the table though
group_by(Letter) %>%
mutate(Ratio = sum(Count)/Total_letters) %>%
mutate(Percent_harry_potter = Ratio * 100) %>%
mutate(Percent = round(Percent_harry_potter, 2))
alphabet_count_hp_tidy <- alphabet_count_hp[-(2:5)]
### OBTAINING OVERALL LETTER FREQUENCY
alphabet_url <- "https://en.wikipedia.org/wiki/Letter_frequency"
alphabet_data <- alphabet_url %>%
read_html() %>%
html_element(xpath = '//*[@id="mw-content-text"]/div[1]/table[1]') %>%
html_table()
alphabet_copy <- alphabet_data
colnames(alphabet_copy)[2] = "Percent"
alphabet_copy <- alphabet_copy[-1, ]
alphabet_copy <- alphabet_copy[-(3:5)]
alphabet_copy_tidy <- alphabet_copy %>%
mutate(across(-Letter, ~ifelse(str_detect(.x,"%"), (str_remove(.x, "%")))))
### GRAPHING THE DATA SEPERATLY - HP
alphabet_count_hp %>%
ggplot() +
geom_col(aes(x = Percent_harry_potter, y = Letter), fill = "darkgreen") +
geom_text(aes(x = Percent_harry_potter, y = Letter, label = Percent),
position = position_nudge(x = 0.5), size = 3) +
coord_cartesian(xlim = c(0, 13)) +
labs(x = "Percent of Text", y = "Letter") +
ggtitle("Relative Letter Frequency in Harry Potter and the Sorcerer's Stone")
### GRAPHING THE DATA SEPERATLY - ENGLISH TEXT
alphabet_copy_tidy %>%
ggplot() +
geom_col(aes(x = as.numeric(Percent), y = Letter), fill = "lightgreen") +
geom_text(aes(x = as.numeric(Percent), y = Letter, label = Percent),
position = position_nudge(x = 0.5), size = 3) +
coord_cartesian(xlim = c(0, 13)) +
labs(x = "Percent of Text", y = "Letter") +
ggtitle("Relative Letter Frequency in English Texts")
### JOINING THE DATA
#Note that significant figures are not consistent across data. This could account for slight rounding errors.
alphabet_count_hp_tidy1 <- alphabet_count_hp_tidy %>%
mutate(Percent_type = "Harry_Potter_Frequency")
alphabet_copy_tidy1 <- alphabet_copy_tidy %>%
mutate(Percent_type = "Overall_Frequency") %>%
mutate(Percent = as.numeric(Percent))
comparison <- alphabet_count_hp_tidy1 %>%
group_by(Percent_type, Letter) %>%
full_join(alphabet_copy_tidy1, join_by(Letter, Percent, Percent_type))
### GRAPHING THE DATA TOGETHER
comparison %>%
ggplot() +
geom_col((aes(fill = Percent_type, x = Percent, y = Letter)), position = "dodge") +
scale_fill_manual(values = c("darkgreen","lightgreen"),
name = "Type of Text", labels = c("Harry Potter", "English Texts")) +
labs(x = "Percent of Text", y = "Letter") +
ggtitle("Relative Letter Frequency in Harry Potter compared to English Texts")
The second chunk of code compares the distribution of letter frequency for the top 50 words (excluding stopwords) of Harry Potter and the Sorcerer’s Stone to the same distribution of letter frequency for English texts. I also have three graphs in this section. This is the same format as the first chunk of code, and my final visualization is called “Relative Letter Frequency in top 50 words of Harry Potter compared to English Texts”. Again, I kept the other two graphs just because they helped me in my journey towards creating the final graph.
The results in this comparison definitely changed. This visualization shows overall more discrepancy across all letters. This is likely due to having a smaller sample size of text in the top 50 words used in Harry Potter and the Sorcerer’s Stone. In this comparison, “R”, “T”, “Y”, “E” and “I” have the biggest difference in frequency. “R” particularly stands out because there is a 9.13% difference in frequency. When I calculated the top 50 words, I noticed that the top five words (harry, ron, hagrid, hermione, professor) all have at least on “R” in them. This is definitely a big factor as to why “R” is more common in Harry Potter and the Sorcerer’s Stone than in English texts. In fact, all the letters in “harry”, the most common word in the book, are all more frequent here than in English texts.
Instead of “B”, in this comparison “G” has a frequency of 2% for both texts. It is interesting that “G” was slightly more common in Harry Potter and the Sorcerer’s Stone overall, but is now slightly less common in just the top 50 words of the book. It is also interesting to see that across both comparisons, letters “T”, “E” and “I” were among the top five of biggest percentage discrepancies.
### FINDING THE TOP 50 WORDS
harry_potter_tokens <- harry_potter %>%
unnest_tokens(input = "text", output = "word")
harry_potter_tokens_top_50 <- harry_potter_tokens %>%
count(word) %>%
filter(!(word %in% stop_words$word)) %>%
arrange(-n) %>%
head(50)
### OBTAINING HARRY POTTER LETTER FREQUENCY FOR THE TOP 50 WORDS
harry_potter_tokens_count <- harry_potter_tokens_top_50 %>%
group_by(word) %>%
summarize(A = sum(str_count(word, "a"))*n,
B = sum(str_count(word, "b"))*n,
C = sum(str_count(word, "c"))*n,
D = sum(str_count(word, "d"))*n,
E = sum(str_count(word, "e"))*n,
F = sum(str_count(word, "f"))*n,
G = sum(str_count(word, "g"))*n,
H = sum(str_count(word, "h"))*n,
I = sum(str_count(word, "i"))*n,
J = sum(str_count(word, "j"))*n,
K = sum(str_count(word, "k"))*n,
L = sum(str_count(word, "l"))*n,
M = sum(str_count(word, "m"))*n,
N = sum(str_count(word, "n"))*n,
O = sum(str_count(word, "o"))*n,
P = sum(str_count(word, "p"))*n,
Q = sum(str_count(word, "q"))*n,
R = sum(str_count(word, "r"))*n,
S = sum(str_count(word, "s"))*n,
T = sum(str_count(word, "t"))*n,
U = sum(str_count(word, "u"))*n,
V = sum(str_count(word, "v"))*n,
W = sum(str_count(word, "w"))*n,
X = sum(str_count(word, "x"))*n,
Y = sum(str_count(word, "y"))*n,
Z = sum(str_count(word, "z"))*n)
harry_potter_tokens_count <- harry_potter_tokens_count[-1]
summarized_counts <- harry_potter_tokens_count %>%
pivot_longer(everything(), names_to = "Letter", values_to = "Count") %>%
group_by(Letter) %>%
summarize(Total = sum(Count))
frequency_of_top_50 <- summarized_counts %>%
mutate(Total_letters = sum(Total)) %>%
group_by(Letter) %>%
mutate(Ratio = sum(Total)/Total_letters) %>%
mutate(Percent_harry_potter = Ratio * 100) %>%
mutate(Percent = round(Percent_harry_potter, 2))
frequency_of_top_50_tidy <- frequency_of_top_50[-(2:5)]
### GRAPHING THE DATA SEPERATLY - HP
frequency_of_top_50 %>%
ggplot() +
geom_col(aes(x = Percent_harry_potter, y = Letter), fill = "darkgreen") +
geom_text(aes(x = Percent_harry_potter, y = Letter, label = Percent),
position = position_nudge(x = 0.5), size = 3) +
coord_cartesian(xlim = c(0, 16)) +
labs(x = "Percent of Text", y = "Letter") +
ggtitle("Relative Letter Frequency in top 50 words of Harry Potter and the Sorcerer's Stone")
### GRAPHING THE DATA SEPERATLY - ENGLISH TEXT
alphabet_copy_tidy %>%
ggplot() +
geom_col(aes(x = as.numeric(Percent), y = Letter), fill = "lightgreen") +
geom_text(aes(x = as.numeric(Percent), y = Letter, label = Percent),
position = position_nudge(x = 0.5), size = 3) +
coord_cartesian(xlim = c(0, 16)) +
labs(x = "Percent of Text", y = "Letter") +
ggtitle("Relative Letter Frequency in English Texts")
### JOINING THE DATA
#Note that significant figures are not consistent across data. This could account for slight rounding errors.
frequency_of_top_50_tidy1 <- frequency_of_top_50_tidy %>%
mutate(Percent_type = "Harry_Potter_Frequency_Top_50")
comparison_top_50 <- frequency_of_top_50_tidy1 %>%
group_by(Percent_type, Letter) %>%
full_join(alphabet_copy_tidy1, join_by(Letter, Percent, Percent_type))
### GRAPHING THE DATA TOGETHER
comparison_top_50 %>%
ggplot() +
geom_col((aes(fill = Percent_type, x = Percent, y = Letter)), position = "dodge") +
scale_fill_manual(values = c("darkgreen","lightgreen"),
name = "Type of Text", labels = c("Top 50 words", "English Texts")) +
labs(x = "Percent of Text", y = "Letter") +
ggtitle("Relative Letter Frequency in Top 50 Words of Harry Potter Compared to English Text")
#Question 3
Here, I have constructed a graphic comparing the average daily revenue since launch for all stores for company X. I also have a second extra graphic comparing the average daily revenue based on days opening to compare these two ways of ranking. The first graph is what I am using to quantitatively rank the success of the stores. In both of these graphs, the rounded average daily revenue is written above each bar for convienience.
The original set of data was pretty messy, so I cleaned it up and grouped the same store locations together. This information is stored in a table called “business_data_mega_summary”. There are five different columns, and they are defined below…
Market_Name: is the location of the store. Operation_length: is the amount of days since the store opened, regardless of it it was open or closed. Number_days_open: is the number of days the store is open. Final_Total_Revenue: is the sum of all the revenue recorded in the original set of data at each location. Final_Total_Sale: is the sum of all the sales made at each location.
I am considering success to be ranked by the ratio of total revenue over the operation length. I think that monetary value is more important than sale counts when determining monetary success. I also chose to factor in the length since launch, rather than just the amount of days the store was open. I made a new table called “ranking”. The column in this set called “RANKING” was calculated by Final_Total_Revenue/Operation_length. The graph titled “Store Locations and their Average Daily Revenue Since Launch” shows the stores with the highest average daily revenue on the left and the lowest average daily revenue. Using this metric, Washington DC, Phoenix AZ and Chicago IL (in that order) are the top three store locations. San Antonio TX is ranked the worst because the length since opening is 250 days, but the store has only been open for 25 during that period. Thus, there is a smaller daily average revenue.
It is important to note that there are several other ways someone could determine success. I feel like I justified the way I ranked the stores, but I wanted to see how the rankings would change if I based them off of days the store was actually open. The graph titled “EXTRA: Store Locations and their Average Daily Revenue Based on Days Open” ranks the stores using the metric Final_Total_Revenue/Number_days_open. Interestingly enough, the top 6 stores and the 9th store are ranked the same as the first graph, but the stores ranked 7th, 8th and 10th change a little bit. San Antonio jumps up in the ranks because 25 is the denominator instead of 250.
Despite the second graph providing some insight, the first graph shows my final rankings. The final rankings are also listed below for convenience (#1 being the best, and #10 being the worst).
#1 - Washington, DC #2 - Pheonix, AZ #3 - Chicago, IL #4 - Orlando, FL #5 - Tampa, FL #6 - Baltimore, MD #7 - Miami, FL #8 - Philadephia, PA #9 - Tuscon, AZ #10 - San Antonio, TX
### FINDING TOTAL REVENUE, TOTAL SALES, AND AVERAGE SALE PRICE -- ALL FACTORS OF "SUCCESS"
colnames(business_data)[2] = "Market_Name"
colnames(business_data)[3] = "Order_Count"
business_data_tidy <- business_data
business_data_summary <- business_data_tidy %>%
group_by(Market_Name) %>%
summarize(Total_Revenue = sum(Revenue),
Total_Sales = sum(Order_Count))
business_data_summary_tidy <- business_data_summary %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,","), (str_remove(.x, ",")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"D.C."), (str_remove(.x, " D.C.")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"DC DC"), (str_remove(.x, " DC")),(.x)))) %>%
group_by(Market_Name) %>%
summarize(Final_Total_Revenue = sum(Total_Revenue),
Final_Total_Sales = sum(Total_Sales))
#Average_Sale_Price = Final_Total_Revenue/Final_Total_Sales)
#Interesting, but not including in the final summary
### FINDING HOW MANY DAYS EACH STORE HAS BEEN OPEN -- ALSO A FACTOR OF "SUCCESS"
days_open <- business_data_tidy %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,","), (str_remove(.x, ",")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"D.C."), (str_remove(.x, " D.C.")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"DC DC"), (str_remove(.x, " DC")),(.x)))) %>%
#Literally counting how many times the name was mentioned in the data
#This gets the count for days the store was open
count(Market_Name)
colnames(days_open)[2] = "Number_days_open"
### FINDING HOW MANY DAYS SINCE OPENING -- ANOTHER WAY TO INTERPRET "SUCCESS"
opening_day <- business_data_tidy %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,","), (str_remove(.x, ",")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"D.C."), (str_remove(.x, " D.C.")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"DC DC"), (str_remove(.x, " DC")),(.x)))) %>%
group_by(Market_Name) %>%
arrange(Day) %>%
slice(1L)
opening_day_tidy <- opening_day[-(3:4)]
colnames(opening_day_tidy)[1] = "Opening_day"
last_day <- business_data_tidy %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,","), (str_remove(.x, ",")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"D.C."), (str_remove(.x, " D.C.")),(.x)))) %>%
mutate(across(Market_Name, ~ifelse(str_detect(.x,"DC DC"), (str_remove(.x, " DC")),(.x)))) %>%
group_by(Market_Name) %>%
arrange(desc(Day)) %>%
slice(1L)
last_day_tidy <- last_day[-(3:4)]
colnames(last_day_tidy)[1] = "Last_day"
joining_days <- last_day_tidy %>%
left_join(opening_day_tidy, join_by(Market_Name))
days_in_operation <- joining_days %>%
group_by(Market_Name) %>%
summarize(Operation_length = floor(Last_day - Opening_day)) %>%
mutate(across(Operation_length, ~ifelse(str_detect(.x,"days"), (str_remove(.x, "")),(.x))))
days_open_data <- days_in_operation %>%
left_join(days_open, join_by(Market_Name))
#mutate(Proportion_open = Number_days_open/Operation_length)
#Interesting, but not including in the final summary
### JOINING ALL OF THE FACTORS OF SUCCESS
business_data_mega_summary <- days_open_data %>%
left_join(business_data_summary_tidy, join_by(Market_Name))
### DETERMINING WHAT I WILL USE IN THE RANKING METRIC AND FINAL VISUALIZATION
#What is success TO ME?
#First, lets define the data in business_data_mega_summary...
#Market_Name is the location of the store.
#Operation_length is the amount of days since the store opened, regardless of it it was open or closed.
#Number_days_open is the number of days the store is open.
#Final_Total_Revenue is the sum of all the revenue recorded in the original set of data at each location.
#Final_Total_Sale is the sum of all the sales made at each location.
#I am considering success to be ranked by the ratio of total revenue over the operation length.
#Why?
#Well, success (in this case) is about money. Even if a store had the most amount of sales, as a business
#owner, I care more about making money than sales. Whichever store makes more money on average daily is
#the most successful to me.
#I also chose to base my success statistic on the length of days in operation rather than days open.
#I want to factor in the entire duration of the store being open into my success statistic. This will
#definitely effect how the San Antonio location will rank, given that it seems to be open infrequently.
#In summary, the quantitative metric I am using to define success is...
#Final_Total_Revenue/Operation_length.
#The stores with the highest values are the most successful based on my idea of success.
#PLEASE NOTE THAT: There are several other ways someone could determine success, but the text above
#gives context as to how I chose to rank them, and the logic behind my quantitative metric.
### RANKING THE LOCATIONS
ranking <- business_data_mega_summary %>%
mutate(RANKING = Final_Total_Revenue/Operation_length) %>%
arrange(-RANKING)
### MAKING A FINAL VISUALIZATION INCORPORATING THE RANKING FACTOR
ranking %>%
ggplot() +
geom_col(aes(x = reorder(Market_Name, -RANKING), y = RANKING, fill = Market_Name)) +
theme(axis.text.x=element_text(angle=45, hjust=0.9)) +
theme(legend.position = "none") +
labs(x = "Location (City, State)", y = "Average Daily Revenue Since Launch (Dollars)") +
ggtitle("Store Locations and their Average Daily Revenue Since Launch") +
geom_text(aes(x = reorder(Market_Name, -RANKING), y = RANKING,
label = as.character(round(RANKING, 2))), size = 3)
### WAIT!!! WHAT IF I RANKED BASED ON DAYS OPEN INSTEAD OF DAYS SINCE LAUNCH (NOT THE FINAL VISUALIZATION, JUST FOR CONTEXT)
ranking_in_a_different_way <- ranking %>%
mutate(RANKING_DIFF = Final_Total_Revenue/Number_days_open) %>%
arrange(-RANKING_DIFF)
ranking_in_a_different_way %>%
ggplot() +
geom_col(aes(x = reorder(Market_Name, -RANKING_DIFF), y = RANKING_DIFF, fill = Market_Name)) +
theme(axis.text.x=element_text(angle=45, hjust=0.9)) +
theme(legend.position = "none") +
labs(x = "Location (City, State)", y = "Average Daily Revenue Based on Days Open (Dollars)") +
ggtitle("EXTRA: Store Locations and their Average Daily Revenue Based on Days Open") +
geom_text(aes(x = reorder(Market_Name, -RANKING_DIFF), y = RANKING_DIFF,
label = as.character(round(RANKING_DIFF, 2))), size = 3)
I claim that peppermints do not help students do better on exams based on this data set. To prove this, I did several things…
The first chunk has four graphs and a table with all of the data. The first graph shows a box plot with all the individual scores of texts as points overlaying the figure. These points are color-coded by test and they are split into two groups, 0 meaning they didn’t get a peppermint and 1 meaning they did get a peppermint. At first glance, it seems that students who eat peppermints before the tests do better than students who didn’t eat peppermints.
However, I wanted some more information. I decided to facet_wrap this data based on which test the students took to see if something else was going on in this data. The second graph shows that for every test, except for maybe the fifth test, the students with peppermints did better.
I then wanted to check to see how many students were getting peppermints. This is when things seemed a little fishy. The third graph shows that several more students were being fed peppermints before the exam.
These three preliminary graphs led me to create my final table and visualization. I realized that in order to make a conclusion, I had to group the data for each individual student. I knew that it could be possible that students who generally do better on tests were the ones who “randomly” received the peppermints. By creating individual comparisons, I could come to a stronger, more informed conclusion.
The forth graph shows how each individual student scored. The dark green points are tests where the student received a peppermint and the light blue points are test where the student didn’t get a peppermint. While the y-axis is on a small scale, it doesn’t look like there is a huge difference in the scores. Also, it doesn’t look like the scores were always higher when a student had a peppermint.
I also decided to shrink my data set. I did this twice. I ran all the same graphs for each time I condensed the data. The second chuck excludes the students who only had peppermints or no mints. And the third chunk only includes students that had a 2 tests with peppermints and 3 tests without peppermints, or vice-versa. I decided to do this because I observed in the fourth graph that a few of the highest scoring students only ate peppermints. I wanted to exclude students who had nothing to compare with.
It is interesting to see how the first and second graphs progressively shift away from supporting the claim that peppermints make students score higher on tests.
Finally, I was curious about the percent difference between the highest and lowest values for each student. And I also wanted to look at the highest and lowest scores and see if the student received a peppermint or not. If the claim was true that peppermints made students do better, then all the high scores should occur when students took a peppermint. I decided to go back and make a table of these values for each of the chunk. The chunk in the final table shows that some students got their highest score with peppermints, and some students got their highest score without peppermints.
Overall, after interpreting this data, it seems that peppermints don’t really make a difference on test scores. The graphs in the third chuck particularity support this claim, and the data here is less skewed.
### CHUNK WITH ALL OF THE DATA
peppermint_data %>%
ggplot +
geom_boxplot(mapping = aes(x = factor(Peppermint), y = Score)) +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Test))) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Test") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
peppermint_data %>%
ggplot +
geom_boxplot(mapping = aes(x = factor(Peppermint), y = Score)) +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Test))) +
facet_wrap(~Test) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Test") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
peppermint_data %>%
ggplot() +
geom_bar(aes(x = factor(Peppermint))) +
labs(x = "Presence of Peppermint", y = "Number of Students") +
ggtitle("Number of Students that Consumed or did not Consume Peppermints")
#Highest scores
tile_data_highs <- peppermint_data %>%
group_by(StudentID) %>%
arrange(StudentID,-Score) %>%
slice(1L)
colnames(tile_data_highs)[3] = "mint_high"
colnames(tile_data_highs)[4] = "highs"
tile_data_highs_tidy <- tile_data_highs[-2]
#Lowest scores
tile_data_lows <- peppermint_data %>%
group_by(StudentID) %>%
arrange(StudentID, Score) %>%
slice(1L)
colnames(tile_data_lows)[3] = "mint_low"
colnames(tile_data_lows)[4] = "lows"
tile_data_lows_tidy <- tile_data_lows[-2]
#Join data IMPORTANT TABLE!!!
compared_values <- tile_data_highs_tidy %>%
left_join(tile_data_lows_tidy, join_by(StudentID)) %>%
mutate(difference = highs - lows)
#Graph
peppermint_data %>%
ggplot +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Peppermint))) +
scale_color_manual(values = c("lightblue","darkgreen")) +
facet_wrap(~StudentID) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Presence of Peppermint") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
Below is the code for the data excluding students who only had peppermints before the test, or students who never had peppermints before the test.
### CHUNK WITHOUT STUDENTS WHO HAD 5 PEPPERMINTS OR STUDENTS WHO HAD NO PEPPERMINTS
shrunk_data <- peppermint_data %>%
filter(!(StudentID %in% c("2", "5", "6", "10", "23", "28", "30")))
shrunk_data %>%
ggplot +
geom_boxplot(mapping = aes(x = factor(Peppermint), y = Score)) +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Test))) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Test") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
shrunk_data %>%
ggplot +
geom_boxplot(mapping = aes(x = factor(Peppermint), y = Score)) +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Test))) +
facet_wrap(~Test) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Test") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
shrunk_data %>%
ggplot() +
geom_bar(aes(x = factor(Peppermint))) +
labs(x = "Presence of Peppermint", y = "Number of Students") +
ggtitle("Number of Students that Consumed or did not Consume Peppermints")
#Highest scores
shrunk_data_tile_data_highs <- shrunk_data %>%
group_by(StudentID) %>%
arrange(StudentID,-Score) %>%
slice(1L)
colnames(shrunk_data_tile_data_highs)[3] = "mint_high"
colnames(shrunk_data_tile_data_highs)[4] = "highs"
shrunk_data_tile_data_highs_tidy <- shrunk_data_tile_data_highs[-2]
#Lowest scores
shrunk_data_tile_data_lows <- shrunk_data %>%
group_by(StudentID) %>%
arrange(StudentID, Score) %>%
slice(1L)
colnames(shrunk_data_tile_data_lows)[3] = "mint_low"
colnames(shrunk_data_tile_data_lows)[4] = "lows"
shrunk_data_tile_data_lows_tidy <- shrunk_data_tile_data_lows[-2]
#Join data IMPORTANT TABLE!!!
compared_values_shrunk <- shrunk_data_tile_data_highs_tidy %>%
left_join(shrunk_data_tile_data_lows_tidy , join_by(StudentID)) %>%
mutate(difference = highs - lows)
#Graph
peppermint_data %>%
ggplot +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Peppermint))) +
scale_color_manual(values = c("lightblue","darkgreen")) +
facet_wrap(~StudentID) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Presence of Peppermint") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
Below is the code for the data with students who only had 2 tests with peppermints and 3 tests without mints, or 3 tests with peppermints and 2 tests without mints.
### CHUNK WITH STUDENTS WHO ONLY HAD
# 2 TESTS WITH MINTS AND 3 TESTS WITHOUT MINTS
# 3 TESTS WITH MINTS AND 2 TEST WITHOUT MINTS
even_more_shrunk_data <- peppermint_data %>%
filter(!(StudentID %in% c("2", "5", "6", "10", "23", "28", "30"))) %>%
filter(!(StudentID %in% c("3", "13", "14", "21")))
even_more_shrunk_data %>%
ggplot +
geom_boxplot(mapping = aes(x = factor(Peppermint), y = Score)) +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Test))) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Test") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
even_more_shrunk_data %>%
ggplot +
geom_boxplot(mapping = aes(x = factor(Peppermint), y = Score)) +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Test))) +
facet_wrap(~Test) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Test") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")
even_more_shrunk_data %>%
ggplot() +
geom_bar(aes(x = factor(Peppermint))) +
labs(x = "Presence of Peppermint", y = "Number of Students") +
ggtitle("Number of Students that Consumed or did not Consume Peppermints")
#Highest scores
shrunked_highs <- even_more_shrunk_data %>%
group_by(StudentID) %>%
arrange(StudentID,-Score) %>%
slice(1L)
colnames(shrunked_highs)[3] = "mint_high"
colnames(shrunked_highs)[4] = "highs"
shrunked_highs_tidy <- shrunked_highs[-2]
#Lowest scores
shrunked_lows <- even_more_shrunk_data %>%
group_by(StudentID) %>%
arrange(StudentID, Score) %>%
slice(1L)
colnames(shrunked_lows)[3] = "mint_low"
colnames(shrunked_lows)[4] = "lows"
shrunked_lows_tidy <- shrunked_lows[-2]
#Join data IMPORTANT TABLE!!!
compared_values_shrunked <- shrunked_highs_tidy %>%
left_join(shrunked_lows_tidy, join_by(StudentID)) %>%
mutate(difference = highs - lows)
#Graph
even_more_shrunk_data %>%
ggplot +
geom_jitter(mapping = aes(x = factor(Peppermint), y = Score, color = factor(Peppermint))) +
facet_wrap(~StudentID) +
scale_color_manual(values = c("lightblue","darkgreen")) +
labs(x = "Presence of Peppermint", y = "Test Score", color = "Presence of Peppermint") +
ggtitle("Box Plot of Student Test Scores Based on Eating Peppermints")