plotting 3d graph by stacking 2d contour plots in R [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have got two different variables x and y that are both functions of variable z. I have multiple contour plots of x vs y at different z values. What I want to do is put these graph slices together along z axis to get a 3D-ish graph.
I searched for packages that lets me do this in R and all I could find was contourrslice in Matlab. Contourslice is exactly what I want but in R.
Is there a package/ function in R like contourslice or any other ways I could go about doing this?
EDIT: Here's a dummy data. For different values of z the heat map changes. I have these several 2D plots, for values of z from -1 to 15. I want to put these 2D plots together along z axis to get a 3D ish figure to see how the red area is displaced.
#Create range of values for x and y
x<- c(11,25)
y<- c(1,5)
length<-10
x_ran <- as.matrix(seq(x[1],x[2], len=length))
y_ran <- as.matrix(seq(y[1],y[2], len=length))
#initialise matrix
x_mat<- x_ran[,rep(1,length)]
y_mat<- t(y_ran[,rep(1,length)])
#Third variable z
z<- -1
#z<- 1
#z<-2
#z<-3
#R and C, funcition of z
R <- x_mat*z
C<-z-y_mat
toget<- R/C
image(toget, xlab="R", ylab="C")

Related

Coding a plot with functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I've probably referenced the entire internet trying to make this problem work, and haven't. However, I found stack overflow. Like I said I've been learning for not even 2 weeks yet.
So this is the problem
Let
f(x)=sqrt((x^3+3x^2+1)/(x^4+5x^3+7x+9))
(x ≥ 0)
(a) Draw a line graph of (x, f(x)) for 0 ≤ x ≤ 10 with increments of 0.01
(b) Find numerically the maximum value of f(x) and the maximizer x (report x to the
second decimal place. For instance, x = 1.23)
So I'm basically been saying x=x and y= the sqrt....., and then I write plot(x,y,type="l") and usually it just doesn't even work.
Also how do I do the increment part. I'm sorry for lack of explanation, but I have no idea what most of this means.
First thing to do would be to define the function:
equation <- function(x){
sqrt((x^3+3*x^2+1)/(x^4+5*x^3+7*x+9))
}
Then, define the values you want to apply the function to, and store them in vector input
input<-seq(0,10,0.01)
Apply the equation function to input, and store the values in vector results
results<-sapply(input,function)
Produce a line plot:
plot(input,results,type="l")
Print the value of x which maximises f(x)
maxx<-input[which.max(results)]
maxx
I would suggest a ggplot2 approach. First you have to create a random x variable and then compute y. I will add the code for that variables an the plot.
In the case of finding the maximum of f(x) you must know calculus or you can use a visual approach. Here the code:
library(ggplot2)
library(dplyr)
set.seed(123)
First we create a random variable x with the limits you mentioned:
#Data
x <- runif(100,0,10)
Now, we save the variable in a dataframe and compute y:
#Allocate data in a dataframe
df <- data.frame(x=x)
#Compute variable
df$y <- sqrt(((df$x^3)+3*((df$x)^2)+1)/((df$x^4)+5*(df$x)^3+7*(df$x)+9))
Finally, we plot:
#Plot
ggplot(df,aes(x=x,y=y))+
geom_point()+
scale_x_continuous(limits = c(0,10))
Output:
Values for x are randomly generated, if you have real values for x you should use those values.

Calculate a distance between two points having lat, long and elevation in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Is there a package which allows to compute the spatial distance between two points taking into account the elevation. So for each point, we would have latitude, longitude and elevation. So far, I had to write the following function:
library(geosphere)
distance3D <- function (point1, point2) {
planiDist <- distm(point1[1:2], point2[1:2])
altiDist <- point2[3] - point1[3]
dist3D <- sqrt(planiDist^2+altiDist^2)
return(dist3D)
}
I was just wondering if one function existed in one of the R packages.
The haversine great circle distance might be what you are looking for.
library(geosphere)
distHaversine(p1, p2, r=6378137)
#p1, p2-longitude/latitude of point(s). Can be a vector of two numbers,
a matrix of 2 columns (first one is longitude, second is latitude)
or a SpatialPoints* object
#r-radius of the earth; default = 6378137 m

Creating a Polygon in r -- without connected vertices [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to create a polygon out of this line graph -- is it possible to do without having connected vertices? If so, how do I change the polygon code to do so?
I'm assuming the reason you what to create a polygon is to shade or color within its boundaries. If I'm wrong about that assumption then you really should put more effort into making your questions explicit. The trick is to connect at the ends of those lines by putting them both in one vectors, at the same time as reversing the X and Y for one of the series.
x <- 1:100
y1=6 + rnorm(100)
y2 = rnorm(100)
plot(x, y1, ylim=c(-3,10) )
?polygon
polygon(x= c( x, rev(x) ), y=c(y1,rev(y2) ), col="red")

Dataset with NaN values for a filled contour plot with R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I cannot get my data plotted properly with R. I got measurements from a football field and I did not fill measurements for each grid.
Here is my dataset contour_map_R.csv at https://db.tt/1L7cxilB
It looks like this using image function to plot it.
Any one can provide an example to create a filled contour plot?
Thanks a lot!
As stated in the comments you need to have complete data before you can calculate contours. Therefore you have to interpolate or replace your missing values in some way that makes sense in your case. I've provided a couple of options below, but you'd need to come up with rationale for using one method over another, and whether a more sophisticated geostatistical approach might be warranted. Furthermore you could interpolate to a finer grid than you currently have as well to produce a smoother result (at the expense of potentially making up data).
d <- read.csv("contour_map_R.csv")
library(raster)
r <- raster(as.matrix(d))
contour(r)
v <- getValues(r)
xy <- xyFromCell(r, 1:ncell(r))
## Interpolate using a thin-plate spline:
library(fields)
tps <- Tps(xy, v)
tp <- interpolate(r, tps)
plot(tp)
contour(tp, add=T)
## Alternatively, interpolate using nearest idw():
library(gstat)
dxy <- data.frame(x=xy[,1], y=xy[,2], v)
dxy <- dxy[complete.cases(dxy),]
id <- gstat(formula = v~1, locations = ~x+y, data=dxy)
ip <- interpolate(r, id)
plot(ip)
contour(ip, nlevels=5, add=T)
If that's what you were looking for you can get filled contours by using the filledContour() function on the interpolated rasters (tp or ip).

Logical Venn Diagrams [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have this question :
Unfortunately, my book didn't provide definition and how to draw/read a logic venn diagram, i have to look at the internet but couldn't find any understandable explanation
From what i understand, the logics for the three diagrams are :
X AND NOT (Y OR Z)
NOT X AND (Y OR Z)
Y AND NOT (X OR Z)
Am i right ? Please correct me if i'm wrong
By the ways, does the question mean to combine all three diagrams with OR operation, like :
1 OR 2 OR 3
Any help is greatly appreciated !
I could not understand the question correctly. Here's the logic for the three Venn Diagrams:
1. Your logic for the first Venn Diagram is correct: X AND NOT (Y OR Z)
2. Your logic for the second Venn Diagram is slightly incorrect, if you look at the Venn Diagram: It is simply NOT (X) (Everything except X).
3. In this case as well, the logic is simply Y.
Now if we perform OR operation on these three:
(X AND NOT (Y OR Z)) + NOT(X) + Y
= X AND NOT(Y) AND NOT(Z) OR NOT(X) OR Y [By de Morgan's law]
which matches option c.
NOTE: Your second and third logics would have been correct if X, Y and Z were the only three regions in the diagram. However there is also a region outside all of them - bounded by the box. You might want to look up on the internet about basics of Venn Diagram, it's one of the easiest things to learn IMO!

Resources