Error in identify(x, y, labels = name, plot = TRUE) - r

I trying to use the function identify() on R, but it's not working.
Do you know if there is any package that I'm missing?
It doesn't work not even for a simple code:
x = 1:10
y = x^2
name = letters[1:10]
plot(x, y)
identify(x, y, labels = name, plot=TRUE, n = 2)

This way it works: it is the same code: With n=2 you have to click on two points:
x = 1:10
y = x^2
name = letters[1:10]
plot(x, y)
identify(x, y, labels = name, plot=TRUE, n = 2)

Related

How to set a logarithmic scale across multiple ggplot2 contour plots?

I am attempting to create three contour plots, each illustrating the following function applied to two input vectors and a fixed alpha:
alphas <- c(1, 5, 25)
x_vals <- seq(0, 25, length.out = 100)
y_vals <- seq(0, 50, length.out = 100)
my_function <- function(x, y, alpha) {
z <- (1 / (x + alpha)) * (1 / (y + alpha))
}
for each alpha in the vector alphas, I am creating a contour plot of z values—relative to the minimal z value—over x and y axes.
I do so with the following code (probably not best practices; I'm still learning the basics with R):
plots <- list()
for(i in seq_along(alphas)) {
z_table <- sapply(x_vals, my_function, y = y_vals, alpha = alphas[i])
x <- rep(x_vals, each = 100)
y <- rep(y_vals, 100)
z <- unlist(flatten(list(z_table)))
z_rel <- z / min(z)
d <- data.frame(cbind(x, y, z_rel))
plots[[i]] <- ggplot(data = d, aes(x = x, y = y, z = z_rel)) +
geom_contour_filled()
}
When alpha = 1:
When alpha = 25:
I want to display these plots in one grouping using ggarrange(), with one logarithmic color scale (as relative z varies so much from plot to plot). Is there a way to do this?
You can build a data frame with all the data for all alphas combined, with a column indicating the alpha, so you can facet your graph:
I basically removed the plot[[i]] part, and stacked up the d's created in the former loop:
d = numeric()
for(i in seq_along(alphas)) {
z_table <- sapply(x_vals, my_function, y = y_vals, alpha = alphas[i])
x <- rep(x_vals, each = 100)
y <- rep(y_vals, 100)
z <- unlist(flatten(list(z_table)))
z_rel <- z / min(z)
d <- rbind(d, cbind(x, y, z_rel))}
d = as.data.frame(d)
Then we create the alphas column:
d$alpha = factor(paste("alpha =", alphas[rep(1:3, each=nrow(d)/length(alphas))]),
levels = paste("alpha =", alphas[1:3]))
Then build the log scale inside the contour:
ggplot(data = d, aes(x = x, y = y, z = z_rel)) +
geom_contour_filled(breaks=round(exp(seq(log(1), log(1400), length = 14)),1)) +
facet_wrap(~alpha)
Output:

Problem with plotting 3D elliptic paraboloid

I'm looking to plot 3D functions using R. For example, take the elliptic paraboloid given by f(x,y) = (𝑥−2𝑦−1)^2 + (3𝑥+𝑦−2)^2. Here's what I've tried:
require(lattice)
x <- seq(-10, 10, by=0.5)
y <- seq(-10, 10, by=0.5)
g <- expand.grid(x = x, y = y)
g$z <- (x-2*y-1)^2 + (3*x-y-2)^2
wireframe(z ~ x * y, g, drape = TRUE,
aspect = c(1,1), colorkey = TRUE)`
And here's the output
However, here's the "true" graph of f:
I've tried changing the definitions of x and y, to no avail. I've also tried the curve3d() function from the emdbook package. It looks even worse.
You multiplied by the wrong x and y. You need to use the ones inside g:
g$z <- with(g, (x-2*y-1)^2 + (3*x-y-2)^2)
wireframe(z ~ x * y, g, drape = TRUE,
aspect = c(1,1), colorkey = TRUE)

How to plot multivariate function in R

I am trying to plot the following function:
This is what I have currently tried:
curve(7*x*y/( e^(x^2+y^2)))
But I get the following error:
One way to plot is using the contour() function. Also, as #Sang won kim noted, exp() is the function for e^(...)
x <- seq(from = 0.01, to = 2.1, by = 0.01)
y <- x
multi_var_fx <- function (x, y) {
7 * x * y / (exp(x^2 + y^2))
}
z <- outer(x, y, multi_var_fx)
contour(x, y, z, xlab = 'x', ylab = 'y')
Created on 2019-10-27 by the reprex package (v0.3.0)
Your e means exponential function. In the r, exponential function code is exp(). So you can revise this code.
curve(7*x*y/(exp(x^2+y^2)))
You can create a contour plot like this:
library(tidyverse)
tibble(x = seq(0, 10, 0.1), # define the drawing grid
y = seq(0, 10, 0.1)
) %>%
cross_df() %>% # create all possible combinations of x and y
mutate(z = 7*x*y/(exp(x^2+y^2)) ) %>% # add your function
ggplot(aes(x = x, y = y, z = z)) + # create the plot
geom_contour()

Plotly Contour plot behaviour

I want to understand how to correctly do contour plots with plotly. In the code below I have x,y,z so I interpolate to have a more defined range with the interp in akima package. I plot the result first with plotly then with filled.contour. The result of the plotly is wrong but I like more its aesthetics in the filled contour the results is correct.
What am I doing wrong with plotly?
require(akima)
require(plotly)
x = rand(15,1)
y = rand(15,1)
z = rand(15,1)
a = interp(x, y, z)
p = plot_ly(x = a$x,
y = a$y,
z = a$z,
type = "contour")
p
filled.contour(a$x,a$y,a$z)
Plotly expects a little bit different matrix arrangement. Here is a fix:
require(akima)
require(plotly)
library(pracma)
set.seed(1)
x = rand(15,1)
y = rand(15,1)
z = rand(15,1)
a = interp(x, y, z)
plot_ly(x = a$x,
y = a$y,
z = matrix(a$z, nrow = length(a$y), byrow = TRUE),
type = "contour")
filled.contour(a$x,a$y,a$z)
without the matrix rearrangement:
plot_ly(x = a$x,
y = a$y,
z = a$z,
type = "contour")

Line instead of Dot (R) Not so easy

If you could help me it would be great :
So i'm doing a double curve (SDT) graph, and i have a bit of a problem : here my graph :
First time I have this problem ... Really have no clue how to solve it, well I just think my data is not ordered but how can I order it easily ?
Here's me code (but really nothing special) :
x = TDSindice2$Hit
mean = mean(x)
sd = sd(x)
y = dnorm(x,mean,sd)
plot(x,y, col = "red")
x = TDSindice2$Fa
mean = mean(x)
sd = sd(x)
y = dnorm(x,mean,sd)
par(new=TRUE)
plot(x,y ,type = "l", col ="blue")
Thanks for all :)
You need to order your data in terms of increasing values of x before plotting. For example:
set.seed(1)
x <- runif(50)
y <- 1.2 + (1.4 * x) + (-2.5 * x^2)
plot(x, y)
lines(x, y)
The order() function can be used to generate an index that when applied to a variable/object places the values of that object in the required order (increasing by default):
ord <- order(x)
plot(x[ord], [ord], type = "o")
But you'd be better off have x and y in the same object, a data frame, and just sort the rows of that:
dat <- data.frame(x = x, y = y)
ord <- with(dat, order(x))
plot(y ~ x, data = dat[ord, ], type = "o") ## or
## lines(y ~ x, data = dat[ord, ])
Note that order() is used to index the data hence we don't change the original ordering, we just permute the rows as we supply the object to the plot() function.

Resources