I'm using the wireframe function in order to obtain a 3d plot. Since I'm using some models in order to forecast, I want to plot observed and forecasted values in the sime wireframe plot, but I want to know if it's possible to change color from the begin of forecasting. This is the result I obtain using this code:
wireframe(grid$mxt~grid$ages*grid$years,
xlab=TeX("$x$"),ylab=TeX("$t$"),zlab=TeX("$log\\mu_x(t)"),
drape = TRUE,col="black",
col.regions = colorRampPalette(c("yellow", "red"))(100),
scales = list(arrows=FALSE, cex=0.8, col = "black", font = 1),
aspect=c(1,0.6))
What I want is to change the surface color from the 2011 years, in order to make understandable the plot. I attach the data. Thank you
Well, I think I partially solved. It seems that wireframe function does not plot different surface with different (x,y). So what you have to do is to extend the data, in order to have two different surface that have the same (x,y), but before a certain y one of them contains NA, and after the other one contains NA. I post the code I used and the results (well I have to change colours but thi is quite easy).
This is an age-year plot, with z equal to log-mortality rate for ages and years. The matrix mxt1 is of dimension (n, n1), while the matrix pred is of dimension (n, n2); y1 is a vector of dimension n1 (years of observed values) while y2 is of dimension n2 (years of predicted values).
grid<-expand.grid(list(ages=ages, years=c(y1,y2)))
grid<-rbind(grid,grid)
grid$mxt <- c(cbind(mxt1, matrix(nrow=n,ncol=n2)),
cbind(matrix(nrow=n,ncol=(n1-1)),mxt1[,n1],pred))
grid$group <- factor(c(rep("obs",n*n1+n*n2),rep("for",n*n1+n*n2)))
wireframe(mxt~ages*years,data=grid,
groups=group, col.groups=c("red","green"))
The trick in the second part of vector grid$mxt is for bind the two surface. The result is this.
Hope to have helped someone.
Related
I want to plot anomalies in a dataset with a different color. For that i generated random numbers, injected anomalies based on a condition, and them plotted them. But the plot that i am getting is wrong. Following is the code:
n = 1000
a = 25
mu = 0
sigma = 0.5
data = rnorm(n,mu,sigma)
n_data = sample(1:n,25,replace = FALSE)
p_data = sample(1:n,25,replace = FALSE)
plot(data)
points(data[n_data],col=2)
points(data[p_data],col=3)
But this gives me a wrong plot. It should show anomalous points distributed among the whole graph, but it shows a plot like this.
How can i plot the points correctly based on index?
here you plot your vector data without x specified so x is x1 = 1... xn = length(data)
just indicate the x corresponding and it will work
points(n_data, data[n_data],col=2)
points(p_data, data[p_data],col=3)
The problem is you do not have x coordinates for your random values, so the plot will simply give each value an index and treat that as your x-value. You have a total of 1000 points, but only 25 are colored in each of the colored points. If you were to take 1000 colored points they would be spread out just as much.
I am trying to smooth my data set, using kernel or loess smoothing method. But, They are all not clear or not what I want. Several questions are the followings.
My x data is "conc" and y data is "depth", which is ex. cm.
1) Kernel smooth
k <- kernel("daniell", 150)
plot(k)
K <- kernapply(conc, k)
plot(conc~depth)
lines(K, col = "red")
Here, my data is smoothed by frequency=150. This means that every data point is averaged by neighboring (right and left) 150 data points? What "daniell" means? I could not find what it means online.
2) Loess smooth
p<-qplot(depth, conc, data=total)
p1 <- p + geom_smooth(method = "loess", size = 1, level=0.95)
Here, what is the default of loess smooth function? If I want to smooth my data with frequency=150 like above case (moving average by every 150 data point), how can I modify this code?
3) To show y-axis with log scale, I put "log10(conc)", instead of "conc", and it worked. But, I cannot change the y-axis tick label. I tried to use "scale_y_log10(limits = c(1,1e3))" in my code to show axis tick labe like 10^0, 10^1, 10^2..., but did not work.
Please answer my questions. Thanks a lot for your help.
Sum
I have created a NMDS plot using the 'vegan' package, like this:
y=metaMDS(data,type="p").
plot(y)
Now I have this NMDS with a good spread of my points. However, I would like to add the graphics of the plot. I would like to give the points in the plot a different colour, depending on a categorical variable (the variable is called 'regio') in my dataset, which has two values (1 or 2).
Is this possible? And if so, how?
Best,
Koen
The easiest way is to use the grouping variable regio to index into a vector of colours you want to plot with. E.g., (untested as I don't have your data...)
colvec <- c("red","blue")
plot(y, type = "n")
points(y, display = "sites", col = colvec[data$regio])
## or
text(y, display = "sites", col = colvec[data$regio])
## depending on how you want to represent the sample scores
I recently came across the R-package beanplot and the offered possibility to plot the distribution of two subgroups in one single plot (special asymmetric beanplot). You find a description of the package in the Journal of Statistical Software and on the cran.r-project.org.
I produced an asymmetric beanplot using the following CODE:
library(psych)
library(beanplot)
var1 <-c(20,33,NA,39,NA,40,34,33,NA,38,NA,8,7,NA,NA,40,34,24,25,36,40,37,34,NA,35)
var2 <- c(1,0,1,1,1,0,1,0,1,NA,1,0,0,0,0,1,1,0,1,0,1,1,NA,0,1)
mydata<-data.frame(var1,var2)
table(mydata)
par(lend = 1, mai = c(0.8, 0.8, 0.5, 0.5))
beanplot(var1 ~ var2, data= mydata, side = "both",log="",
what=c(1,1,1,0), border = NA, col = list("black", c("grey", "white")))
legend("bottomleft", fill =c("black", "grey"), legend = c("no", "yes"))
The produced plot nicely shows the different shape of the two subgroups' distribution.
PROBLEM
The dependent variable is measured on a scale ranging from 7 to 40. However, the y-axis appears to go from -1 to +55.
It would be great if anyone could explain how the scale is modified, i.e. what is actually plotted here. Is there a way to plot the distribution by using the original scale?
Many many thanks!
beanplot uses density. The estimated density can give mass to areas past the range of the observed data. You could try this to get an idea of what density does - plot(density(1:2))and you should see that it's just taking an average of gaussian densities centered at the data points (note that you can use a different kernel as beanplot does allow you to specify a kernel parameter). How it chooses the variance for that gaussian is up to you but by default it looks like beanplot uses bw.SJ with the "dpi" method to choose the bandwidth.
You could use the cutmin and cutmax to control the range that beanplot actually plots over but this doesn't actually change the density estimate.
I have a simple data set with two columns of data- K and SwStr.
K = c(.259, .215, .224, .223, .262, .233)
SwStr = c(.130, .117, .117, .114, .113, .111)
I plotted the data using:
plot(res$K, res$SwStr)
I want to plot the result of a linear model, using SwStr to predict K. I try to do that using:
graphic<-lm(K~SwStr-1, data=res)
P=predict(graphic)
plot(res$K, res$SwStr)
lines(P, lty="dashed", col="green", lwd=3)
But when I do this, I don't get any line plotted. What am I doing wrong?
(1) You are inverting the axes of the original plot. If you want SwStr on the x axis and K on the y axis you need
plot(res$SwStr, res$K)
or
with(res,plot(K~SwStr))
If you check the actual values of the plotted points on the graph, this might be obvious (especially if K and SwStr have different magnitudes) ...
For lm fits you can also use abline(graphic,...)
edit: (2) You also have to realize that predict gives just the predicted y values, not the x values. So you want something like this:
K=c(.259, .215, .224, .223, .262, .233)
SwStr=c(.130, .117, .117, .114, .113, .111)
g <- lm(K~SwStr-1)
par(las=1,bty="l") ## my favourites
plot(K~SwStr)
P <- predict(g)
lines(SwStr,P)
Depending on the situation, you may also want to use the newdata argument to predict to specify a set of evenly spaced x values ...