-1

Edit: adding link to the data being used (https://drive.google.com/open?id=0B0PKu0-SX1JPRW94SXFybFd6N2c)

Before I begin, I apologize if this is a duplicate but I cannot seem to find the answer after hours of research (though it likely exists...). I want to read in data from a .csv file and produce a .png showing the density and data points from a specified working directory. This should be able to run from an isolated environment (e.g. usb drive).

I have my data in a .csv file. I have produced some images, but not like that found in the following example (See "Figure 2A" from https://www.r-bloggers.com/reproducible-art-with-r/):

ORIGINAL EXAMPLE

set.seed(16211)
rc <- rpoispp(function(x,y){50 * exp(-3*(max(y)-y))}, 100, win=W23)
rcdist <- distmap(rc, dimyx=c(1200, 800))
rc2 <- rpoispp(1/rcdist*50)
rcd <- dirichlet(rc2)

png(filename = "Figure_2A.png", width=6000, height=4000, res=400)
par(mai=c(0,0,0,0))
plot(rcdist, legend=FALSE, main="", frame=FALSE, box=FALSE, ribbon=FALSE)
plot(rcd, add=T)
plot(rc, add=T, col="black", pch=19, cex=2.5)
plot(rjitter(rc, 0.01), add=T, col="white", pch=19, cex=0.4)
contour(rcdist, add=T, col="white")
dev.off()

I however want my code to produce a similar graph but not using random points. As I sit here delirious from lack of sleep... the best I have come up with is below (and I know it is probably hideous). Any corrections would be much appreciated.

CURRENT ATTEMPT

# load workspace ----------------------------------------------------------
getwd()
setwd("D:/AH_DataToPng_Test/")

# install packages --------------------------------------------------------
install.packages("spatstat")

# load library ------------------------------------------------------------
library(spatstat)

# import my data ----------------------------------------------------------
my.data=read.csv("Figure.csv")

# view in table -----------------------------------------------------------
grid.table(my.data) #Visually confirms data input

# print the data using ppp ------------------------------------------------
# TODO: Answer - Is ppp(xdata, ydata, xrange, yrange) strictly for use in the "Plots" view window?
my.pattern <- ppp(my.data[,1],
                  my.data[,2], 
                  c(-6,6), 
                  c(-5.1,4.13))
# plot(my.pattern) #TODO: DOESN'T WORK... needed to plot the points?
# plot(density(my.pattern)) #TODO: DOESN'T WORK... needed to produce the neat color graph?
#TODO: Answer - How do I get both the points and density to display simultaneously?

# print the data to png ---------------------------------------------------
png(filename="AH_FinalProjectOutput.png", 
    units="in", 
    width=11, 
    height=8.5, 
    pointsize=12,
    res=72)
# plot(my.data) DOESN"T WORK... NOT NEEDED FOR png()?

# pause for review --------------------------------------------------------
for(i in 1:9){
  Sys.sleep(1)
  }

dev.off()
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • For those not too familiar with PNG (myself included), you might want to give some background regarding how a CSV file of point can be converted to a PNG image. – Tim Biegeleisen May 01 '17 at 08:05
  • 1
    This question needs a lot of improvement. First, you should try and supply us with your data so we can reproduce your problems. Second, you should tell us what the problem is - for example your first problem seems to be related to `"# plot(my.pattern) #TODO: DOESN'T WORK... needed to plot the points?"` BUT you don't show an error message, or give any more info beyond "DOESN'T WORK". How are we supposed to help there? Also, you should *stop* the question at that point, and not go on to complicate matters by talking about making a PNG. – Spacedman May 01 '17 at 08:11
  • I will try to update the question soon. I'm not able to access all of the info as I am on the way to the hospital. Thank you for pointing out the issues @Spaceman! – Aaron Crawford May 01 '17 at 11:18
  • With your data `plot(my.pattern)` **does** work. I see the plot. Six points in a rectangle. They are in the top left quadrant of the rectangle. So why does this not work for you? – Spacedman May 01 '17 at 11:56

1 Answers1

0
png(file="example%02d.png", width=200, height=200)
for (i in c(10:1, "G0!")){
  plot.new()
  text(.5, .5, i, cex = 6)
}
dev.off()

For me this will create 11 png files in the working directory.

TylerH
  • 20,799
  • 66
  • 75
  • 101
quant
  • 4,062
  • 5
  • 29
  • 70