Plotting map facets using R - r

I am trying to plot lat/long using PlotOnStaticMap function from the RgoogleMaps library in R.
library(RgoogleMaps);library(plotrix);
mydata <- read.csv(file=name,header=T,sep=",")
map1 <- PlotOnStaticMap(lat = mydata1$latitude, lon = mydata1$longitude, zoom=1, cex=1, pch=1, col="green", FUN=points, add=F, size = c(640,640))
and then I am trying to create a facet using other field that has two values "Yes/No"
map1 + facet_grid(section_status ~.)
I understand the function "facet_grid" is from plotrix library.
However, is there any way to combine the two, or create a facet of Maps?

Related

How to add the original data to a contour plot without external libraries in R?

I have a standard unfilled contour plot in R. It has two regions and was generated using the KDE function. It looks to be normalised to between 0 and 1. I want to plot the original data over it however R just seems to plot the data on a separate graph each time. I have tried using lines() and points(). So my two questions are: 1) how do you un-normalise a contour plot (did KDE normalise the output?) and 2) how do you plot the original data over a contour plot?
Skeleton code:
data.kde <- kde(data)
plot(data)
contour(data.kde$estimate, add=TRUE)
I am not sure if the add=TRUE statement is working, as the data is on different scales as my contour plot has come out normalised to between 0 and 1. If I normalise my original data it does not quite match where it should on the contour - the two data centres are slightly off from the contour centres.
Suppose your data is like this:
library(ks)
set.seed(1)
x <- rnorm(100)
y <- rnorm(100)
data <- cbind(x, y)
Then you can do:
KDE <- kde(data)
plot(KDE, drawpoints = TRUE)
Or if you want to use contour
contour(x = KDE$eval.points[[1]], y = KDE$eval.points[[2]], z = KDE$estimate)
points(KDE$x[,1], KDE$x[,2])
Created on 2022-02-03 by the reprex package (v2.0.1)

Change the y limits ( especially the minimum) with Vlnplot

I would like to draw a violin plot from my single cell data.
I am using this function :
Vlnplot(object, features, cols = NULL, pt.size = 0.1)
But I would like to change the y axis to 3000-10000 instead of 0-70000.
They only propose to change the y max but not the mean
Does someone have an idea how to do it ?
The VlnPlot function in the Seurat R package uses ggplot2 to draw the violin plot. This means we can modify the y-axis using scale_y_continuous.
In your case, to change the y-axis of your violin plot to 3000-10000, we would write:
VlnPlot(object, features, cols = NULL, pt.size = 0.1) + scale_y_continuous(limits = c(3000,10000))
To show you a reproducible example, we can draw a violin plot using the pbmc_small dataset from the Seurat package:
VlnPlot(pbmc_small, "CD3E")
The plot above has a default axis of 0 to around 6.3. Here is how the same plot looks like after altering the y-axis using scale_y_continuous, in which I zoom in between 0 and 3:
VlnPlot(pbmc_small, "CD3E") + scale_y_continuous(limits = c(0,3))
Many of the other visualizations in the Seurat package also use ggplot2, so you can make all types of cosmetic changes to them using various ggplot2 commands (themes, axis labels, colors, etc.)

How to export X and Y plot values from an R plot?

Using the eha and survival packages in R, I am using the following code to create the survival plot below:
with(dataset, plot(Surv(enter, exit, event), ylim= c(0.87, 1.0), fn = "surv", strata = Gender))
Rather than exporting the image of the plot, how can I export the X and Y values for each line on this plot? I attempted to use the following code, however it only creates a blank .csv file:
write.table((with(dataset, plot(Surv(enter, exit, event), ylim= c(0.87, 1.0), fn = "surv", strata = Gender))), file="testfit.csv", sep=',')
Thanks for any help in advance.
On a secondary note, does anyone happen to know how to make this plot look nicer in ggplot2?
You can use the gridGraphics package to convert your plot to a grob, then grab the points from out of the grob object. I like this solution because it is applicable to a wide range of problems.
Using the graph that was created here for an example graph:
# plot a graph
library(survival)
library(grid)
library(gridGraphics)
data(lung)
lung.surv <- survfit(Surv(time,status) ~ 1, data = lung)
plot(lung.surv)
# capture the plotted output as a grob
grid.echo()
grid.grab() -> k
# pull out the data from the grob..
k$children$`graphics-plot-1-points-1`$x -> x
k$children$`graphics-plot-1-points-1`$y -> y
I noticed that your plot is stratified by gender...Make sure you pull out the correct subset of the points for each gender.

Simple way to contour netcdf variables on a map using r package

I have to draw maps using R package so that country limits are plotted, and also the contours for the values of a meteorological variable read from a NetCDF file.
I do the following:
r=raster('netcdffile.nc')
map('worldHires', xlim=c(-10,50), ylim =c(30,50))
plot(r, add = TRUE)
contour(r, add = TRUE)
but the country limits don't appear.
It seems that the plotting of the raster, eliminates the country limits previously drawn.
I need a simple wway, please.
Attaching an example netcdf file with correct coordinates would help. I don't have any netCDF file here on hand to test. Did you try the excellent rasterVis package? You can easily plot using trellis or ggplot, and add the map in the usual ways.
For example with rasterVis and ggplot2 something like this should work:
r=raster('netcdffile.nc')
library(rasterVis)
library(maps)
world <- data.frame(map(plot=FALSE)[c("x","y")])
gplot(r) +
geom_tile(aes(fill=value)) +
geom_path(data=world, aes(x,y)) +
stat_contour(aes(z=value)) +
coord_equal()

How to plot polar coordinates in R?

Suppose that (x(t),y(t)) has polar coordinates(√t,2πt). Plot (x(t),y(t)) for t∈[0,10].
There is no proper function in R to plot with polar coordinates. I tried normal plot by giving, x=√t & y=2πt.
But resultant graph was not as expected.
I got this question from "Introduction to Scientific Programming  and Simulation using r"and the book is telling the plot should be spiral.
Make a sequence:
t <- seq(0,10, len=100) # the parametric index
# Then convert ( sqrt(t), 2*pi*t ) to rectilinear coordinates
x = sqrt(t)* cos(2*pi*t)
y = sqrt(t)* sin(2*pi*t)
png("plot1.png");plot(x,y);dev.off()
That doesn't display the sequential character, so add lines to connect adjacent points in the sequence:
png("plot2.png");plot(x,y, type="b");dev.off()
As already mentioned in a previous comment, R can plot using polar coordinates. The package plotrix has a function called polar.plot that does this. Polar coordinates are defined by length and angle. This function can take a sequence of lengths and a sequence of angles to plot with polar coordinates. For example to make one spiral:
library(plotrix)
plt.lns <- seq(1, 100, length=500)
angles <- seq(0, 5*360, length=500)%%360
polar.plot(plt.lns, polar.pos=angles, labels="", rp.type = "polygon")
An option worth a try, It is Plotly package.
library(plotly)
p <- plot_ly(plotly::mic, r = ~r, t = ~t, color = ~nms, alpha = 0.5, type = "scatter")
layout(p, title = "Mic Patterns", orientation = -90)
Note: If you are using RStudio, the plots are going to be shown in Viewer tab.

Resources