mcraia

reddit-1

Jun 18th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.53 KB | None | 0 0
  1. # You will need packages ggplot2 and scales for this script to work
  2. library(ggplot2)
  3. attach(economics)
  4. library(scales)
  5.  
  6. ch <- economics # we will use the dataset economics from the package ggplot2
  7.  
  8. #we divide the unemployed with the total population to get the ratio as the y axis
  9. new.plot <- ggplot(ch,
  10.                    aes(x=ch$date,
  11.                        y=ch$unemploy/ch$pop,
  12.                        color = ch$unemploy/ch$pop))
  13.  
  14. # We will produce the graph with a x axis tick of each year
  15. new.plot + (scale_x_date(breaks=date_breaks("2 years"),
  16.                          name="Year",
  17.                          labels=date_format("%y"))) +
  18.   # then we will give it a format of month and year and rotate it at 90 degrees
  19.   theme(axis.text.x=element_text(angle=0,
  20.                                  hjust=0.5)) +
  21.   # we rotate the y axis 90 degrees as well
  22.   theme(axis.text.y=element_text(angle=0,
  23.                                  hjust=0)) +
  24.   # now we add the labels in our axes
  25.   scale_y_continuous(name="Unemployment %",
  26.                      labels = percent,
  27.                      limits = c(0, 0.06),
  28.                      breaks = seq(0, 0.06, 0.01)) +
  29.   # here we add the title and subtitle to our graph
  30.   ggtitle("Unemployment in the USA",
  31.           subtitle = "as % of US population \n\nData: Federal Reserve Bank of St. Louis") +  
  32.   #geom_point(alpha = 1, size = 4) +
  33.   geom_line() +
  34.   # lets make the points have a gradient color according to their value
  35.   scale_color_gradientn(colours = rainbow(7),
  36.                        breaks = c(0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06),
  37.                        labels = c("0%", "1%", "2%", "3%", "4%", "5%", "6%"),
  38.                        guide = "colorbar",
  39.                        limits = c(0,0.06)) +
  40.   # here we will adjust the color legend
  41.   guides(color = guide_colorbar(barwidth = 1,
  42.                                 barheight = 15)) +
  43.   # and we draw the line of the graph
  44.   labs(color = "") +
  45.   geom_smooth(se = F,
  46.               color = "darkgrey",
  47.               size = 1.2,
  48.               method = 'loess') +
  49.   # lets change some aspects of the theme
  50.   theme(panel.border = element_blank(),
  51.         panel.grid.major = element_blank(),
  52.         plot.title = element_text(hjust = 0.5),
  53.         plot.subtitle = element_text(hjust = 0.5),
  54.         panel.grid.minor = element_blank(),
  55.         plot.background = element_rect(fill = "transparent", color = NA),
  56.         panel.background = element_rect(fill = "transparent",color = NA),
  57.         axis.line = element_line(color = "black"))
Add Comment
Please, Sign In to add comment