R "points" function misaligned with map - r

The points() function will not properly overlay the coordinates onto a map generated with base plot, even though the coordinates all match up.
misaligned map is shown below.
links to 2 simple lists of coordinates:
https://drive.google.com/drive/folders/1m0EgTw_XJ25x5r_IrsSXow-WFwFBLVXm?usp=sharing
(for "main", separator is a semicolon)
Thank you so much for your help, I have been trying to fix this for hours!
I have tried manually specifying the coordinate boundaries but that does not change the issue.
plot(main,col=1,main="",
xlim = c(-124.48,-114.13), ylim = c(32.53,42.01),
)
points(plantcoords)
misaligned plot:
as you can see, the points do not properly line up with the map, even though the range of the coordinates of the points is within the limits of the map.

Related

Control how much space legend.only takes from a raster plot in r

I am trying to simply plot a raster map and a legend barr, but i cannot seem to get it at the right distance from the raster. Is either over it or too far. Also, despite i indicate a fixed width and height to the SVG printing function, the final size does not look similar. Would it be a way to control the exact position of the legend barr when plotting legend.only=T? By the way, stack overflow does not allow me to upload an SVG image, so i am uploading a screen capture that looks similar.
here is how i see it in R
Here is how i get it from the file generated
Here is the script used to generate that image, region and range are polygons, and hill and tmax are rasters. The plotting in R works fine-I just want to know how avoid the changes in the image from the R window to the file. There seems to be something in the exporting function that is creating a high white space between the map and the barr, and it express only when exporting the graph.
svg(paste("/Users/",spp[i],"filename", ".svg",sep=""),
bg="transparent", width = 5,height=5)
plot(region)
plot(hill,col=grey(0:100/100), add=T, legend=F)
plot(range,add=T, border="turquoise")
plot(Tmaxc>tmaxsp,col=heat.colors(100),add=T,legend=F)
plot(Tmaxc>tmaxsp,col=heat.colors(100),legend.only=TRUE, horizontal = TRUE,
legend.args = list(text='Warming tolerance\u00B0C', side = 1, line = 2))
dev.off()

(Over)plotting points on a line plot

I am trying to plot individual data points on a line plot I already made as follows:
p=plot('3.29*exp(-17.4*(x^2))-0.908',xrange=[0.,1.],yrange=[-1.,1.5])
I first tried overplotting a point like this but nothing appears on the graph
estimate1=plot([0.549],[0.755],overplot=1)
When I give the plot function two points to overplot by adding another set of x and y values in input vectors, it connects them.
estimate=plot([0.349,0.9595],[0.555,0.9995],overplot=1)
How can I (over)plot the points without them being connected?
You should be able to set linestyle = 6 which will plot without the line.
I found a way around the problem I was having. After choosing a symbol for the points I wanted to show, I simply set the transparency of the line connecting them to 100 and the symbol transparency to 0.
estimate1.symbol='diamond'
estimate1.transparency=100
estimate1.sym_transparency=0
The work around is not elegant, but it works.

Difficulties with adding arrows to plot in R

I am attempting to project data onto a plot in R and see the correlation between the points. I have added a line to let the reader see the connection between these points. I am however stumped when it comes to inputting arrows to show the direction of the line. Rddproj was just an arbitrary name given to the data. Three sets of x and y coordinates are plotted x=c(-0.7159425, -0.8129311, -0.7392371); y=0.7743088, 0.7732762, 0.7490996) Here is the example below.
x<-rddproj[1:3,1]; y<-rddproj[1:3,2]
plot(x,y)
My concern is that the second group of coordinates is the greatest negative point on the x-axis. In drawing a line with arrows, the arrow will most likely point towards this point, when it should be forming a V with that point in the middle. Is it possible to plot an arrow to reflect the placement of points in a group and not just the most positive point to the most negative point or vice versa?
The arrows function ( a modified segments function) is used for this purpose (to the extent that I understand the question) in base R:
# fixed your assignment code.
plot(NA, xlim=range(x), ylim=range(y) )
arrows(head(x,-1),head(y,-1),tail(x,-1), tail(y,-1), angle=30)
An alternative reading of your question would have the glaringly obvious solution : plot(x,y) which I hope is not what you were asking since that should have been satisfactory.

How to draw abline() that doesn't intersect the Y-axis in R?

I have a location quotient plot drawn in R and want to draw a horizontal line along the plot where Y = 1. I have the code abline(h=1, col="black") but when the line is drawn, it intersects the Y-axis and crosses out my Y-axis labels.
Does anyone know how to terminate the line at the Y-axis rather than having it intersect?
Many thanks.
As mentioned in the comments, it looks like the parameter xpd has been changed, so one option is to change it back to FALSE, see ?par. you can control the clipping region using the clip function to further limit the range that abline and other functions plot within. This may also be affected by you plotting device (different devices can deal with clipping differently).

Plotting a box within filled.contour plots in R?

I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps

Resources