[R] add segments from other data ggplot

Crump, Ron R.E.Crump at warwick.ac.uk
Mon Aug 15 12:56:43 CEST 2016

Dear Petr,

In your code:

p<-ggplot(ram.ag, aes(Raman, XRD, colour=mereni))
p+geom_point(size=5)+geom_segment(data=dat, aes(x=Ramanpuv,
yend=XRDnov), arrow = arrow(length = unit(1,"cm")))

you have set mereni to control the colour aesthetic in the call to ggplot.
This will be used in all subsequent calls to geoms, unless you make a
local call to aes to override it. So both ram.ag and dat would require
mereni columns for this to work.

In the second piece of code you use a call to aes to override colour in
the call to geom_segment, which gets you past the error, but do not set it
to a variable in dat. You shouldn't set an aesthetic to a constant
("black"), use colour="black" outside of the aes() if you want it set
(although it is redundant in this case, black being the default).

If you are using multiple data sources it is probably safer to refer to
them and set the appropriate aesthetics where they are used, e.g.

p <- ggplot()+geom_point(data=ram.ag,aes(x=Raman,y=XRD,colour=mereni))+

Hope this helps, I apologise if it isn't very clear.


More information about the R-help mailing list