Change direction of axis title and label in 3dplots in R? - r

I have a question that might be easy for a person who is expert in R plot. I need to draw 3D plot in R. My data is as follows:
df <- data.frame(a1 = c(489.4, 505.8, 525.8, 550.2, 576.6),
a2 = c(197.8, 301, 389.8, 502, 571.2),
b1 = c(546.8, 552.6, 558.4, 566.4, 575),
b2 = c(287.2, 305.8, 305.2, 334.4, 348.6), c1 = c(599.6, 611.4,
623.6, 658, 657.4), c2 = c(318.8, 423.2, 510.8, 662.4, 656),
d1 = c(616, 606.8, 600.2, 595.6, 595),
d2 = c(242.4, 292.8, 329.2, 378, 397.2),
e1 = c(582.4, 580, 579, 579, 579),
e2 = c(214, 255.4, 281.8, 303.8, 353.8))
colnames(df) <- rep(c("V1", "V2"), 5)
df.new <- rbind(df[, c(1, 2)],df[, c(3, 4)],df[, c(5, 6)],
df[, c(7, 8)],df[, c(9, 10)])
df.new$Group <- factor(rep(c("a","b","c","d","e"), each = 5))
df.new$Class <- rep(c(1:5), 5)
I am drawing a 3D Plot using scatterplot3d package.
x=df.new$Class
y=V1
z=V2
scatterplot3d(x,y,z, pch = 16, color=colors,main="3D V1 v.s V2",xlab =
"Class",ylab = "V1", zlab = "V2")
Now I want to do 2 modifications. One is to make the vertical title of those axis horizontal and the next is to put a label for values of x and for example put a label "first interval" for 1 in x values and so one and so forth. How an I do it?
Also, how can I make the points linear or plane instead of dots.

One is to make the vertical title of those axis horizontal
To do this you need to hide the current label, and use the text() function to add a rotated label in the correct spot; as described here Rotate y-axis label in scatterplot3d (adjust to angle of axis)
set.seed(42)
scatterplot3d(rnorm(20), rnorm(20), rnorm(20), ylab = "")
text(x = 5, y = -2.5, "Y-axis", srt = 45)
and for example put a label "first interval" for 1 in x values and so one and so forth. How an I do it?
From the documentation - https://cran.r-project.org/web/packages/scatterplot3d/scatterplot3d.pdf
Use the x.ticklabs attribute, for example:
xlabs <- c("first interval", "second", "third", "fourth", "this is 5")
scatterplot3d(x,y,z, pch =16,main="3D V1 v.s V2",xlab = "Class",ylab = "V1", zlab = "V2", x.ticklabs=xlabs)
Also, how can I make the points linear or plane instead of dots.
Scatterplot3d offers "lines" and "vertical lines", for example:
scatterplot3d(x,y,z , type="l", lwd=5, pch=" ")
#or
scatterplot3d(x,y,z , type="h", lwd=5, pch=" ")

Related

plot(var()) displays two different plots, how do I merge them into one? Also having two y axis

> dput(head(inputData))
structure(list(Date = c("2018:07:00", "2018:06:00", "2018:05:00",
"2018:04:00", "2018:03:00", "2018:02:00"), IIP = c(125.8, 127.5,
129.7, 122.6, 140.3, 127.4), CPI = c(139.8, 138.5, 137.8, 137.1,
136.5, 136.4), `Term Spread` = c(1.580025, 1.89438, 2.020112,
1.899074, 1.470544, 1.776862), RealMoney = c(142713.9916, 140728.6495,
140032.2762, 139845.5215, 139816.4682, 139625.865), NSE50 = c(10991.15682,
10742.97381, 10664.44773, 10472.93333, 10232.61842, 10533.10526
), CallMoneyRate = c(6.161175, 6.10112, 5.912088, 5.902226, 5.949956,
5.925538), STCreditSpread = c(-0.4977, -0.3619, 0.4923, 0.1592,
0.3819, -0.1363)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
I want to make my autoregressive plot like this plot:
#------> importing all libraries
library(readr)
install.packages("lubridtae")
library("lubridate")
install.packages("forecast")
library('ggplot2')
library('fpp')
library('forecast')
library('tseries')
#--------->reading data
inputData <- read_csv("C:/Users/sanat/Downloads/exercise_1.csv")
#--------->calculating the lag=1 for NSE50
diff_NSE50<-(diff(inputData$NSE50, lag = 1, differences = 1)/lag(inputData$NSE50))
diff_RealM2<-(diff(inputData$RealMoney, lag = 1, differences = 1)/lag(inputData$RealMoney))
plot.ts(diff_NSE50)
#--------->
lm_fit = dynlm(IIP ~ CallMoneyRate + STCreditSpread + diff_NSE50 + diff_RealM2, data = inputData)
summary(lm_fit)
#--------->
inputData_ts = ts(inputData, frequency = 12, start = 2012)
#--------->area of my doubt is here
VAR_data <- window(ts.union(ts(inputData$IIP), ts(inputData$CallMoneyRate)))
VAR_est <- VAR(y = VAR_data, p = 12)
plot(VAR_est)
I want to my plots to get plotted together in same plot. How do I serparate the var() plots to two separate ones.
Current plot:
My dataset :
dataset
Okay, so this still needs some work, but it should set the right framework for you. I would look more into working with the ggplot2 for future.
Few extra packages needed, namely library(vars) and library(dynlm).
Starting from,
VAR_est <- VAR(y = VAR_data, p = 12)
Now we extract the values we want from the VAR_est object.
y <- as.numeric(VAR_est$y[,1])
z <- as.numeric(VAR_est$y[,2])
x <- 1:length(y)
## second data set on a very different scale
par(mar = c(5, 4, 4, 4) + 0.3) # Leave space for z axis
plot(x, y, type = "l") # first plot
par(new = TRUE)
plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(z)))
mtext("z", side=4, line=3)
I will leave you to add the dotted lines on etc...
Hint: Decompose the VAR_est object, for example, VAR_est$datamat, then see which bit of data corresponds to the part of the plot you want.
Used some of this

R: Changing height/location of column labels

Using the following code, the column labels for my barplot overlap with the plot itself (see image). Changing the margin appears only to affect the axis label, but not the column labels. Did a search but couldn't find this question. Suggestions? Thank you! (P.S.--I'm a beginner!)
library(colorspace)
df <- matrix(c(20, 14, 26, 18, 14, 4, 19, 21, 13, 1, 5, 4), ncol = 4, byrow = TRUE)
rownames(df) <- c("Character", "Tree", "Distance")
colnames(df) <- c("nrITS", "trnH-\npsbA", "matK", "rbcL")
graph.dat <- as.table(df)
italic_latin2 <- c(expression(atop(italic("nrITS"), (104))),
expression(atop(italic("trnH-\npsbA"), (82))),
expression(atop(italic("matK"), (42))),
expression(atop(italic("rbcL"), (28))))
barplot(graph.dat, beside = TRUE, ylab = "Percent Identified",
xlab = "Locus", ylim = c(0, 30), col = rainbow_hcl(3),
names.arg = italic_latin2)
Look at where the ?barplot function arguments get sent by the ... parameter values. The ?axis page says there is a padj parameter to adjust vertical label positioning, so perhaps:
italic_latin2 <- expression( atop(italic("nrITS"), (104)),
atop(italic("trnH-\npsbA"), (82)),
atop(italic("matK"), (42)),
atop(italic("rbcL"), (28)) )
barplot(graph.dat, beside = TRUE, ylab = "Percent Identified",
xlab = "Locus", ylim = c(0, 30), col = rainbow_hcl(3),
names.arg = italic_latin2, padj=0.8)
Notice that I simplified the expression vector code as well. The arguments to expression are adequately separated by commas.

Conver 3D scatter plots to 3D linear plots and separate based on the colours

I have been struggling to plot a 3D chart in R for a while. I think I am very close to what I want. I have asked a question before. What I need to know now is only how to convert the scatterplots with dots to linear. I mean if only I can connect the points it is great for me. What I have now looks like below:
I need to connect the points which has a better view in 3D. I need a separate line for each color and I want to add legend to the chart.
I have defined my data as:
df <- data.frame(a1 = c(489.4, 505.8, 525.8, 550.2, 576.6),
a2 = c(197.8, 301, 389.8, 502, 571.2),
b1 = c(546.8, 552.6, 558.4, 566.4, 575),
b2 = c(287.2, 305.8, 305.2, 334.4, 348.6), c1 = c(599.6, 611.4,
623.6, 658, 657.4), c2 = c(318.8, 423.2, 510.8, 662.4, 656),
d1 = c(616, 606.8, 600.2, 595.6, 595),
d2 = c(242.4, 292.8, 329.2, 378, 397.2),
e1 = c(582.4, 580, 579, 579, 579),
e2 = c(214, 255.4, 281.8, 303.8, 353.8))
colnames(df) <- rep(c("V1", "V2"), 5)
df.new <- rbind(df[, c(1, 2)],df[, c(3, 4)],df[, c(5, 6)],
df[, c(7, 8)],df[, c(9, 10)])
df.new$Group <- factor(rep(c("a","b","c","d","e"), each = 5))
df.new$Class <- rep(c(1:5), 5)
x=df.new$Class
y=V1
z=V2
Below is my code:
library(scatterplot3d) #colors
colors <- c("#999999", "#E69F00", "#56B4E9","#1B9E77", "#D95F02")
colors <- colors[as.numeric(df.new$Group)]#Others
xlabs <- c("[7,9]", "[10,12]", "[16,18]", "[19,21]", "[22,24]")
scatterplot3d(x,y,z, pch = 16, color=colors,main="Title",xlab
="Intervals",ylab = "", zlab = "Total time", x.ticklabs=xlabs)
text(8, 2.4, "c",cex = 1)
text(9, 2, "c",cex = 1)
I really appreciate if someone can help me about this issue that I have been struggling. I know there is a type=1 but it makes just one unified plot.
Try this:
sd<-scatterplot3d(x,y,z, pch = rep(16:12, each=5), color=colors,main="Title",xlab
="Intervals",ylab = "", zlab = "Total time", x.ticklabs=xlabs)
sd$points3d(x[1:5],y[1:5],z[1:5], col="purple", type="l")
sd$points3d(x[6:10],y[6:10],z[6:10], col="orange", type="l")
sd$points3d(x[11:15],y[11:15],z[11:15], col="blue", type="l")
sd$points3d(x[16:20],y[16:20],z[16:20], col="green", type="l")
sd$points3d(x[21:25],y[21:25],z[21:25], col="red", type="l")
legend("right", legend = levels(df.new$Group), col= levels(as.factor(colors)),pch = rep(16:12, each=1))

How to add a trendline to a two variables, two y-axes plot in r?

I have this dataset
structure(list(Year = 1988:2012, A = c(1415.6, 1345.3, 1321.7,
1234.5, 1567.8, 1476.6, 1610.1, 1422.6, 1209.1, 1249.3, 1377.5,
1525.7, 1683.7, 1500.1, 1565.3, 1737.4, 1321, 1477.8, 1642, 1608.1,
1427.8, 1608.2, 1404.4, 1688.3, 1795.4), B = c(76, 359, 299,
215, 177, 3112, 12047, 26307, 27173, 6514, 4190, 1776, 1708,
1335, 1012, 8170, 4306, 3716, 23531.986, 34803.012, 22758.7645,
29140.16304, 36369.3619, 56975.62256, 33516.95628)), .Names = c("Year",
"A", "B"), class = "data.frame", row.names = c(NA, -25L))
and I created this plot with the twoord.plot function:
install.packages("plotrix")
library(plotrix)
twoord.plot(Example$Year, Example$B, Example$Year, Example$A, xlim = range(1988:2012))
abline(lm(B ~ Year, data = Example), col="black")
abline(lm(A ~ Year, data = Example), col="red")
How should I tell to the second trendline (the red one) that it belongs to the righ hand y-axis? Is it possible to do it in r?
I guess R knows only one scale for x and one for y. If you have a look into the function twoord.plot you can see that it adds the right hand plot with a scaling-offest operation :
points(rx, ry * ymult + yoff, col = rcol, pch = rpch,
type = type[2], ...)
my guess is that you have to do the same to add extra lines. Just selecting some lines into the function, it should be (ly = Example$B and ry = Example$A) :
lylim <- range(ly, na.rm = TRUE)
rylim <- range(ry, na.rm = TRUE)
ymult <- diff(lylim)/diff(rylim)
yoff <- lylim[1] - rylim[1] * ymult
coef = lm(A ~ Year, data = Example)$coefficients
abline(a = coef[1]*ymult + yoff, b = coef[2]*ymult, col="red")

R: How to add highlighted angle lines in polar plots?

Please consider the following sample polar plot:
library(plotrix)
testlen <- c(rnorm(36)*2 + 5)
testpos <- seq(0, 350, by = 10)
polar.plot(testlen, testpos, main = "Test Polar Plot",
lwd = 3, line.col = 4, rp.type = "s")
I would like to add lines at angles 30 and 330 as well as 150 and 210 (from the center to the outside). I experimented with the line function but could not get it to work.
The calculations for exact placement are a bit goofy but using your test data
set.seed(15)
testlen<-c(rnorm(36)*2+5)
testpos<-seq(0,350,by=10)
polar.plot(testlen,testpos,main="Test Polar Plot",
lwd=3,line.col=4,rp.type="s")
You can add lines at 20,150,210,300 with
add.line <- c(30,330, 150,210)/360*2*pi
maxlength <- max(pretty(range(testlen)))-min(testlen)
segments(0, 0, cos(add.line) * maxlength, sin(add.line) * maxlength,
col = "red")
And that makes the following plot
You can just use the rp.type = "r" argument and add = TRUE. So, something like
library(plotrix)
set.seed(1)
testlen <- c(rnorm(36)*2 + 5)
testpos <- seq(0,350, by = 10)
polar.plot(testlen, testpos, main = "Test Polar Plot",
lwd = 3, line.col = 4, rp.type = "s")
followed by
pos <- c(30, 330, 150, 210)
len <- c(10, 10, 10, 10)
polar.plot(lengths = len, polar.pos = pos,
radial.lim = c(0, 15),
lwd = 2, line.col = 2, rp.type = "r", add = TRUE)
yields your desired output.

Resources