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.
Related
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 :-).
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="")
I am making a scatterplot matrix with three variables (x,y,z) with the points coloured different based on type (w). I want to add a legend to this scatterplot matrix to explain what the colours of plotted points stand for. However, the legend command does not seem to work (i.e. I see no legend in the plot), and trying to change margins of the scatterplot matrix seems to not be working either (no change seems to happen in the plot). My code is as below:
x <- rnorm(20, 1, 0.1)
y <- rnorm(20, 5, 1)
z <- rnorm(20, 10, 2)
w <- c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2)
dat <- data.frame(x, y, z, w)
pallet = c( "red", "blue")
plot(dat[c('x','y','z')], col=pallet[dat$w])
legend("topright", legend=c("one", "two"), col=pallet)
Is there a way to add a legend to a scatterplot matrix that I am missing here?
I'm trying to create a map which includes a raster and a polygon shapefile. Since I would also like to add a legend I'm working with the layout() command. However, I found out that plotting a raster does overwrite the layout() so it is recommended to use image() instead of plot() for the Raster ("R - plotting multiple rasters using matrix layout". It works totally fine with the layout() command and the overlaying of the polygon shapefile is no problem either. But, the map is totally distorted, there are compressions in y and y direction (see Image). On the left the layers are plotted proportional, on the right the map is distorted:
Here is part of my code:
#loading shapes and raster datasets
CityMap <- raster("CityMap.tif")
RISE <- readShapeSpatial("RISE")
setwd("G:/maps")
pdf(paste("map",i,".pdf", sep=""), width=16.53, height=11.69)
#set the layout for the map
mat <- matrix(c(1,2), 1,2)
layout(mat, c(0.75, 0.25))
par(mar=c(0.5,0.5,2.5,0.5), oma=c(0,3,1,0))
#set map extent
maxxlim <- max((data$UTM_x) + 800)
maxylim <- max((data$UTM_y) + 800)
minxlim <- min((data$UTM_x) - 800)
minylim <- min((data$UTM_y) - 800)
#plot Raster
image(Stadtkarte, xlim=c(minxlim, maxxlim), ylim=c(minylim, maxylim), maxpixels = 3000000, axes=FALSE, col = gray.colors(10, start = 0.3, end = 0.9, gamma = 2.2, alpha = NULL), useRaster = TRUE, bty='n')
#plot shape
plot(RISE, xlim=c(minxlim, maxxlim), ylim=c(minylim, maxylim), col=alpha(c("green", "yellow", "orange", "red")[RISE$z_Werte_21], 0.3), border="white", lwd=0.001, add=T)
I am pretty new to R and I don't know how to solve the problem, when plotting the raster the layers do not overlap properly, when imaging the raster the map gets distorted. Is there anybody who had this problem before or who has a solution for the problem?
I used this code to make this plot:
plot(p, cv2,col=rgb(0,100,0,50,maxColorValue=255),pch=16,
panel.last=abline(h=67,v=1.89, lty=1,lwd=3))
My plot looks like this:
1.) How can I plot the value of the ablines in a simple plot?
2.) How can I scale my plot so that both lines appear in the middle?
to change scale of plot so lines are in the middle change the axes i.e.
x<-1:10
y<-1:10
plot(x,y)
abline(a=1,b=0,v=1)
changed to:
x<-1:10
y<-1:10
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
by "value" I am assuming you mean where the line cuts the x-axis? Something like text? i.e.:
text((0), min(y), "number", pos=2)
if you want the label on the x axis then try:
abline(a=1,b=0,v=1)
axis(1, at=1,labels=1)
to prevent overlap between labels you could remove the zero i.e.:
plot(x,y,xlim=c(-30,30),yaxt="n")
axis(2, at=c(1.77,5,10,15,20,25))
or before you plot extend the margins and add the labels further from the axis
par(mar = c(6.5, 6.5, 6.5, 6.5))
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
axis(2, at=1.77,labels=1.77,mgp = c(10, 2, 0))
Similar in spirit to the answer proposed by #user1317221, here is my suggestion
# generate some fake points
x <- rnorm(100)
y <- rnorm(100)
# positions of the lines
vert = 0.5
horiz = 1.3
To display the lines at the center of the plot, first compute the horizontal and vertical distances between the data points and the lines, then adjust the limits adequately.
# compute the limits, in order for the lines to be centered
# REM we add a small fraction (here 10%) to leave some empty space,
# available to plot the values inside the frame (useful for one the solutions, see below)
xlim = vert + c(-1.1, 1.1) * max(abs(x-vert))
ylim = horiz + c(-1.1, 1.1) * max(abs(y-horiz))
# do the main plotting
plot(x, y, xlim=xlim, ylim=ylim)
abline(h=horiz, v=vert)
Now, you could plot the 'values of the lines', either on the axes (the lineparameter allows you to control for possible overlapping):
mtext(c(vert, horiz), side=c(1,2))
or alternatively within the plotting frame:
text(x=vert, y=ylim[1], labels=vert, adj=c(1.1,1), col='blue')
text(x=xlim[1], y=horiz, labels=horiz, adj=c(0.9,-0.1), col='blue')
HTH