3

I am new to R and I am trying to convert plot to ggplot.

plot(res$s, type="n", main=title)  
print(lines(res$s)) 

res$s output

2014-02-14 51.8460                                                         
2014-02-14 44.5080  
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
kKenny
  • 89
  • 1
  • 6
  • 1
    You can't convert a plot object to a ggplot object. But you can use `ggplot` instead of `plot` in your code. What have you tried so far? Can you give us a [mcve]? –  Apr 01 '18 at 19:54
  • Try this https://stackoverflow.com/questions/49590999/how-to-convert-a-one-variable-list-chart-in-plot-into-ggplot2-format/49591415?noredirect=1#comment86202179_49591415 – Jack Brookes Apr 01 '18 at 20:31
  • @kKenny Can you expand on this? What is your intended output vs actual? – NonCreature0714 Apr 01 '18 at 22:23

2 Answers2

8

Using package ggplotify: https://cran.r-project.org/web/packages/ggplotify/vignettes/ggplotify.html

library(ggplotify)
res<-read.table(text="
s
\"2014-02-14 51.8460\"                                                         
\"2014-02-14 44.5080\" ", header=T)  
p <- as.ggplot(function() plot(res$s, type="n", main="title")  )
p
Ferroao
  • 3,042
  • 28
  • 53
0

You cannot "convert" a graphic from base R to ggplot2. You have to replot the data. ggplot2 provides the function qplot()that has a syntax similar to plot().

If you have a two column dataframe: df$x and df$y,

qplot(df$s,df$y, geom='line')

should do what you want.

xraynaud
  • 2,028
  • 19
  • 29