[R] using png and identify commands

Henrik Bengtsson hb at biostat.ucsf.edu
Mon Feb 2 20:31:49 CET 2015


On Mon, Feb 2, 2015 at 9:56 AM, Antonio Silva <aolinto.lst at gmail.com> wrote:
> Hi R users
>
> I want to save a plot after using the command identify.
>
> I use identify to place labels manually near the points in order to avoid
> overlapping lines and numbers.
>
> x<-c(1,2,3,4,5,6)
> y<-c(20,30,15,7,25,40)
> plot(x,y,type="b",ylim=c(0,45))
> identify(x,y,labels=y)
>
> But when I add
>
> png(filename="test.png",width=20,height=20,units="cm",res=300)
> x<-c(1,2,3,4,5,6)
> y<-c(20,30,15,7,25,40)
> plot(x,y,type="b",ylim=c(0,45))
> identify(x,y,labels=y)
> dev.off()
>
> The plot is saved directly, without the identification of the points.My
> question is: how to save the plot only after having labelled the points.

Not that surprising, because identify() needs an "interactive" /
screen device, e.g. From help("identify"): "identify is only supported
on screen devices such as X11, windows and quartz. On other devices
the call will do nothing."

You need to do it in two steps, e.g.

x<-c(1,2,3,4,5,6)
y<-c(20,30,15,7,25,40)
plot(x,y,type="b",ylim=c(0,45))
marks <- identify(x,y,labels=y,pos=TRUE)

png(filename="test.png",width=20,height=20,units="cm",res=300)
plot(x,y,type="b",ylim=c(0,45))
xym <- cbind(x=x[marks$ind], y=y[marks$ind])
points(xym)
text(xym, labels=xym[,"y"], pos=marks$pos)
dev.off()


To make sure you call the exact same plot commands in the two steps,
you can also do:

library("R.devices")

x<-c(1,2,3,4,5,6)
y<-c(20,30,15,7,25,40)

marks <- NULL
devEval(c("x11", "png"), name="test", {
  plot(x,y,type="b",ylim=c(0,45))
  if (is.null(marks)) {
    marks <- identify(x,y,labels=y,pos=TRUE)
  } else {
    xym <- cbind(x=x[marks$ind], y=y[marks$ind])
    points(xym)
    text(xym, labels=xym[,"y"], pos=marks$pos)
  }
})

/Henrik

>
> I use R integrated to gedit in UBUNTU Trusty Tahr 64 bits. I'd like to save
> the plot using command lines and not GUIs
>
> Thanks for any help. Best regards,
>
> Antonio Olinto
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list