0

I am new to R and have a problem on running a ggplot command. Below is my code. The program exits immediately without showing the diagram. Is there anything wrong with my code?

#!/usr/bin/env Rscript
library(ggplot2)

data <- read.csv(file="./data/assignment-02-data.csv",head=TRUE,sep=",")

head(data)
nrow(data)

print(ggplot(data, aes(longitude, latitude)) + geom_point())

From this post: Generate multiple graphics from within an R function. It says that I need to print the value but it is not working.

I am running the program from shell ./myr.r.

Community
  • 1
  • 1
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • http://stackoverflow.com/questions/4811106/cant-print-to-pdf-ggplot-charts – user2957945 Apr 14 '17 at 01:49
  • For troubleshooting, try adding `Sys.sleep(3)` after your `print` row. In my quick testing, the image won't show because immediately after the "print" command, R exits, killing the plot. If the plot displays for a moment then exits, you'll at least know that your script is doing what you intend. – r2evans Apr 14 '17 at 03:18
  • Did you try this? http://stackoverflow.com/questions/3301694/running-r-scripts-with-plots – ar7 Apr 14 '17 at 03:26
  • @r2evans I just tried `Sys.sleep(3)`, it gives 3 seconds before program exist but no diagram show during this period time. That means there is something wrong with `ggplot` – Joey Yi Zhao Apr 14 '17 at 04:17

0 Answers0