I tried to plot two semivariogram in one plot but unfortunately it does not work.
I guess the solution is pretty easy but I am at the end of my latin.
This ist the code of the variograms I want to put in one plot together:
variog_iso_a1 <- fit.variogram( emp_variog_iso_a1,
vgm( psill = 2000,
model = "Sph",
range = 200,
nugget = 500))
plot(emp_variog_iso_a1, variog_iso_a1, as.table=TRUE, main = "Acker H1 C_org", plot.numbers=T)
variog_iso_a2 <- fit.variogram( emp_variog_iso_a2,
vgm( psill = 2000,
model = "Sph",
range = 200,
nugget = 500))
plot(emp_variog_iso_a2, variog_iso_a2, as.table=TRUE, main = "Acker H2 C_org", plot.numbers=T)
Secondly I would also like to plot two semivariogram in one plot but with a second y-axis on the right side because of the different values.
variog_iso_a1 <- fit.variogram( emp_variog_iso_a1,
vgm( psill = 2000,
model = "Sph",
range = 200,
nugget = 500))
plot(emp_variog_iso_a1, variog_iso_a1, as.table=TRUE, main = "Acker H1 C_org", plot.numbers=T)
variog_iso_l1 <- fit.variogram( emp_variog_iso_l1,
vgm( psill = 2000,
model = "Sph",
range = 200,
nugget = 500))
plot(emp_variog_iso_l1, variog_iso_l1, as.table=TRUE, main = "Acker H1 Lichtwert", plot.numbers=T)
I was only able two show the points of each variogram but not with the model fitted inside. This is the code I tried but does not work!
plot(emp_variog_iso_a1$dist, emp_variog_iso_a1$gamma, ylim=c(0,2500))
points(emp_variog_iso_a2$dist, emp_variog_iso_a2$gamma, col = "red", add=T, labels=emp_variog_iso_a2$np)
plot(emp_variog_iso_a1$dist, emp_variog_iso_a1$gamma, ylim=c(0,2500),main="Semivarianz Lichtwert und organische Substanz Horizont 1 Ackerland", ylab = "Semivarianz", xlab = "Distanz" )
par(new = TRUE)
plot(emp_variog_iso_l1$dist, emp_variog_iso_l1$gamma, col = "red", labels=emp_variog_iso_l1$np )
points(emp_variog_iso_l1$dist, emp_variog_iso_l1$gamma, col = "red", add=T, labels=emp_variog_iso_l1$np, yaxt = "n")
plot(emp_variog_iso_a2$dist, emp_variog_iso_a2$gamma, ylim=c(0,2500),main="Semivarianz Lichtwert und organische Substanz Horizont 2 Ackerland", ylab = "Semivarianz", xlab = "Distanz" )
points(emp_variog_iso_l2$dist, emp_variog_iso_l2$gamma, col = "red", add=T, labels=emp_variog_iso_l2$np)
Thanks for any help!!!
You can use variogramLine() for this:
VL=variogramLine(YourVariogramFit, maxdist = ...)
next, add it to your plot with lines()
lines(VL)
Related
To expand upon visualize a list of colors/palette in R I am trying to display a series of custom colour palettes in R in a single figure. Is there a way that I can expand on one of the methods listed in the link to display the list of palettes below:
convert_coolers <- function(coolers_string){
strsplit(coolers_string, split = ", ")[[1]]
}
# diverging
storm_panels <- convert_coolers("#001219, #005f73, #0a9396, #94d2bd, #e9d8a6, #ee9b00, #ca6702, #bb3e03, #ae2012, #9b2226")
harry_tipper <- convert_coolers("#f72585, #b5179e, #7209b7, #560bad, #480ca8, #3a0ca3, #3f37c9, #4361ee, #4895ef, #4cc9f0")
firepit <- convert_coolers("#03071e, #370617, #6a040f, #9d0208, #d00000, #dc2f02, #e85d04, #f48c06, #faa307, #ffba08")
# sequences
the_deep <- convert_coolers("#03045e, #023e8a, #0077b6, #0096c7, #00b4d8, #48cae4, #90e0ef, #ade8f4, #caf0f8")
earth <- convert_coolers("#ede0d4, #e6ccb2, #ddb892, #b08968, #7f5539, #9c6644")
# categorical
pastal_rainbow <- convert_coolers("#ff595e, #ffca3a, #8ac926, #1982c4, #6a4c93")
fisherman <- convert_coolers("#353535, #3c6e71, #ffffff, #d9d9d9, #284b63")
in a figure resembling that displayed by RColorBrewer::display.brewer.all()? i.e. with palettes stacked as horizontal bars labelled to the left with the palette title.
I have been trying to dissect the method out from the RColorBrewer function but am finding that it depends too much on internal variables for me to understand what is going on.
I achieved what I set out to do by modifying RColorBrewer::display.brewer.all
Following directly on from the code in the question:
display_custom_palettes <- function(palette_list, palette_names){
nr <- length(palette_list)
nc <- max(lengths(palette_list))
ylim <- c(0, nr)
oldpar <- par(mgp = c(2, 0.25, 0))
on.exit(par(oldpar))
plot(1, 1, xlim = c(0, nc), ylim = ylim, type = "n", axes = FALSE,
bty = "n", xlab = "", ylab = "")
for (i in 1:nr) {
nj <- length(palette_list[[i]])
shadi <- palette_list[[i]]
rect(xleft = 0:(nj - 1), ybottom = i - 1, xright = 1:nj,
ytop = i - 0.2, col = shadi, border = "light grey")
}
text(rep(-0.1, nr), (1:nr) - 0.6, labels = palette_names, xpd = TRUE,
adj = 1)
}
plot.new()
palette_list <- list(storm_panels, harry_tipper, firepit, the_deep, earth, pastal_rainbow, fisherman)
palette_names <- c("storm panels", "harry tipper", "firepit", "the deep", "earth", "rainbow", "fisherman")
display_custom_palettes(palette_list, palette_names)
The scatterplot3D function seems to be plotting incorrectly and I am unsure about why. For example, the following commands should yield identical plots but they do not. I also providing reproducible code to create the data structures below. I guess it is not correctly processing my input?
install.packages("scatterplot3d")
library("scatterplot3d")
cent = array(dim=c(4,3))
cll = c("Factor1", "Factor2", "Factor3")
colnames(cent) = cll
cent[1,] = c(-0.25320707, -0.5878291, -0.4522262)
cent[2,] = c(2.49368231, 0.5911989, -0.3728652)
cent[3,] = c(-0.02927063, -0.2627355, 1.6147719)
cent[4,] = c(-0.63391974, 1.0109955, -0.1542808)
new.cent = array(dim=c(4,3))
colnames(new.cent) = cll
new.cent[1,] = c(2.1572533, 0.4985594, -0.1989068)
new.cent[2,] = c(-0.1362396, -0.4134629, 1.2677813)
new.cent[3,] = c(-0.2566698, -0.6602819, -0.5245323)
new.cent[4,] = c(-0.5847768, 0.7672588, -0.1918044)
Now I try to plot
plot.new()
scatterplot3d(new.cent, pch = 10)
points(cent, pch = 3)
plot of new.cent with cent added as points in different format
plot.new()
scatterplot3d(cent, pch = 3)
points(new.cent, pch = 10)
plot of cent with new.cent added as points in different format
The above points don't seem correct in any case... Moreover, if I try to add a single point as in "points(cent[1,])" it adds three points which is also indicative of the malfunction.
Please refer to linked manual, how to add points3d to the plot. Also, to compare plots, please make sure they axes limits are the same.
library("scatterplot3d")
cent = array(dim=c(4,3))
cll = c("Factor1", "Factor2", "Factor3")
colnames(cent) = cll
cent[1,] = c(-0.25320707, -0.5878291, -0.4522262)
cent[2,] = c(2.49368231, 0.5911989, -0.3728652)
cent[3,] = c(-0.02927063, -0.2627355, 1.6147719)
cent[4,] = c(-0.63391974, 1.0109955, -0.1542808)
new.cent = array(dim=c(4,3))
colnames(new.cent) = cll
new.cent[1,] = c(2.1572533, 0.4985594, -0.1989068)
new.cent[2,] = c(-0.1362396, -0.4134629, 1.2677813)
new.cent[3,] = c(-0.2566698, -0.6602819, -0.5245323)
new.cent[4,] = c(-0.5847768, 0.7672588, -0.1918044)
plot.new()
a <- scatterplot3d(new.cent, pch = 10, xlim = c(-1,2.5), ylim = c(-1,1.5), zlim = c(-1,2))
a$points3d(cent, pch = 3)
b <- scatterplot3d(cent, pch = 3, xlim = c(-1,2.5), ylim = c(-1,1.5), zlim = c(-1,2))
b$points3d(new.cent, pch = 10)
Created on 2022-01-27 by the reprex package (v2.0.1)
I would like to plot several forecasts on the same plot in different colours, however, the scale is off.
I'm open to any other methods.
reproducible example:
require(forecast)
# MAKING DATA
data <- c(3.86000, 19.55810, 19.51091, 20.74048, 20.71333, 29.04191, 30.28864, 25.64300, 23.33368, 23.70870 , 26.16600 ,27.61286 , 27.88409 , 28.41400 , 24.81957 , 24.60952, 27.49857, 32.08000 , 29.98000, 27.49000 , 237.26150, 266.35478, 338.30000, 377.69476, 528.65905, 780.00000 )
a.ts <- ts(data,start=c(2005,1),frequency=12)
# FORECASTS
arima011_css =stats::arima(x = a.ts, order = c(0, 1, 1), method = "CSS") # css estimate
arima011_forecast = forecast(arima011_css, h=10, level=c(99.5))
arima321_css =stats::arima(x = a.ts, order = c(3, 2, 1), method = "CSS") # css estimate
arima321_forecast = forecast(arima321_css, h=10, level=c(99.5))
# MY ATTEMPT AT PLOTS
plot(arima321_forecast)
par(new=T)
plot(arima011_forecast)
Here is something similar to #jay.sf but using ggplot2.
library(ggplot2)
autoplot(a.ts) +
autolayer(arima011_forecast, series = "ARIMA(0,1,1)", alpha = 0.5) +
autolayer(arima321_forecast, series = "ARIMA(3,2,1)", alpha = 0.5) +
guides(colour = guide_legend("Model"))
Created on 2020-05-19 by the reprex package (v0.3.0)
You could do a manual plot using a sequence of dates.
rn <- format(seq.Date(as.Date("2005-01-01"), by="months", length.out=12*3), "%Y.%m")
Your ARIMAs you'll need as.matrix form.
arima321_mat <- as.matrix(as.data.frame(arima321_forecast))
arima011_mat <- as.matrix(as.data.frame(arima011_forecast))
Some colors with different alpha=.
col.1 <- rainbow(2, ,.7)
col.2 <- rainbow(2, ,.7, alpha=.2)
For the CIs use polygon.
plot(data, type="l", xlim=c(1, length(rn)), ylim=c(0, 3500), xaxt="n", main="Forecasts")
axis(1, axTicks(1), labels=F)
mtext(rn[(seq(rn)-1) %% 5 == 0], 1, 1, at=axTicks(1))
lines((length(data)+1):length(rn), arima321_mat[,1], col=col.1[1], lwd=2)
polygon(c(27:36, 36:27), c(arima321_mat[,2], rev(arima321_mat[,3])), col=col.2[1],
border=NA)
lines((length(data)+1):length(rn), arima011_mat[,1], col=col.1[2], lwd=3)
polygon(c(27:36, 36:27), c(arima011_mat[,2], rev(arima011_mat[,3])), col=col.2[2],
border=NA)
legend("topleft", legend=c("ARIMA(3,2,1)", "ARIMA(0,1,1)"), col=col.1, lwd=2, cex=.9)
Edit: To avoid the repetition of lines and polygon calls, you may unite them using Map.
mats <- list(arima321_mat, arima011_mat) ## put matrices into list
plot(.)
axis(.)
mtext(.)
Map(function(i) {
lines((length(data)+1):length(rn), mats[[i]][,1], col=col.1[i], lwd=2)
polygon(c(27:36, 36:27), c(mats[[i]][,2], rev(mats[[i]][,3])), col=col.2[i], border=NA)
}, 1:2)
legend(.)
require(forecast)
data <- c(3.86000, 19.55810, 19.51091, 20.74048, 20.71333, 29.04191, 30.28864, 25.64300, 23.33368, 23.70870 , 26.16600 ,27.61286 , 27.88409 , 28.41400 , 24.81957 , 24.60952, 27.49857, 32.08000 , 29.98000, 27.49000 , 237.26150, 266.35478, 338.30000, 377.69476, 528.65905, 780.00000 )
a.ts <- ts(data,start=c(2005,1),frequency=12)
arima011_css =stats::arima(x = a.ts, order = c(0, 1, 1), method = "CSS") # css estimate
arima011_forecast = predict(arima011_css, n.ahead = 2)$pred
arima321_css =stats::arima(x = a.ts, order = c(3, 2, 1), method = "CSS") # css estimate
arima321_forecast = predict(arima321_css, n.ahead = 2)$pred
plot(a.ts, type = "o", xlim = c(2005, 2007.5) , ylim = c(-1, 1200) , ylab = "price" ,main = "2 month Forecast")
range = c(2007+(3/12), 2007+(4/12)) # adding the dates for the prediction
lines(y = arima011_forecast , x = range , type = "o", col = "red")
lines(y = arima321_forecast, x = range , type = "o", col = "blue")
I have a data like this
df<- structure(list(How = c(3.1e-05, 0.000114, 0.000417, 0.00153,
0.00561, 0.0206, 0.0754, 0.277, 1.01, 3.72), Where = c(1, 0.948118156866697,
0.920303987764611, 1.03610743904536, 1.08332987533419, 0.960086785898477,
0.765642506120658, 0.572520170014998, 0.375835106792894, 0.254180720963181
)), class = "data.frame", row.names = c(NA, -10L))
library(drc)
I make my model like this
fit <- drm(formula = Where ~ How, data = df,
fct = LL.4(names=c("Slope","Lower Limit","Upper Limit", "EC50")))
Then I plot it like this
plot(NULL, xlim = c(0.000001, 4), ylim = c(0.01, 1.2),log = "x")
points(df$How, df$Where, pch = 20)
x1 = seq(0.000001, 4, by=0.0001)
y1 = coef(fit)[3] + (coef(fit)[2] - coef(fit)[3])/(1+(x1/coef(fit)[4])^((-1)*coef(fit)[1]))
lines(x1,y1)
Now I want to be able to print the following information inside the figure
max(df$How)
min(df$How)
coef(fit)[2]
coef(fit)[3]
(-1)*coef(fit)[1]
coef(fit)[4]
I tried to do it like this
text(labels = bquote(FirstT~"="~.(round(max(df$How)))))
text(labels = bquote(SecondT~"="~.(round(min(df$How))))
text(labels = bquote(A[min]~"="~.(round(coef(fit)[2]))))
text(labels = bquote(A[max]~"="~.(coef(fit)[3]))))
text(labels = paste0("Slope = ", round((-1)*coef(fit)[1])))
which of course does not work. I am more into an automatic way to find a place in right left corner of the figure that print these info
In the code below, we get the plot area coordinate ranges with par("usr") and then use those and the data point locations to automatically place the labels in the desired locations.
# Reduce margins
par(mar=c(5,4,0.5,0.5))
# Get extreme coordinates of plot area
p = par("usr")
p[1:2] = 10^p[1:2] # Because xscale is logged
text(max(df$How), df$Where[which.max(df$How)],
labels = bquote(FirstT~"="~.(round(max(df$How)))), pos=1)
text(min(df$How), df$Where[which.min(df$How)],
labels = bquote(SecondT~"="~.(round(min(df$How)))), pos=1)
text(1.1*p[1], p[3] + 0.02*diff(p[3:4]),
labels = bquote(A[min]~"="~.(round(coef(fit)[2]))), adj=c(0,0))
> 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