-2

I am new to learning R, and I am working with some data in a csv file. I was wondering if someone could help me in plotting the data as a line graph in RStudio. Below is an image of the chart in the csv I am plotting and the second is a picture of what I am trying to get the chart to look like from one I made using excel. Chart Data Image of Chart

Community
  • 1
  • 1
  • This is probably too open ended a question for this site. A better question would include some reproducible data, some actual code that you tried yourself, an explanation of how the code you tried didn't work. Images of data & code are pretty much useless to the people who would like to help you. – joran Jun 12 '19 at 15:06
  • 2
    Reproducible data should be given in a way that is easy for us to copy and use in our own session. In order of my personal preference as a suggestion: (1) programmatically, with `x <- data.frame(...)`; (2) unambiguous sample of data, with `dput(head(x))`, ensuring there is sufficient diversity in the data to show what you need; or (3) copy the output from the console, the least-desirable because many things might be different (know that "presentation is different from object internals"). – r2evans Jun 12 '19 at 15:10
  • Possible duplicate: https://stackoverflow.com/questions/50410524/plotting-n-columns-of-a-data-frame-as-lines-with-ggplot-in-r – MrFlick Jun 12 '19 at 15:32
  • Another possible duplicate: https://stackoverflow.com/questions/9531904/plot-multiple-columns-on-the-same-graph-in-r – MrFlick Jun 12 '19 at 15:33

1 Answers1

-1

Use the ggplot2 and tidyverse packages

`#' Make sure all packages are installed
 #' Load packages
 library(dplyr)
 library(tidyr)
 library(ggplot2)
  chart <- df %>%
    #' Get all variables into groupings for chart, exclude months
    gather(key = "group", value = "value", -MONTHS) %>%
    #' Use ggplot2 to plot
    ggplot(aes(x=MONTHS, y=value, group=group)) +
    #' Want a point and line chart with a clean white theme
       geom_point() + geom_line() + theme_minimal()`

This should get you there. You can look at the documentation for ggplot2 to see how to add titles, labels etc.

rfortin
  • 184
  • 8
  • I re-wrote the code and when I try running it I get the error message: Error in UseMethod("gather_") : no applicable method for 'gather_' applied to an object of class "function" – Noah Zouine Jun 12 '19 at 15:47
  • Is your data stored under a dataframe called df? It shouldn't be a function. – rfortin Jun 12 '19 at 15:49
  • I changed the df to what it actually should be, and it ran now, but I do not get any output when I look at the plots section in the bottom right – Noah Zouine Jun 12 '19 at 16:01
  • type chart in the console. You can see from the code above that you're assigning your output to an object called "chart", so to get what's saved in that output, you need to enter "chart" in the console – rfortin Jun 12 '19 at 16:02
  • Okay, sorry I this is really helping me learn. I have gotten the chart to plot, however it places the months in alphabetical order. Is there a way to change the order of the months so that they are like Jan, Feb,...Dec? – Noah Zouine Jun 12 '19 at 16:06
  • This link would be helpful https://stackoverflow.com/questions/36020146/how-can-i-order-the-months-chronologically-in-ggplot2-short-of-writing-the-month – rfortin Jun 12 '19 at 17:04
  • Also, please approve the answer if you found it answered your question :) – rfortin Jun 12 '19 at 17:31