Circle plot error, some data offset from zero, plot inside - r

I am trying to plot vanishing bearing data from pigeons in R using plot.circular from the package circular. My R packages are up to date
Currently I have two problems
1) For some reason the bearings at 270 and 90 are being plotted closer to 0 than they should.
2) I would like to plot the data inside the circle rather than outside it
Cheers
Josh
Example code:
###Data
VB<-rad(c(0,90,90,180,180,180,270,270,270,270))
##Make circular
VB<-as.circular(VB, type = 'angles',units = "radians",
template = "none", modulo = "asis",
zero = pi/2, rotation = c("clock"))
##Plot
plot.circular(VB, pch = 16, cex = 1, stack = TRUE,
axes = FALSE, sep = 0.07, shrink = 1, bins = NULL, ticks = FALSE,
tcl = 0.025, tcl.text = 0.125, col = NULL, tol = 0.04, uin = NULL,
xlim = c(-1, 1), ylim = c(-1, 1), digits = 6, units = "radians",
template = NULL, zero = pi/2, rotation = "clock",
main = "", sub="", xlab = "", ylab = "",
control.circle=circle.control())

The misplaced points are apparently due to the way the bins are formed.
Try setting the bins parameter to some large value.
plot(VB, stack=TRUE, bins=360)
To plot inside the circle, you can use a negative value for the sep parameter.
plot(VB, stack=TRUE, bins=360, sep=-.07)

Related

How to remove space above scatterplot3d while box = F

I have a plot generated with scatterplot3d. I set box asbox = F with the goal to have several 2D plots visualized as 3D. Now as I want to save the plot, I have a huge margin on the top that results from the invisible box (this can be proven as I set the box = T) Now my question is how I could "crop" the plot as negative margins are not possible.
Do you have a solution how I can get rid of my huge margin as I have box = F?
image
my code:
plot_how <- scatterplot3d(Test.df,
mar = c(3, 3, 0, 1),
color = "white",
xlab = "x-lab",
ylab = " ",
type = "l",
box = T,
xlim = c(0, 16),
ylim = c(0, 9),
zlim = c(0, 15000),
zlab = expression("units"),
grid = F,
yaxt = "n",
label.tick.marks = T,
y.ticklabs = c(namesMy[1], namesMy[2], namesMy[3], namesMy[4], namesMy[5],
namesMy[6], namesMy[7], namesMy[8], namesMy[9], namesMy[10]),
lab = c(4, 9, 10),
scale.y = 0.5,
angle = 50)

I have no clues on how to add a title to a 3d graph with rgl

I have tried them all but I'm sure I'm doing something wrong. I just want to add a title, xlabel, ylabel, zlabel. I would also like to set 5 values to create the idea of what are the values (something that automatically appeared on bbox (but I don't want to use that, I'm happy with the transparent box).
Here the code I'm using and what I've tried so far (they all appear at the bottom and white:
rgl.viewpoint(theta = 0, phi = 0, fov = 60, zoom = 0.7)
rgl.bg(color = "white")
box3d()
axes3d(edges = "bbox", labels = TRUE, tick = TRUE, nticks = 5, box = FALSE, expand = 1.03, specular = "red")
title3d(main= "3D cluster visualization", xlab="Annual Income", ylab="Spending Score", zlab="Age", line = 10, outer = TRUE)
Add bgplot3d() to your code. It allows you to put base-R plotting features onto an rgl 3D plot. You'll have to play with the settings to make if fit your needs.
require(rgl)
rgl.viewpoint(theta = 0, phi = 0, fov = 60, zoom = 0.7)
rgl.bg(color = "white")
plot3d(x=1:4, y=1:4, z=1:4, type='s')
axes3d(edges = "bbox", labels = TRUE, tick = TRUE, nticks = 5, box = FALSE, expand = 1.03, specular = "red")
bgplot3d({
plot.new()
title(main = 'This is the main title', line = 2)
mtext(side = 1, 'Here is my subtitle', line = 5)
})

contourplot color and labels options in Lattice for R

I am quite new to Lattice and I am stuck with some possibly basic coding. I am using shapefiles and geoTIFFS to produce maps of animals distribution and in particular I have:
1 x point shapefile
2 x geoTIFF
1 x polygon shapefile
I am overlapping a levelplot of one of the geoTIFF (UD generated with adehabitatHR) with a contourplot of the same geoTIFF at specific intervals (percentile values), a contourplot of the second geoTIFF (depth raster from ETOPO2) for three specific values (-200, -1000 and -2000), the point shapefile (animal locations) and the polygon shapefile (land). All works fine but I need to change the font size of contour plot labels, their length (i.e. from 0.12315 to 0.123) and positioning for all the contourplots. For the depth contourplot I would like to change the style of each line in something like "continous line", "dashed line" and "point line", and for the contourplot of the UD I would like to change the color of each line using a yellow to red palette.
As far as I understand, I should use panel functions to implement these changes (e.g. Controlling z labels in contourplot) but i am not quite sure how to do it. Part of my code to generate the "plot":
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
depth<-c(-100, -200, -2000)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(locations, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = TRUE,
margin = FALSE
) + contourplot(
ETOPO2,
at = depth,
labels = TRUE,
margin = FALSE
)
A simplified image, with no UD layer and no point shapefile can be found here and as you can see it is pretty messy. Thanks for your help.
So far for the ETOPO2 countourplot I have solved by eliminating the labels and adding the argument lty to style the line. Because I can't figure out how to use lty with different values for each single line in my contour, I have replicated the contourplot function three times on the same surface, one for each contour I am interested into (this was easy because I only need three contours).
For the position, font and font size of the labels of the remaining contourplot I have used
labels = list(cex = 0.8, "verdana"),
label.style = "flat"
To "shorten" the length of the labels I have used the function round where I specify to which decimal digit to round number.
So now my new code looks like:
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(positions, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = list(cex = 0.8, "verdana"),
label.style = "flat",
margin = FALSE
) + contourplot(
ETOPO2,
at = -200,
labels = FALSE,
margin = FALSE,
lty = 1,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -1000,
labels = FALSE,
margin = FALSE,
lty = 2,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -2000,
labels = FALSE,
margin = FALSE,
lty = 3,
pretty = TRUE
)
As one could expect, it takes a bit longer to produce the plot. Still no idea on how to change the colors of the UD contourplot.

Polygon not shading to the x-axis

I am trying to plot two distributions on a graph, and I am using the polygon function to shade
x <- seq(-0.1,0.1,length=1000)
plot(x = x,
y = dnorm(x, 2, 1),
type = "l",
col = "white",
axes = FALSE,
mgp = c(2, 2, 2),
ylim=c(0,30),
frame.plot=TRUE,
xlab = " ",
ylab = " ",
font.main = 1,
main=paste(" "),
lwd=2,)
polygon(x,dnorm(x, 0, 0.019),col=6,border = NULL)
polygon(x,dnorm(x, 0.08, 0.02),col=rgb(0.8,0.8,0.8,0.5),border = T)
Unfortunately the second distribution isn't plotting correctly, as can be seen in the attached picture. I'm aware that this is because the polygon function joins the first and last points of the distribution, but I'm not sure how to specify that I would like the most far-right part of the distribution to return to zero on the y-axis before joining those two points.
Thanks in advance.

Formatting issues with Heatmap.2 function in gplots library - margins and axis

I've created a heatmap in R using the heatmap2 from the gplots package and I am having awful trouble trying to format the image for use in a report.
The image represents the eigenvalues of a small correlation matrix of wavelet coefficients and I wish to represent the largest eigenvalues with a strong colour such as red and smallest using a soft yellow or similar. The colour palette isn't so much the issue it's the representation of the colours. Currently yellow represents the largest eigenvalue so I wish to reverse the order if possible.
Also is it possible to rotate the legend similar to the first image below, I've checked the help for such info but all I can find is row and column label rotation?
Since my data represents a week's worth of data taken every hour, how can I change the x-axis values to show the following 12/24hr vector: 12,24,36,48,60,72,84,96,108,120,132,144,156,168. I tried setting this using cexCol but I got an error about cex.axis having the wrong length and I don't see this as an argument in heatmap2.
par(mfrow=c(1,1))
heatmap.2(eigenvalsCombined,
trace = "none",
dendrogram = "none",
Rowv = NULL,
Colv = NULL,
density.info = "none",
margin = c(5,7),
main = expression(paste("Heatmap of Largest Eigenvalues ",
lambda[1],
" Across 7 Wavelet Scales")),
xlab = "Time Index (hours)",
key = TRUE,
lmat = rbind(c(2,3),c(4,1)),
key.title = NA,
key.xlab = "Eigenvalue Magnitude")
As you can see from my image below the main title is also chopped off, I have tried playing with the outer and inner margins but this doesn't have any impact either. When I use the zoom feature in the plotter and blow up the image the text appears, does this mean my formatting is correct?
For those interested in the data, I have posted a copy of the dput(eigenvalsCombined) to google drive for download.
For tasks like this I am a fan of calling either image or using the fields package. To me there is no value added for using gplots and heatmap2 on a task like this.
library(fields)
sq <- c("", seq(12,168, 12))
par(mar=c(3.1,5.1,4.1,7.1), xpd=TRUE)
image(t(eigenCombined), col = rev(heat.colors(100)),
xaxt="n", yaxt="n", bty="n", xlim=c(-0.15,1),
main=expression(paste("Heatmap of Largest Eigenvalues ",
lambda[1], " Across 7 Wavelet Scales")))
axis(4, at = seq(0,1, length.out = 7),
labels = rownames(eigenCombined), lty = 0, las=2)
axis(1, at = seq(0,1, length.out = length(sq)),
labels = sq, lty = 0, las=2)
image.plot(t(eigenCombined), legend.only = TRUE,
col = rev(heat.colors(100)),
smallplot = c(0.05,0.1, 0.1,0.85))
Below you can find the solutions for some of your problems.
# (1) Define column names of data matrix following your 12/24hr vector
clnames <- rep("",ncol(eigenvalsCombined))
sq <- seq(12,168,12)
clnames[sq] <- sq
colnames(eigenvalsCombined) <- clnames
# (2) Reverse your color map
rev.heat.colors <- function(n) rev(heat.colors(n))
library(gplots)
#par(mfrow=c(1,1))
heatmap.2(eigenvalsCombined,
trace = "none",
dendrogram = "none",
Rowv = NULL,
Colv = NULL,
density.info = "none",
margin = c(5,7),
main = "",
xlab = "Time Index (hours)",
lmat = rbind(c(5,2,3),c(6,1,4)),
lwid = c(0.2, 4, 1.1),
lhei = c(0.5, 4),
key = TRUE,
key.xlab = "Eigenvalue Magnitude",
col = "rev.heat.colors",
cexCol=1.2)
# Add title to the plot
title(main=expression(paste("Heatmap of Largest Eigenvalues ",
lambda[1], " Across 7 Wavelet Scales")))
This is the plot generated by the code:
EDIT
I modified the heatmap.2 function and now the colormap is rotated according to your needs.
First, download the file myheatmap2.r from this link and save it in your working directory.
Then, run the following code:
clnames <- rep("",ncol(eigenvalsCombined))
sq <- seq(12,168,12)
clnames[sq] <- sq
colnames(eigenvalsCombined) <- clnames
rev.heat.colors <- function(n) rev(heat.colors(n))
library(gplots)
source("myheatmap2.r")
myheatmap.2(eigenvalsCombined,
trace = "none",
dendrogram = "none",
Rowv = NULL,
Colv = NULL,
density.info = "none",
margin = c(5,7),
main = "",
xlab = "Time Index (hours)",
lmat = rbind(c(2,3,6),c(4,1,5)),
lwid = c(0.8, 4, 0.5),
lhei = c(0.5, 4),
key = TRUE,
key.title="",
key.xlab = "Eigenvalue\n Magnitude",
col = "rev.heat.colors",
cexCol=1.2)
title(main=expression(paste("Heatmap of Largest Eigenvalues ",
lambda[1], " Across 7 Wavelet Scales")))
Here is the final plot:

Resources