0

Unable to generate scatterplot, This is my code:

png(file = "MonthVsUniqueCode.png")

p1<-plot(x = month_UC$new.col,y = month_UC$UniqueCode,

     xlab = "Month",
     ylab = "UniqueCode",
     main = "Month Vs UniqueCode"
)
dev.off()

print(p1)

Printing plot returns NULL.

print(p1) NULL


My Month_UC dataframe has 56003 rows and two columns (uniqueCode int, Month char)


Note: I've just learnt R 4 hours back. What am I doing wrong?

user20650
  • 24,654
  • 5
  • 56
  • 91
  • You may have noticed plots getting assigned to an object in examples online (especially on SO). These will generally be using `ggplot`, which are produced used the `grid` package. However, base R plots, such as you use print straight to the screen, and generally, you cant save these to an object. To see have a look at `str(p1)` ; it is NULL, hence there is nothing to print. – user20650 Nov 05 '16 at 23:37
  • Thanks! I'll need to try ggplot perhaps. Since I have 56003 points on my Y azis, the scatterplot looked like a fat line of dots, basically almost a bar graph since I only took month. What plot can i use in this case to present a better visual? to do a month-on-month analysis of my uniqueCode (customer number) according to month, should I extract the date and month field from transaction date and use them as my X axis? Should I extract date and month separately using strftime() and merge them to make my points for x axis? – Ankita Mazumdar Nov 06 '16 at 07:28
  • Its hard to say without knowing what the data are or what relationship you want to show by plotting. But a scatterplot with many points you can use this http://stackoverflow.com/questions/7714677/r-scatterplot-with-too-many-points. Or perhaps you want to add a trend line across time ?? or.... – user20650 Nov 06 '16 at 14:10
  • If you decide to ask a further question on this , please try and create a small example that shows your issue: see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Please show the code you tried , and issues with the resulting plot. – user20650 Nov 06 '16 at 14:11

1 Answers1

3

I believe the basic plotting functions do not allow for plots to be assigned to objects. You can do:

png(file = "MonthVsUniqueCode.png")
plot(x = month_UC$new.col,
     y = month_UC$UniqueCode,
     xlab = "Month",
     ylab = "UniqueCode",
     main = "Month Vs UniqueCode"
)
dev.off()

to save it to the png. Or just:

plot(x = month_UC$new.col,
     y = month_UC$UniqueCode,
     xlab = "Month",
     ylab = "UniqueCode",
     main = "Month Vs UniqueCode"
)

to display it.

ira
  • 2,542
  • 2
  • 22
  • 36
  • Since I have 56003 points on my Y azis, the scatterplot looked like a fat line of dots, basically almost a bar graph since I only took month. What plot can i use in this case to present a better visual? to do a month-on-month analysis of my uniqueCode (customer number) according to month, should I extract the date and month field from transaction date and use them as my X axis? Should I extract date and month separately using strftime() and merge them to make my points for x axis? – Ankita Mazumdar Nov 06 '16 at 07:25