I want to plot a raster (.tif) over my gmap tile in R. It can be done with simple plot() function or with ggplot2 or with dismo gmap function. I found several examples, but none of them is really working for me.
Here is the simpliest way I was able to understand:
r<- raster(whatever.tif)
g<-gmap()
plot(g)
plot(r, add= TRUE)
This displays the correct gmap tile, it plots the desired raster, BUT it overlays also a huge legend over the whole visualization, making it not useful anymore.
From the question its obvious I am beginner. Thanks for any help!
Quick fix!
plot(raster(x), add=TRUE, legend=FALSE)
This will suppress your legend. If you desire to add a legend later check out ?legend.
Related
I’m using the metafor package to create Forest plots in R. I’d like to add text to my plots to create labels using the text() function. I’m wondering what the simplest way is to determine the x,y coordinates of where I want my text to go. Currently I just guess and see how it looks and then edit as necessary. Is there a way to overlay a grid over my plot or something to guide me (and then remove it after)?
Thank you!
Start by saving what forest() returns:
x <- forest(res)
And then take a look at x. Among other things, it contains xlim and ylim, which are the x-axis and y-axis limits. Try this:
abline(v=x$xlim)
abline(h=x$ylim)
Also useful:
text(x$xlim[1], x$rows, x$rows, xpd=NA)
I hope this helps.
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)))
##Example data to illustrate problem:
type=c("grp1","grp2","grp1","grp3","grp3","grp3","grp4")
num=c(1,1,2,4,3,5,1)
cols=c(rep("red",5),"green","red")
library(lattice)
bwplot(num~type)
par(new=T)
stripplot(num~type,col=cols)
I like the additional information displayed by the box plot but I need the information conveyed by the coloured points in the strip chart. Obviously par(new=T) doesn't work, so how can I overlay the points onto the box plot?
You can define a panel function like this :
library(lattice)
bwplot(num~type,panel=function(x,y,...){
panel.bwplot(x,y,...)
panel.stripplot(x,y,col=cols,...)
})
the answer by agstudy is quite good, though as I pointed out in a comment, any outliers plotted by bwplot will also be plotted by stripplot, which could get confusing.
Note this is only an issue if you were using jitter.data=TRUE as an option to stripplot, otherwise the duplicate points would plot directly over top of one another and you'd never know they were there. If the data had outliers, and you used jitter.data=TRUE, you'd see twice as many outliers as you should.
the following approach suppresses the outliers from bwplot to avoid this issue:
bwstrip <- function(x,y,...){
panel.stripplot(x,y,col=cols,do.out=FALSE,jitter.data=TRUE,...)
panel.bwplot(x,y,...)
}
bwplot(lobe.depths,panel=bwstrip)
I have a plot that I created using thet twoord function. I am trying to make it an interactive graph so that I can hover around a point with my mouse and see what the x,y coordinates are for that plot.
I can't figure out how to do this with the twoord.plot function. Any idea how to do this?
My apologies, I was misreading the R command for identify. FYI, just place this code: identify(x, y, labels=row.names(mydata)) after the plot command and you are able to select points accordingly.
I want to draw a polar graph in R.
I found plotrix, which has polar.plot, but I'm open to any other solutions.
It plots a polygon just fine, even though I have a few thousand points, so great!
My question: how can I change the start value of the radial axis? ie, how to do ylim in polar.plot?
Try using ggplot2 - it has a coord_polar() command to make polar plots
Here's the official Documentation:
http://docs.ggplot2.org/current/coord_polar.html
And a simple tutorial to work off of:
http://learnr.wordpress.com/2010/08/16/consultants-chart-in-ggplot2/
I found a solution for polar.plot: option radial.lim.