Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getwd()
- options(scipen = 999) #this prevents conversion to scientific notation
- # putting a HASHTAG or number sign prevents everything
- # to the right on that line from being run
- '''
- For comments that are too long to be a single line
- one can insert 3 single quote marks like
- i did here.
- '''
- library(tidyverse)
- library(lubridate)
- library(ggplot2)
- ############### DATA: https://databank.worldbank.org/reports.aspx?source=world-development-indicators#
- data<- read_csv("Data_2.csv")
- View(data) # See the data in its entiruity
- dim(data) # rowss & columns
- head(data, 5) # see the first 5 rows.(Ommit 5 and defaults to 10)
- tail(data) # you guessed it
- glimpse(data) # pointless option
- ls(data) #list of variables
- #################### Tuesday Below ###################################
- data_2<- data%>%
- rename(variable = Years) # rename teh variables New Name = old name
- skinny_data_2<- data_2 %>%
- gather("year" , "value", -variable) # thisis where the magic happens.
- str(skinny_data_2)
- head(skinny_data_2)
- skinny_data_2$value <- as.numeric(skinny_data_2$value)
- #skinny_data_2$year<- mdy(skinny_data_2$year)
- #format(lubridate::dmy(skinny_data_2$year), "%Y")
- #yr <- as.Date(as.character(yrs), format = "%Y")
- skinny_data_2$year <- as.Date(as.character(skinny_data_2$year) , format = "%Y")
- # Odd. as.Date added April 4th to each of the years!
- # Let us keep only the forst 4 characters int hat column.
- #df$filname <- substr(df$filname, 0, 3)
- skinny_data_2$year <- substr(skinny_data_2$year , 0 , 4)
- head(skinny_data_2,3) # Much better
- str(skinny_data_2)
- head(skinny_data_2)
- # https://stackoverflow.com/questions/39946535/ggplot2-error-aesthetics-must-be-either-length-1-or-the-same-as-the-data-16
- ######################## GRAPHING ##########################
- # First we subset the data we want to graph
- # Adjusted net national income per capita (current US$)
- Net_Nat_Income <- filter(skinny_data_2, variable =="Adjusted net national income per capita (current US$)")
- head(Net_Nat_Income,20)
- #p<- ggplot(test, aes(year , pop, color = city, group = city))+
- geom_line()
- A<-ggplot(Net_Nat_Income , aes(x= year, y = value, group = 1)) +
- geom_line()+
- ggtitle("Test of code: \n Net National Income")
- A
- #Boston<-
- #filter(test, city == "Boston")
- ########################## Tuesday Above #################################
- A<- data
- head(data,3)
- A<- data%>%
- rename(variable = Years) # rename teh variables New Name = old name
- head(A,3)
- skinny_data<- A%>%
- gather("year" , "value", -variable) # thisis where the magic happens.
- View(skinny_data)
- write_csv(skinny_data, "skinny_data.csv") # thisis how we vouchsafe output . save it as a csv an export it
- str(skinny_data)
- skinny#data$year<- mdy(skinny_data$year) this is from LUBRIDATE. Uncharacteristically it did not work
- #as.Date(df$Date, "%m/%d/%Y %H:%M:%S")
- #as.Date(skinny_data$year , "%Y")
- skinny_data$year<- as.factor(skinny_data$year) # yesterday you converted to NUMERIC
- skinny_data$value <- as.numeric(skinny_data$value) # Factors are things that cant be mixed
- # together like years
- ###################################################################################
- # GRAPHS #
- ###################################################################################
- ggplot(data=skinny_data, aes(x = year, y= value,group = 1 )) +
- geom_line()+
- theme(axis.text.x = element_text(angle = 45, hjust = 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement