Advertisement
JF11579

Jack_2

Apr 28th, 2020
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.55 KB | None | 0 0
  1.  
  2. getwd()
  3. options(scipen = 999) #this prevents conversion to scientific notation
  4.  
  5. # putting a HASHTAG or number sign prevents everything
  6. # to the right on that line from being run
  7.  
  8. '''
  9. For comments that are too long to be a single line
  10. one can insert 3 single quote marks like
  11. i did here.
  12. '''
  13.  
  14. library(tidyverse)
  15. library(lubridate)
  16. library(ggplot2)
  17.  
  18. ###############  DATA:  https://databank.worldbank.org/reports.aspx?source=world-development-indicators#
  19.  
  20. data<- read_csv("Data_2.csv")
  21. View(data)  # See the data in its entiruity
  22. dim(data)  # rowss & columns
  23. head(data, 5) #  see the first 5 rows.(Ommit 5 and defaults to 10)
  24. tail(data) # you guessed it
  25. glimpse(data)  # pointless option
  26. ls(data) #list of variables
  27.  
  28. ####################  Tuesday Below  ###################################
  29. data_2<- data%>%
  30.   rename(variable = Years)  # rename teh variables  New Name = old name
  31.  
  32. skinny_data_2<- data_2  %>%
  33.   gather("year" , "value", -variable)  # thisis where the magic happens.
  34.  
  35. str(skinny_data_2)
  36. head(skinny_data_2)
  37.  
  38. skinny_data_2$value <- as.numeric(skinny_data_2$value)
  39. #skinny_data_2$year<- mdy(skinny_data_2$year)
  40. #format(lubridate::dmy(skinny_data_2$year), "%Y")
  41. #yr <- as.Date(as.character(yrs), format = "%Y")
  42. skinny_data_2$year <- as.Date(as.character(skinny_data_2$year) , format = "%Y")
  43.  
  44. # Odd. as.Date added April 4th to each of the years!
  45. # Let us keep only the forst 4 characters int hat column.
  46. #df$filname <- substr(df$filname, 0, 3)
  47. skinny_data_2$year <- substr(skinny_data_2$year , 0 , 4)
  48. head(skinny_data_2,3)  # Much better
  49.  
  50.  
  51.  
  52. str(skinny_data_2)
  53. head(skinny_data_2)
  54.  
  55. # https://stackoverflow.com/questions/39946535/ggplot2-error-aesthetics-must-be-either-length-1-or-the-same-as-the-data-16
  56. ########################  GRAPHING  ##########################
  57. # First we subset the data we want to graph
  58. # Adjusted net national income per capita (current US$)
  59. Net_Nat_Income <- filter(skinny_data_2, variable =="Adjusted net national income per capita (current US$)")
  60. head(Net_Nat_Income,20)
  61.  
  62. #p<- ggplot(test, aes(year , pop, color = city, group = city))+
  63.   geom_line()
  64.  
  65. A<-ggplot(Net_Nat_Income , aes(x= year, y = value, group = 1)) +
  66.     geom_line()+
  67.   ggtitle("Test of code: \n Net National Income")
  68. A
  69.  
  70. #Boston<-
  71.   #filter(test, city == "Boston")
  72.  
  73. ##########################  Tuesday Above  #################################
  74. A<- data
  75. head(data,3)
  76.  
  77. A<- data%>%
  78.   rename(variable = Years)  # rename teh variables  New Name = old name
  79. head(A,3)
  80.  
  81. skinny_data<- A%>%
  82.   gather("year" , "value", -variable)  # thisis where the magic happens.
  83.  
  84. View(skinny_data)
  85.  
  86. write_csv(skinny_data, "skinny_data.csv") # thisis how we vouchsafe output .  save it as a csv an export it
  87.  
  88. str(skinny_data)
  89.  
  90. skinny#data$year<- mdy(skinny_data$year)   this is from LUBRIDATE.  Uncharacteristically it did not work
  91. #as.Date(df$Date, "%m/%d/%Y %H:%M:%S")
  92. #as.Date(skinny_data$year , "%Y")
  93. skinny_data$year<- as.factor(skinny_data$year)  # yesterday you converted to NUMERIC
  94. skinny_data$value <- as.numeric(skinny_data$value)  # Factors are things that cant be mixed
  95.                                                     # together like years
  96.  
  97.  
  98. ###################################################################################
  99. #                                 GRAPHS                                          #
  100. ###################################################################################
  101. ggplot(data=skinny_data, aes(x = year, y= value,group = 1 )) +
  102.         geom_line()+
  103.   theme(axis.text.x = element_text(angle = 45, hjust = 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement