Histogram chart getting cut when using package plot3D - r

I'm trying to plot a simple histogram using hist3D() from plot3D package using the following code:
library(tidyverse)
library(plot3D)
data(iris)
iris=as.tibble(iris)
x=c(1,2)
y=x
z=matrix(rnorm(4,sd=0.5,mean=1),ncol=2,nrow=2)
pmat<-hist3D(x,y,
z,
border="black",
axes=TRUE,
expand=0.4,
theta=40,phi=30,
zmin=-1,
margin=c(10,10),
mar=c(10, 1, 0, 2),
ticktype = "detailed",col="green",box=TRUE)
But the histogram get cut at the bottom:
So, I'm looking for a way to extends the width or the height of the canvas for hist3D().

You can use par(xpd = NA) before the hist3D call to allow your full device region to be used for plotting. This might not be enough space; if not, set the margins to be really large as well. For example,
par(xpd = NA, mar = c(10,10,10,10))
hist3D(x,y,
z,
border="black",
axes=TRUE,
expand=0.4,
theta=40,phi=30,
zmin=-1,
margin=c(20,20),
mar=c(10, 1, 0, 2),
ticktype = "detailed",col="green",box=TRUE)
produces this for me:
It's pretty ugly, but that's to be expected for a 3D histogram :-).

Related

Forest plot using Metafor in R to remove overall estimate and wider scale

In creating a forest plot using metafor in R, I am having trouble making some aesthetics changes to the plot as I have limited knowledge of this package. Thanks!
I'd like to remove the overall estimate at the bottom of the plot, boxed in red. This is the code I'm using and res is a rma.uni object.
forest(res, at=c(-0.5, -1, 0, 0.5, 1), xlim=c(-16,6), atransf = mytransf,
ilab=cbind(Z0, Z1, ZD0, ZD1), ilab.xpos=c(-9.5,-8,-6,-4.5),
cex=.75,
header="Subgroups",
mlab="",
xlab = 'ORR Relative Difference',
overall=FALSE, overall.hetstat = FALSE)
I would like to make the scale at the bottom wider and still spanning from -100 to 100. Right now it looks very narrow.
You need to set the ylim manually and start by 0
forest(res, at=c(-0.5, -1, 0, 0.5, 1), xlim=c(-16,6), atransf = mytransf,
ilab=cbind(Z0, Z1, ZD0, ZD1), ilab.xpos=c(-9.5,-8,-6,-4.5),
cex=.75, ylim=c(0,22),
header="Subgroups",
mlab="",
xlab = 'ORR Relative Difference',
overall=FALSE, overall.hetstat = FALSE)
to get a wider scale you have to experiment with the xlim

Can't get axis labels to show on r plot()

I'm working with the meuse dataset in the sp library in R and I'm just trying to obtain a simple plot of the meuse grid which highlights the different areas of flooding frequency. However, I can't seem to get the axis labels to display. I've tried using a par() statement beforehand but it doesn't appear to be doing anything?
data(meuse.grid) #in sp library
summary(meuse.grid)
str(meuse.grid)
coordinates(meuse.grid) = ~x+y
proj4string(meuse.grid)<-CRS("+init=epsg:28992")
gridded(meuse.grid)=TRUE
class(meuse.grid)
par(mar=c(10,10,4,2)+0.1,mgp=c(5,1,0))
plot(meuse.grid["ffreq"], scale.frac = 0.6,main="Flooding Frequency Class Map",
xlab="Easting",ylab="Northing",axes=TRUE)
Any suggestions?
You could use mtext as a fix, expand slightly outer margins oma in advance. You could also fix the title with this method.
par(mar=c(10,10,4,2) + 0.1, mgp=c(5,1,0), oma=c(2, 2, 2, 2))
plot(meuse.grid["ffreq"], scale.frac = 0.6,main="",
xlab="",ylab="",axes=TRUE)
mtext("Easting", side=1, line=3, font=2)
mtext("Northing", side=2, line=3, font=2)
mtext("Flooding Frequency Class Map", side=3, line=1, font=2, cex=1.2)
Try reducing the plot margins by setting par() before your plot() function. The default values are:
par(mar = c(5, 4, 4, 2) + 0.1)
where each number represents a side of the plot (bottom, left, top, right). setting the outer margins via par(oma) (in a similar way to above) might also help.

Scale bar using RGL

is there a way I could get scale when I run a plot in RGL in R Studio for a point cloud?
I currently have a point cloud from a las file and when I plot the data it process with a different color corresponding to changing height in the point cloud. Is there a way to get a sale bar that shows the corresponding color in RGL?
Here's one way: divide the plot region into two parts, one for the plot, one for the scale. Plot your points in one region, then use bgplot3d() in the other region to plot a scale.
For example:
library(rgl) # for the plot
library(plotrix) # for the scale
x <- rnorm(1000); y <- rnorm(1000); z <- seq(-3, 3, len=1000)
open3d(windowRect = c(10, 10, 500, 500))
layout3d(matrix(1:2, 1,2), c(0.8, 0.2), 1)
plot3d(x, y, z, col=rainbow(1000)[rank(z)])
next3d()
bgplot3d({
plot.new()
color.legend(0.1, 0.1, 0.9, 0.9,
rect.col=rainbow(1000),
legend=(-3):3, gradient="y", cex = 1.5)
})
This produces
One problem with this method of drawing a scale is that it is a bitmap drawing, so if you resize the plot, it will tend to look bad. If you want one that will scale itself, you might want to investigate the plot3Drgl package.

Why does my map plot remain the same size (and not resize accordingly) when I expand the plot?

I have plotted two sets of points on a world map in RStudio using maps, and when I try to maximise the plot, the map remains the same size.
I feel that I am missing something.
In Rstudio:
Expanded plot:
I would like the world map to maximise to fit my screen in order to better-distribute the points but it remains the same size.
Where am I going wrong?
Or is there a better package to do this in?
My code:
library(tidyverse)
library(maps)
library(geosphere)
par(mar=c(0,0,0,0))
map('world')
points(x = geochats$origin_lon, y = geochats$origin_lat, col = "green", cex = 1, pch = 20)
points(x = geochats$end_lon, y = geochats$end_lat, col = "red", cex = 1, pch = 20)
map() uses a rather archaic method to fix the plot window and aspect ratio. It doesn't behave well after rescaling the plot window (also outside of Rstudio). Not sure about Rstudio, but I suppose you can avoid this problem by using a standard plot command:
mymap <- map("world", plot=FALSE)
plot(mymap,asp=1,type="l",frame=0,axes=0,xlab="",ylab="")

Creating a reactive rectangular plot

I am relatively new to R. I am making an R Shiny app, and based on the input of the user, I would like to analyze the data and output a bar that shows the Jaccard index. This is what I want it to look like, although obviously a smooth gradient:
Please note that the Jaccard index (in this case, 0.35) will change after each input, so I'd like something reactive. I just have no idea where to start or if making plots like this is even possible in R.
Thanks.
edit: I used an online gradient generator to come up with this plot instead: how could I overlay a vertical line with its corresponding Jaccard index and corresponding location on the bar on this particular image?
edit: I want to remove the white space before the actual plot and after my text. any ideas?
With the plotrix package:
library(plotrix)
# get an empty box
plot(0:10, type="n", axes=FALSE, xlab=NA, ylab=NA)
# rectangle filled with a gradient
gradient.rect(0, 0, 10, 5, col=smoothColors("red",38,"blue"), border=NA)
# vertical bar
segments(3.5, 0, 3.5, 5, lwd=2)
text(3.5, 0, "0.35", pos=1, xpd=TRUE)
For something in base R, an imperfectly modified version of this solution, might work.
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut)-1)/(max-min)
dev.new(width=1.75, height=5)
plot(c(min,max), c(0,10), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
for (i in 1:(length(lut)-3)) {
x = (i-1)/scale + min
rect(x, 0 ,1, 30/scale, col=lut[i], border=NA)
}
}
Then make the graph as follows -
color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), 0, 1)
At this point perhaps you can add an abline(v = 0.35) to get what you want?
You could even try pointing to the appropriate position using an arrow using
arrows(0.35, -1, 0.35, 0, length = 0.07, angle = 25)

Resources