0

Here is the data set, df.test:

   MLSpredictions BPLPredictions
1        1.392213      0.8326201
2        1.392213      0.8662049
3        1.448370      0.9011444
4        1.448370      1.0146486
5        1.448370      0.9374932
6        1.448370      0.9374932
7        1.448370      0.9011444
8        1.448370      1.0981538
9        1.448370      1.0555757
10       1.506792      1.0555757
11       1.506792      1.1424492
12       1.506792      1.0555757
13       1.567570      1.0981538
14       1.567570      1.0981538
15       1.567570      1.1424492
16       1.567570      1.1424492
17       1.567570      1.1885314
18       1.567570      1.1424492
19       1.567570      1.1885314
20       1.630800      1.2364723

I know that GGPlot requires you to include all of the information in the same data frame which I believe I have done above.

Here is my starting point:

ggplot(df.test, aes(x = 1:20, y = , color = ))

Since my column names are different, I'm not sure what to put for "y". I've been looking all over for sample data frames that would be used in this instance but I'm coming up empty.

Please advise.

[EDIT] I would like to come up with a plot that has two lines with two different colors in the same plot.

7
  • 5
    possible duplicate. And this post gives information about aes(x) Commented Aug 15, 2016 at 3:42
  • The question lacks information on what you are trying to come up with. Can you please provide on what you are trying to plot within your graph? Commented Aug 15, 2016 at 4:53
  • ggplot(df.test, aes(x=MSLpredictions, y=BPLPredictions)) + geom_line() will give you one line for all of the data you posted. To get, say, two lines with different colors, you'd need a categorical column with two different values. For example, df.test$group = rep(c("A","B"), 10), then, ggplot(df.test, aes(x=MSLpredictions, y=BPLPredictions, color=group)) + geom_line() Commented Aug 15, 2016 at 4:53
  • @eipi10 OP can also do rbind() on the two columns, create a new data frame and plot the graph. Will that be a wrong approach? Commented Aug 15, 2016 at 5:01
  • 1
    I guess what OP wants is library(reshape2); library(ggplot2); cbind(df.test, id=1:20) %>% melt(id.vars="id") %>% ggplot(aes(x = id, y = value, colour = variable)) + geom_line() Commented Aug 15, 2016 at 5:18

1 Answer 1

1

ggplot expects the input data to be in so-called "long" format. In a long data set, 1 column contains the actual data values (whatever they may be), and all other columns tell us characteristics of those data points, such as what type of measure the values might be, what group they're a part of, etc. A long version of your data might look like:

index         variable      value
    1   MLSpredictions   1.392213
    2   MLSpredictions   1.392213
  ...              ...        ...
    1   BPLPredictions  0.8326201
    2   BPLPredictions  0.8662049
  ...              ...        ...

And you could then get your intended plot with:

my.plot <- ggplot(data = long.data, aes(x = index, y = value, color = variable)) +
           geom_line()

There are a few ways to convert your "wide" data into long format, one of which would be:

library(dplyr)
library(tidyr)

df$index <- 1:20
long.data <- gather(df, variable, value, -index)
Sign up to request clarification or add additional context in comments.

3 Comments

Thnaks, that definitely helps. Now, to make things a little more complex, say I have 4 more columns that are the upr and lwr bounds of the confidence interval for both sets of predictions, how would that work. I know how to create the confidence interval ribbon using ggplot, just not sure how to plot them all on the same plot.
@user3552144 consider asking as a separate question
I agree that it's probably best asked separately. Briefly, for every ggplot aesthetic you want to use, you need a column. So in this case, your data would be "longish", and contain another column for the error term. Assuming this column was called "error", your aes would look something like aes(x = index, y = value, ymin = value - error, ymax = value + error).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.