Overlaying R county map onto a state map - r

R newbie here.
If I first use map('state'), how can I then use map('county', ylim=..., xlim=...) but offset it like this:
Right now, I am using imagemagick's composite -gravity southwest ... to combine 2 PNG files, but this seems less than ideal.

You can use the fig option in a graphics parameter call to set the region where a figure will be plotted.
map("state")
par(fig=c(0.1,0.9,0,0.6), new=TRUE, xpd=NA)
map("county",regions="california",add=TRUE)
In regards to the other code, xpd=NA lets you plot in the margins of a plot while new=TRUE makes sure you don't scrap the existing plot when starting a new plot. Adding add=TRUE to the end of the plot for the county acts similarly to the new=TRUE call.

Related

Cicular contour map using Plotly

I am trying to create contour plots of film thicknesses on a wafer using plotly, but would like the outputted plot to be a circle since it is a wafer instead of the default square. Do I have to somehow overlay a circle on to the plot and then exclude anything outside of it after the plot is generated? I prefer using plotly if possible since it looks nice. I've tried using ggplot as well but for some reason my data doesn't work with it since the x and y coordinates are apparently irregularly spaced. I've searched around but have not seen any results at least using R.
Thanks!

How to plot a picture in R and change its background color

My goal is to plot with R a colored map of the human body, and will use here the simplified example of a smiley. I see two options but am stuck with both:
1) Extracting the different areas of the body using photoshop, and then plotting them on the top of each other:
R
link = "http://images.clipartpanda.com/clipart-smiley-face-9czEnApcE.jpeg"
download.file(link,basename(link))
jj <- readJPEG("clipart-smiley-face-9czEnApcE.jpeg",native=TRUE)
plot(1, type="n", axes=F, xlab="", ylab="")
rasterImage(jj,0.7,0.6,1,1)
I look for a way to do something like
rasterImage(jj,0.7,0.6,1,1, col="yellow")
But rasterImage works only with opaque pictures :/
2) Defining a polygon by interactively clicking on the picture, save that polygon to plot it later on the top of the original pic. That way I could simply create a map of the human body.
But most maps are for countries and so rely on available geographical data.
Is there a way to "draw" in R lines and define polygons in an easy way?

Control contour line transparency plot.kde

I'm using plot.kde in library(ks) to extract contour levels of kernel density plots. I'd like to overlay multiple plots so I'm making the contour fills semi-transparent. However, there is a border/contour line whose color I just can't seem to control.
I have tried changing all of the different col,cont.col,color (etc) options in the plot.kde function and just can't seem to hone in on the color of the contour itself. I could probably use some work around by extracting the coordinates of the contour from the kde object and then plotting this using the polygon() function, but I'd really like to control this from within plot.kde. It's something I'll be running many times.
This is likely super simple but I'm just missing it! In the figure below, it's the thicker red line I'm trying to control.
Thanks!!!
library(ks)
data(unicef)
H.scv <- Hscv(x=unicef)
fhat <- kde(x=unicef, H=H.scv, compute.cont=TRUE)
plot(fhat, display="filled.contour2", cont=c(10),col=c(NA,rgb(1,0,0,0.5)))

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

IDL: Can IDL add a colorbar/other legend info below a contour plot, so that it doesn't overlap anything?

I am using a map_set call to draw a map, and then using contour to plot some data on top of it.
I want to add a legend to this plot to make it useful, but it would have to be below the entire plot, and everything I've tried creates an overlapping legend over top of my image.
Can you do two plots -- one for your contour map, and another for the colorbar/legend,
using the system variable !P.MULTI to lay them out one above the other? You might also
need to specify YMARGIN keywords appropriately on each plot to keep them from overlapping.
You use pre-written colorbar routines (cbar from JHU or colorbar from Coyote, for example) and use the POSITION keyword to put them exactly where you want them. You can also pass POSITION to MAP_SET to position that where you want it.

Resources