adding another dimension to a 3d plot with scatterplot3d package - r

I'm working on a 3d graph with the package scatterplot3d,
but I need another dimension to the graph.
I would like to increase the size of each point inside the scatter plot, according to a numeric vaule of a fourth variable.
IS IT POSSIBLE?
my code for the plot is:
library(scatterplot3d)
with(masad, {
s3d <- scatterplot3d(HLC, cites, E.publications,
color=title, pch=19,
angle=20,
main="English",
xlim=c(0,1),
xlab="HLC",
ylab="cites",
zlab="E.publications")
s3d.coords <- s3d$xyz.convert(HLC, cites, E.publications)
legend("topright", inset=.10,
bty="n", cex=.5,
title="title",
c("none/else","doctor","professor") , fill=c("red",
"blue", "darkgreen"))
})

Here's one way to do it
library(scatterplot3d)
library(plotrix) # for rescale()
with(mtcars,
scatterplot3d(disp,
hp,
wt,
cex.symbol = rescale(qsec, c(.5, 5)), # .5 <= size <= 5
color = "#00000066", # overplotting, so add transparency
pch = 19)
)

Related

How do I plot the coordinates of a spatial dataset when plotting it over a shapefile in R?

I am plotting a SpatialPointsDataFrame object of archaeological finds over a shapefile of the trench they were found in. So far I have managed to load and plot both sets of data together with:
coordinates(finds) <- ~x+y
trencharea <- readOGR(dsn="excpoly", layer="excpoly")
trencharea <- as.owin(trencharea)
plot(trencharea, main= "Trench & Finds")
plot(finds, add=TRUE, col = "blue", pch = 4)
However, I also want the x and y coordinates associated with the finds to be displayed on the x and y axes of my plot. How do I do this? I'm not looking for a solution that involves ggplot
I don't know if I understood correctly, are you looking for the following
plot(trencharea, main= "Trench & Finds")
plot(finds, add=TRUE, col = "blue", pch = 4)
axis(1)
axis(2)
box(col = 'black')
Dummy plot

How to plot out of bound raster color scale with a specific color in image R?

I have a raster and I want to plot the out of bound color with a specific color in image plot. The code I have so far
## read the libraries
library(raster)
library(fields)
library(grDevices)
##random raster object
set.seed(1)
r <- raster(ncol=5, nrow=5)
r[] <- rnorm(n=ncell(r),mean=2)
par(mfrow=c(2,1))
col = colorRampPalette(c("darkred","red","lightskyblue","blue","blue4"))(20)
##plot without any z limit
image(r, xaxs="i", yaxs="i", col= rev(col))
##plot with z limit
image(r, xaxs="i", yaxs="i", col= rev(col),zlim = c(min(r#data#values),2))
It looks like this
The first plot is a normal image plot without specifying any limits and the second plot with some limiting condition.
I want to change the white color (out of bound values i.e. raster values higher than 2) in my second plot with the first color of color palette ("darkred").
Thanks.
You can use custom breaks with the image function to set the range of the highest display color group.
set.seed(1)
r <- raster(ncol=5, nrow=5)
r[] <- rnorm(n=ncell(r),mean=2)
par(mfrow=c(2,1))
col = colorRampPalette(c("darkred","red","lightskyblue","blue","blue4"))(20)
col = rev(col)
image(r, xaxs="i", yaxs="i", col = col, main="Initial")
breaks = seq(r#data#min, r#data#max, length.out=21)
col[which(breaks[1:20] >= 2)] = col[20]
image(r, xaxs="i", yaxs="i", col = col, breaks=breaks, main="Z-Limit")

How to add multiple straight lines in a multi plot.zoo

I have multiple time series data plots and I need an horizontal line in each plot but with different horizontal values (es. 1st plot: h=50, 2nd plot: h=48...).
I tried abline(h=50... and I get the horizontal line in each plot.
I tried abline(h=c(50,48... and I get multilple horizontal lines in each plot.
I can't figure out how to get the plot.zoo index in order to plot h=50 in the 1st plot, h=48 in the 2nd plot and so on.
library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
# plot with single line
my.panel <- function(x, ...) {
lines(x, ...)
abline(h=50, col = "red", lty="solid", lwd=1.5 )
}
plot.zoo(x, main="title",
plot.type="multiple", type="o", lwd=1.5, col="blue",
panel=my.panel)
# plot multiple lines in all plots
my.panel <- function(x, ...) {
lines(x, ...)
abline(h=c(50,50,48,50), col = "red", lty="solid", lwd=1.5 )}
plot.zoo(x, main="title",
plot.type="multiple", type="o", lwd=1.5, col="blue",
panel=my.panel)
To customize single panels in a multipanel plot is not thoroughly described in the actual ?plot.zoo text. In the 'Details' section you find:
"In the case of a custom panel the panel can reference parent.frame$panel.number in order to determine which frame the panel is being called from. See examples.". And there are quite a few examples. Using them as template, I found that this could be a way to call separate panels, and draw a separate hline in each.
Update. Thanks to #G. Grothendieck for an edit that made the code much cleaner!
# create values for hline, one for each panel
hlines <- c(50, 50, 48, 50)
# panel function that loops over panels
my.panel <- function(x, ...) {
lines(x, ...)
panel.number <- parent.frame()$panel.number
abline(h = hlines[panel.number], col = "red", lty = "solid", lwd = 1.5)
}
plot.zoo(x, main = "title", type = "o", lwd = 1.5, col = "blue", panel = my.panel)

histogram for multiple variables in R

I want to make a histogram for multiple variables.
I used the following code :
set.seed(2)
dataOne <- runif(10)
dataTwo <- runif(10)
dataThree <- runif(10)
one <- hist(dataOne, plot=FALSE)
two <- hist(dataTwo, plot=FALSE)
three <- hist(dataThree, plot=FALSE)
plot(one, xlab="Beta Values", ylab="Frequency",
labels=TRUE, col="blue", xlim=c(0,1))
plot(two, col='green', add=TRUE)
plot(three, col='red', add=TRUE)
But the problem is that they cover each other, as shown below.
I just want them to be added to each other (showing the bars over each other) i.e. not overlapping/ not covering each other.
How can I do this ?
Try replacing your last three lines by:
plot(One, xlab = "Beta Values", ylab = "Frequency", col = "blue")
points(Two, col = 'green')
points(Three, col = 'red')
The first time you need to call plot. But the next time you call plot it will start a new plot which means you lose the first data. Instead you want to add more data to it either with scatter chart using points, or with a line chart using lines.
It's not quite clear what you are looking for here.
One approach is to place the plots in separate plotting spaces:
par("mfcol"=c(3, 1))
hist(dataOne, col="blue")
hist(dataTwo, col="green")
hist(dataThree, col="red")
par("mfcol"=c(1, 1))
Is this what you're after?

How can I add regression lines to a plot that has multiple data series that are colour coded by a factor?

I wish to add regression lines to a plot that has multiple data series that are colour coded by a factor. Using a brewer.pal palette, I created a plot with the data points coloured by factor (plant$ID). Below is an example of the code:
palette(brewer.pal(12,"Paired"))
plot(x=plant$TL, y=plant$d15N, xlab="Total length (mm)", ylab="d15N", col=plant$ID, pch=16)
legend(locator(1), legend=levels(factor(plant$ID)), text.col="black", pch=16, col=c(brewer.pal(12,"Paired")), cex=0.6)
Is there an easy way to add linear regression lines to the graph for each of the different data series (factors)? I also wish to colour the lines according to the factor plant$ID?
I can achieve this by adding each of the data series to the plot separately and then using the abline function (as below), but in cases with multiple data series it can be very time consuming matching up colours.
plot(y=plant$d15N[plant$ID=="Sm"], x=plant$TL[plant$ID=="Sm"], xlab="Total length (mm)", ylab="d15N", col="green", pch=16, xlim=c(50,300), ylim=c(8,15))
points(y=plant$d15N[plant$ID=="Md"], x=plant$TL[plant$ID=="Md"], type="p", pch=16, col="blue")
points(y=plant$d15N[plant$ID=="Lg"], x=plant$TL[plant$ID=="Lg"], type="p", pch=16, col="orange")
abline(lm(plant$d15N[plant$ID=="Sm"]~plant$TL[plant$ID=="Sm"]), col="green")
abline(lm(plant$d15N[plant$ID=="Md"]~plant$TL[plant$ID=="Md"]), col="blue")
abline(lm(plant$d15N[plant$ID=="Lg"]~plant$TL[plant$ID=="Lg"]), col="orange")
legend.text<-c("Sm","Md","Lg")
legend(locator(1), legend=legend.text, col=c("green", "blue", "orange"), pch=16, bty="n", cex=0.7)
There must be a quicker way! Any help would be greatly appreciated.
Or you use ggplot2 and let it do all the hard work. Unfortunately, you example is not reproducible, so I have to create some myself:
plant = data.frame(d15N = runif(1000),
TL = runif(1000),
ID = sample(c("Sm","Md","Lg"), size = 1000, replace = TRUE))
plant = within(plant, {
d15N[ID == "Sm"] = d15N[ID == "Sm"] + 0.5
d15N[ID == "Lg"] = d15N[ID == "Lg"] - 0.5
})
> head(plant)
d15N TL ID
1 0.6445164 0.14393597 Sm
2 0.2098778 0.62502205 Lg
3 -0.1599300 0.85331376 Lg
4 -0.3173119 0.60537491 Lg
5 0.8197111 0.01176013 Sm
6 1.0374742 0.68668317 Sm
The trick is to use the geom_smooth geometry which calculates the lm and draws it. Because we use color = ID, ggplot2 knows it needs to do the whole plot for each unique ID in ID.
library(ggplot2)
ggplot(plant, aes(x = TL, y = d15N, color = ID)) +
geom_point() + geom_smooth(method = "lm")

Resources