-1

I have a dataframe and I want to plot it with ggplot:

ggplot(newlevels, aes(dates)) + 
geom_line(newlevels$levels, aes(y=levels), colour="red") + 
geom_line(newlevels$mean, aes(y=newlevels$mean), colour="green") + 
geom_line(newlevels$levelSMA,aes(y=newlevels$levelSMA), colour="blue") 

But, I want to plot only [1:100] lines of the dataframe, can I do it directly with ggplot without creating a new dataframe which is dataframe[1:100]? I cannot find the syntax to plot a part of data from dataframe. Thanks.

jasbner
  • 2,253
  • 12
  • 24
Nikolay Yasinskiy
  • 227
  • 1
  • 2
  • 13
  • Just subset the dataframe when you pass it to ggplot: `ggplot(newlevels[1:100,], aes(dates))` – divibisan Apr 20 '18 at 14:38
  • 1
    `ggplot` does not have a subsetting option. It assumes you've properly manipulated/subset your data before plotting. Also you really shouldn't use "$" inside `aes()` statements if the columns re coming from the passed data.frame. When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 20 '18 at 14:38

1 Answers1

1

This has been addressed by @agstudy and @pmcs ... @divibisan, i think you need the c(1:100)

For your particular dataframe, you would include this:

ggplot(newlevels[c(1:100), ], aes(x,y))+geom_points()+...
Ben
  • 1,113
  • 10
  • 26
  • If you're only using a single range, you don't need the `c()`. `1:100` returns the same thing as `c(1:100)`. If you want more than just one range, of course, you need the `c()`. – divibisan Apr 20 '18 at 14:55
  • Thanks! I suppose I am more conservative in coding sets! :) – Ben Apr 20 '18 at 14:56
  • That's fine! It actually is probably safer to include it, just in case you do want to add another row later, but it's not strictly necessary. – divibisan Apr 20 '18 at 14:58