I am not an R pro, self-taught- thank you for your help!
I have figured out the following code to my satisfaction, which gives me a 3d plot of my data and I can automatically rotate it. However, I want to stop the automatic redraw of the axes as it spins. I have found many resources using par3d(skipRedraw=TRUE) but I cannot figure out how to incorporate it into my code, it is all a bit beyond me.
Also, (I was going to give up on this one but since I'm asking anyway) I'd also like to be able to have the axes labels stay next to the axis tick marks (rather than across from it/on the other side). But this is secondary.
Thank you in advance!
mydata<-read.csv(file=file.choose(),header=TRUE,row.names=1)
mydata$Colour<-factor(mydata$ColourB,levels=c("Black","Blue","Red","Green","Yellow","Purple","Brown"))
colourb<-as.character(mydata$ColourB)
library(rgl)
open3d()
plot3d(mydata[,"Sr"],
mydata[,"Rb"],
mydata[,"Zr"],
xlab="Sr (ppm)",
ylab="Rb (ppm)",
zlab="Zr (ppm)",
pch=21,
col=colourb,
type="s",
radius=10
)
bgplot3d({
plot.new()
title(main = 'Trace Elements', line = 1)
})
play3d(spin3d(axis=c(0,0,1), rpm=10), duration=10)
It is easy to stop the automatic redraw of the axes. When you use bbox-axes (default), they are redrawn. So you just use fixed position axes. (EDITED: I might misunderstand your quiestion.)
Here is my example (using data trees):
open3d()
plot3d(trees, type="s", radius=0.4, col="red", xlab="xxx", ylab="yyy", zlab="zzz",
axes=F) # not use bbox-axes
axes3d(edges = c("x","y","z")) # draw fixed position axes
box3d() # if you need, draw full box
bgplot3d({
plot.new()
title(main = 'Trees', line = 1)
})
play3d(spin3d(axis=c(0,0,1), rpm=10), duration=10)
# PS: skipRedraw isn't what you think.
plot3d(trees)
par3d(skipRedraw = T) # you can't turn the graph by drag
Related
I'm making a raster data ploting in R, when I adjust to the area I'm working on, R displays the ylim that doesn't want to be cut off.
I tried:
# set lon lat
ylim=c(-4,2)
xlim=c(118,126)
plot(pm10_mean,xlim=xlim, ylim=ylim)
plot(shp, add=TRUE)
plot(shp2, add=TRUE)
but I got picture like this
How to remove free space above 2 and below -4? I just want to plot area in xlim and ylim
I think spplot works fine but I find it really slow and hard to customize.
I found a more recent duplicate of this question in gis.stackexchange and provided the below answer:
I found that raster::plot's documentation indicates that you can specify the plotting window and in my experience it has zero effect. Instead it will always assume the extent of the raster you give it.
My suggested workaround is to first draw your desired plot window with a blank object then add the raster plot to this window.
my_window <- extent(-4, 2, 118, 126)
plot(my_window, col=NA)
plot(my_raster, add=T)
For me, this achieves what I'm after. The caveat is if you're plotting a stack or brick, the add functionality doesn't work in my experience. The solution is to use subset, like plot(subset(my_brick,4), add=T)
I have had this problem before. You can manually resize the plot area to remove the blank areas, or insert polygons to cover the unwanted areas of shapefile. But the safest option is to use spplot as this will automatically resize the plotting area for you:
require(maptoolS)
require(raster)
data(wrld_simpl)
rs=raster()
rs[]=1
id_shp=wrld_simpl[which(wrld_simpl$ISO2=="ID"),]
rs=crop(rs,id_shp)
rs=disaggregate(rs,40)
rs=mask(rs,id_shp)
spplot(rs,ylim=c(-4,2),xlim=c(118,126),sp.layout=list('sp.lines', id_shp, lwd=2,first=F))
Just as easy to simply plot a NULL plot and add your raster
plot(NULL, xlim = c(-114.5, -35.5), ylim = c(-24.5, 29.5), asp = 30/30,
xlab = "Longitude", ylab = "Latitude")
plot(your_raster, add = T)
Stacking still seems to work
If you call function hist on r, you will note that the box that usually surrounds plotting region doesn't appear, instead, only rulers indicating plot scale appear on the bottom and on the left. If you use r a lot you may probably have noticed this already. my question is: there is some graphical parameter or workaround to make this happen on any other plot of basic r (like in a scatterplot, a line plot, a qq plot or whatever)?
The only parameter I found was axes, but setting it to FALSE makes it disappear not only the box, but also the rulers.
You are looking for box().
op <- par(mfrow=c(1, 2))
hist(mtcars$mpg, sub="w/o box")
hist(mtcars$mpg, sub="w/ box")
box() ## <-- this
par(op)
the answer is bty graphical parameter:
x= matrix(rnorm(100), ncol= 2)
plot(x, bty= 'n')
I have a plot as shown below. To this plot i would like to add a similar kind of line plot somewhere within the plot (bottomright or bottomleft). The command for the subplot i am using is
plot( 1:121, sample(1:121),type='l' )
It plots right on the top of the first one. I need it as a small plot either at the bottomleft or bottomright. COuld someone help to do this in R?
op <- par(no.readonly = TRUE)
set.seed(42)
plot(rnorm(100), runif(100))
par(new=TRUE, oma=c(3,1,1,2))
layout(matrix(1:4,2))
plot(rnorm(100), runif(100), col="blue", xlab="", ylab="")
par(op)
If you set the parameter new to TRUE, the canvas will not be cleaned before the next plotting command:
par( new= TRUE )
I leave it to your ingenuity to create a suitable white background and position the new plot :-) Hint: take a look at the omd parameter in the manual for par.
How can I use R to make axes always square in scatter plots? for example in:
> plot(iris)
or
> plot(iris$Petal.Width, iris$Petal.Length)
I'd like the axes to be square, i.e. the same length and tick labels for the x and y axes.
The current proposed answer does not work: the call,
plot(iris$Petal.Width, iris$Petal.Length, xlim=c(0,10), ylim=c(0,10), asp=1)
Generates:
which is not square, and does not have the same axis ticks and tick labels. The spaces between the x tick labels must be the same and the plot should be square, not rectangular.
You need to also set pty="s" in the graphics parameters to make the plot region square (independent of device size and limits):
par(pty="s")
plot(iris$Petal.Width, iris$Petal.Length, asp=1)
lines(2+c(0,1,1,0,0),3+c(0,0,1,1,0)) # confirm square visually
First of all, for me the plot already comes out square (big image). Clearly for you this is not the case, and you might need to make plots larger than the screen anyhow.
So, the size of the plot is controlled by the size of the output area, ie the plot window, the image file, or whatever else. Using Rstudio, you can use the built-in GUI the specify plot size. If you insist on using the base R console, you'll need to manually do the exporting. First open the file:
png("image.png", width=600, height=600)
This will open an image file in the working directory with equal proportions. Now plot:
x = iris$Petal.Width
y = iris$Petal.Length
all = c(x,y)
range = c(min(all), max(all))
plot(x, y, xlim=range, ylim=range)
And close the file:
dev.off()
The result:
I'm trying to build an histogram using data available from here. I'm using using the CSV version of this database to display the number of exoplantes discovered per year. A simple script would be
bulkdata <- read.csv('file.csv',head=1,sep=',')
pdf(file="yearcount.pdf",family="Times")
bins <- seq(min(bulkdata$discovered,na.rm=T),max(bulkdata$discovered,na.rm=T),by=1)
hist(bulkdata$discovered,breaks=bins,col='gray',ylab="Discovered",xlab="Year",main="",ylim=c(0,100),axes=FALSE)
axis(1, at=seq(1989,2012,by=1))
axis(2, at=seq(0,100,by=10))
grid(nx=10)
hist(bulkdata$discovered,breaks=bins,col='gray',ylab="Discovered",xlab="Year",main="", add=TRUE)
dev.off()
The problem is that the xaxis is not aligned with the 0 point of the yaxis. This is a problem because the lines drawn by grid() does not mean anything because they are not aligned with the ticks! I tried to add in axis(1, at=seq(1989,2012,by=1)) the option line=-1 to correct but this way the axis is correctly drawn but the grid start below the axis. Maybe a non standard package is needed?
?grid says:
If more fine tuning is required, use ‘abline(h = ., v = .)’
directly.
So here's a suggestion:
par(las=1,bty="l")
h <- hist(bulkdata$discovered,breaks=bins,
col='gray',ylab="Discovered",xlab="Year",main="",
ylim=c(0,100),axes=FALSE)
yrs <- 1989:2012
yvals <- seq(0,100,by=10)
axis(1, at=yrs)
axis(2, at=yvals)
abline(h=yvals,v=yrs,col="gray",lty=3)
hist(bulkdata$discovered,breaks=bins,
col='gray',ylab="Discovered",xlab="Year",main="", add=TRUE)
I would consider making the grid lines a little bit sparser (e.g. every 5 years?)