Repository navigation
Expand file tree
/
Copy pathR_data_analysis_example.Rmd
More file actions
55 lines (42 loc) · 1.61 KB
/
Copy pathR_data_analysis_example.Rmd
File metadata and controls
55 lines (42 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
title: "Data analysis example"
output: html_document
---
```{r setup, include=FALSE}
library(readxl)
library(dplyr)
library(tidyverse)
library(ggplot2)
```
```{r}
# file obtained from https://webarchive.nationalarchives.gov.uk/ukgwa/20190603113555/https://orr.gov.uk/statistics/published-stats/station-usage-estimates
# file location to read
file <- "C:\\Users\\kelvi\\Desktop\\estimates-of-station-usage-2017-18.xlsx"
# load file
file <- read_excel(file,sheet = "Estimates of Station Usage")
# drop not needed columns
file <- dplyr::select(file,-`OS Grid Easting`,-`OS Grid Northing`)
# select only columns of interest
file <- dplyr::select(file,TLC,`Station Name`,Region,`Local Authority`,`Station Facility Owner`,`SRS Description`,`NR Route`,`1718 Entries & Exits`)
# get summary of dataframe
summary(file)
# remove NA values
file <- drop_na(file)
# let's see how many entries and exits were made by each specific column
columns <- c("Region","Local Authority","Station Facility Owner")
for (coler in columns){
type <- ensym(coler)
filer <- file %>%
group_by(!!type)%>%
summarise(sumer=sum(`1718 Entries & Exits`)) %>%
mutate(pct=round((sumer/sum(sumer))*100,2)) %>%
arrange(-sumer) # arrange highest to lowest
# graph each
ggplot(filer,aes(x=reorder(!!type,-pct),y=pct))+ # reorder highest to lowest
geom_col(fill = "#CD2456")+
geom_text(aes(label=paste(round(pct,0),"%",sep=""),vjust=-0.5))+
theme_classic()
#save graph
#ggsave(path="C:\\Users\\kelvi\\Desktop\\",paste(coler,".png",sep=""),height=15,width=30,units="cm")
}
```