2

I am trying to emulate a ggplot of multiple lines which works as follows:

    set.seed(45)
    df <- data.frame(x=c(1,2,3,4,5,1,2,3,4,5,3,4,5), val=sample(1:100, 13), 
             variable=rep(paste0("category", 1:3), times=c(5,5,3)))
    ggplot(data = df, aes(x=x, y=val)) + geom_line(aes(colour=variable))

Simple example I can get this simple example to work, however on a much larger data set I am following the same steps but it is not working.

   ncurrencies = 6
   dates = c(BTC$Date, BCH$Date, LTC$Date, ETH$Date, XRP$Date, XVG$Date)
   opens = c(BTC$Open, BCH$Open, LTC$Open, ETH$Open, XRP$Open, XVG$Open)
   categories = rep(paste0("categories", 1:ncurrencies), 
             times=c(nrow(BTC), nrow(BCH), nrow(LTC), nrow(ETH), nrowXRP), nrow(XVG)))
   df = data.frame(dates, opens, categories)

   # Plot - Not correct.
   ggplot(data=df, aes(x=dates, y=opens)) + 
   geom_line(aes(colour=categories))

Real Data

As you can see, the different points are discretised and the y-axis is strange. I am guessing this is a rookie error but I have been going round in circles for a while. Can anyone see it?

P.S. I don't think I can upload the data here as it would be too much code. However, the dataframe is in the same format as the practice example and the categories match up correctly to the x and y data. Therefore I believe it is the way I am defining ggplot - I am relatively new to R.

2
  • What is the output of class(df$opens)? Commented Jan 17, 2018 at 19:42
  • 2
    Just guessing here because you haven't shared your data, but it looks like your y-axis variable may be classified as factor or character when it should be numeric. Commented Jan 17, 2018 at 21:14

1 Answer 1

1

Thank you Markus and Jan, yes you are correct. df$opens was a factor and changing it to a numeric solved the problem.

    opens = as.numeric(c(BTC$Open, BCH$Open, LTC$Open, ETH$Open, XRP$Open, XVG$Open))

Correct graph

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.