diff --git a/module3/exo3/exercice_en.Rmd b/module3/exo3/exercice_en.Rmd index 13b258ddd0da29bc3bf08c64b6a1db742f6d5409..9f2e418b768ba6ba62096776f9fc5bf01c050e44 100644 --- a/module3/exo3/exercice_en.Rmd +++ b/module3/exo3/exercice_en.Rmd @@ -1,33 +1,105 @@ + --- -title: "Your title" -author: "Your name" -date: "Today's date" +title: "Playfair's Graph Reproduction and Analysis" +author: "Your Name" output: html_document --- - ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) +library(ggplot2) +library(readxl) +library(tidyr) +library(dplyr) ``` -## Some explanations +## 1. Reproduce Playfair's Graph -This is an R Markdown document that you can easily export to HTML, PDF, and MS Word formats. For more information on R Markdown, see . +### Load and Prepare Data +```{r load-data} +# Load the Excel data (assumed in the first sheet) +data <- read_excel("F:/lin/end/lesson/New Microsoft Excel Worksheet.xlsx") + +# View the structure of the data +head(data) +``` -When you click on the button **Knit**, the document will be compiled in order to re-execute the R code and to include the results into the final document. As we have shown in the video, R code is inserted as follows: +Assuming the data contains columns `Year`, `Wheat_Price` (in shillings per quarter), and `Salary` (in shillings per week), we will reproduce Playfair's original graph. -```{r cars} -summary(cars) +### Reproduce the Graph +```{r reproduce-graph} +# Reproduce Playfair's graph +colnames(data)=c("row","Year","Wheat_Price","Salary") +ggplot(data, aes(x = Year)) + + geom_bar(aes(y = Wheat_Price), stat = "identity", fill = "grey",width = 3) + + geom_area(aes(y = Salary), fill = "blue", alpha = 0.3) + + geom_line(aes(y = Salary), color = "red", size = 1) + + labs( + title = "Playfair's Graph: Wheat Price and Salaries (1565-1821)", + x = "Year", + y = "Shillings" + ) ``` -It is also straightforward to include figures. For example: +## 2. Improve the Presentation with Two Axes -```{r pressure, echo=FALSE} -plot(pressure) +To make the presentation more accurate, we will use two y-axes: one for wheat price in shillings per quarter and one for salaries in shillings per week. + +### Plot with Two Y-Axes +```{r two-axes} +ggplot(data, aes(x = Year)) + + geom_bar(aes(y = Wheat_Price), stat = "identity", fill = "grey") + + geom_line(aes(y = Salary * 10), color = "red", size = 1) + # Scaling salary to match wheat price + scale_y_continuous( + name = "Wheat Price (Shillings per Quarter)", + sec.axis = sec_axis(~ . / 10, name = "Salary (Shillings per Week)") + ) + + labs( + title = "Wheat Price and Salaries with Dual Axes", + x = "Year" + ) + + theme( + axis.title.y.right = element_text(color = "red") + ) ``` -Note the parameter `echo = FALSE` that indicates that the code will not appear in the final version of the document. We recommend not to use this parameter in the context of this MOOC, because we want your data analyses to be perfectly transparent and reproducible. +## 3. Analyze Purchasing Power Over Time + +Next, we will calculate the purchasing power as the amount of wheat a worker can buy with their weekly salary and plot it over time. + +### Purchasing Power Calculation +```{r purchasing-power} +# Calculate purchasing power +data <- data %>% + mutate(Purchasing_Power = Salary / Wheat_Price) + +# Plot purchasing power +ggplot(data, aes(x = Year, y = Purchasing_Power)) + + geom_line(color = "green", size = 1) + + labs( + title = "Purchasing Power Over Time", + x = "Year", + y = "Wheat Quarters per Weekly Salary" + ) +``` + +## 4. Alternative Data Representation: Wheat Price vs Salary Without Time + +To represent the wheat price and salary relationship without using time directly, we will make a scatter plot of wheat price versus salary and use a color gradient to show the progression of time. + +### Wheat Price vs Salary +```{r wheat-vs-salary} +ggplot(data, aes(x = Wheat_Price, y = Salary, color = Year)) + + geom_point() + + scale_color_gradient(low = "blue", high = "red") + + labs( + title = "Wheat Price vs Salary Over Time", + x = "Wheat Price (Shillings per Quarter)", + y = "Salary (Shillings per Week)", + color = "Year" + ) +``` -Since the results are not stored in Rmd files, you should generate an HTML or PDF version of your exercises and commit them. Otherwise reading and checking your analysis will be difficult for anyone else but you. +## Conclusion -Now it's your turn! You can delete all this information and replace it by your computational document. +This analysis reproduces Playfair's famous graph, improves the clarity of the presentation with dual axes, and provides insights into the workers' purchasing power and how it evolves over time. The scatter plot of wheat price versus salary, with time represented by color, offers an alternative view of the data. \ No newline at end of file