-3

I have a data set in excel that I am trying to create a multiple line plot with on R. The data set contains 7 food groups and the calories consumed daily associated to the groups. As well, there is that set of data over 38 years (from 1970-2008) and I am attempting to use this data set to create a multiple line plot on R. I have tried for hours on end but can not seem to get R to recognize the variables within the data set.

Please do not instantly flag my question as having "no research effort" simply because you know more than I do. That is why I am here; I have spent the last 7 days spending 6 hours a day reading r compilations and trying to make this function work but I have had no luck.

This is the data set: http://www.filedropper.com/calories. After another 2 hours of research I still can not get R to use the file beyond reading it - it simply does not treat the file as if it is still in use nor does R associate the column names as variable names. So my question is how to create a multi-line plot in R with this data set? I simply can not do it at my current knowledge of R apparently.

  • At the moment there is no question here, and almost certainly will get closed. Read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for how to ask a good question, and then try again. At a minimum, post some data, or use the `iris` data to create a `ggplot` and tell us where you get stuck. – Andrie Mar 29 '12 at 05:57
  • Share your data and you're more than likely to get a response. Even if it's not really a question. – Brandon Bertelsen Mar 29 '12 at 06:14
  • 1
    Show your code. How do you read the file? Then put the results of `str(your.variable)` in the question. And if you want us to import data from somewhere, at least put the data in a text file, not some unknown file format on some unknown website that wants me to install a PDF reader. At the moment we can only guess what you are trying to do, so it's impossible to help you. Read the link I posted earlier. Make your example reproducible, and we might be able to help. – Andrie Mar 29 '12 at 07:32

2 Answers2

3

Since your file starts like this:

"Average daily per capita calories from the U.S. food availability, adjusted for spoilage and other waste",,,,,,
,,,,,,
"Meat, eggs, and nuts",Dairy,Fruit,Vegetables,Flour and cereal products,Added fats and oils and dairy fats,Caloric sweeteners
,,,,,,
,,,,,,
,,,,,,
,,,,,,
463,267,70,125,432,411,402
472,268,72,123,426,405,405
470,265,67,122,421,416,410

you may want to remove a few lines or use the skip argument of read.csv.

d <- read.csv("Calories.csv", skip=2)
matplot(d, type="l", lty=1, lwd=3, las=1)
legend("topleft", names(d), col=1:ncol(d), lty=1, lwd=3)
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
2

Your question asked for a ggplot2 solution. Here it is.

First, load the ggplot2 package. You may have to install it first

# you may need to install the packages
# install.packages('ggplot2')
library(ggplot2)

Then, import your data frame (solution provided by Vincent Zoonekynd). In addition, you will have to transfrom it into the long format, which is the one expected by ggplot2. To do it, I'll use the melt() function from the reshape package.

d <- read.csv("Calories.csv", skip=2)
str(d)

# convert from wide to long format
# add a row index, required for plotting
d$x <- 1:nrow(d)
# melt is from the reshape library
# install.packages('reshape')
library(reshape)
md <- melt(d, id='x')
str(md)

And now, plot

ggplot(data=md, aes(x=x, y=value, colour=variable)) +
    geom_path()
Matthieu Dubois
  • 317
  • 1
  • 3