Hi,

My issue is that I want to use geom_tile in order to plot the values of a function with 2 variables. But the filling is really strange. If you look at the plot, in the left-upper corner there is a line, and every point above that line get the same value (at about y > 10). The more funny thing, is that the true values arent equal (sorry for the long reprex), but even if I change the invtervalls, the position of this "bound" changes.

``````library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3

f0 <- function(x) x^2+x^2

f1 <- function(x) x + x*2 - 5

L <- Rsolnp::solnp(pars = c(0,0), fun = f0, eqfun = f1, eqB = 0)
#>
#> Iter: 1 fn: 6.2407    Pars:  1.38572 2.07858
#> Iter: 2 fn: 5.0000    Pars:  1.00000 2.00000
#> Iter: 3 fn: 5.0000    Pars:  1.00000 2.00000
#> solnp--> Completed in 3 iterations

interval.x <- c(-10,10)
interval.y <- c(-10,10)
f <- function(x) {
y <- f0(x)
if (f1(x) > 0) y <- NA
return(y)
}

df <- expand.grid(c(seq(from = interval.x, to = interval.x, length.out = 100), L\$pars),y = c(seq(from = interval.y, to = interval.y, length.out = 100), L\$pars)) %>% setNames(c("x", "y"))

z <- vector()
for (i in seq_along(df\$x)) {
z[i] <- f(as.numeric(df[i,]))
}
df\$z <- z

df <- df %>% transmute(
xmin = x - mean(unique(sort(df\$x)),unique(sort(df\$x))),
xmax = x + mean(unique(sort(df\$x)),unique(sort(df\$x))),
ymin = y - mean(unique(sort(df\$y)),unique(sort(df\$y))),
ymax = y + mean(unique(sort(df\$y)),unique(sort(df\$y))),
z = z
)

df <- df %>% filter(!is.na(z))

pp <- df %>% ggplot(aes(xmin = xmin, ymin = ymin, xmax = xmax, ymax = ymax, fill = z)) +
geom_vline(xintercept = L\$pars) +
geom_hline(yintercept = L\$pars) +
geom_rect(alpha = 0.3) + scale_fill_gradient(low = "blue", high = "red")
pp
`````` Created on 2020-09-12 by the reprex package (v0.3.0)

I thing the problem can be somewhere with the fill method, but I don't know how to solve it.
Marcell

A solution to how necessarily passes through what. For this it is helpful to simplify

``````suppressPackageStartupMessages({library(dplyr)
library(ggplot2)})

f0 <- function(x) x^2+x^2

f1 <- function(x) x + x*2 - 5

L <- Rsolnp::solnp(pars = c(0,0), fun = f0, eqfun = f1, eqB = 0)
#>
#> Iter: 1 fn: 6.2407    Pars:  1.38572 2.07858
#> Iter: 2 fn: 5.0000    Pars:  1.00000 2.00000
#> Iter: 3 fn: 5.0000    Pars:  1.00000 2.00000
#> solnp--> Completed in 3 iterations

interval.x <- c(-10,10)
interval.y <- c(-10,10)
f <- function(x) {
y <- f0(x)
if (f1(x) > 0) y <- NA
return(y)
}

dat <- expand.grid(c(seq(from = interval.x, to = interval.x, length.out = 100), L\$pars),y = c(seq(from = interval.y, to = interval.y, length.out = 100), L\$pars)) %>% setNames(c("x", "y"))

z <- vector()
for (i in seq_along(dat\$x)) {
z[i] <- f(as.numeric(dat[i,]))
}
dat\$z <- z

dat <- dat %>% transmute(
xmin = x - mean(unique(sort(dat\$x)),unique(sort(dat\$x))),
xmax = x + mean(unique(sort(dat\$x)),unique(sort(dat\$x))),
ymin = y - mean(unique(sort(dat\$y)),unique(sort(dat\$y))),
ymax = y + mean(unique(sort(dat\$y)),unique(sort(dat\$y))),
z = z
)

dat <- dat %>% filter(!is.na(z))

# dat is 6Kx5 data frame with 5 double columns; renamed because df
# is already in namespace globally as a function in base

# create bare plot, explicitly providing dat and aes(x,y)

p <- ggplot(dat)

# examine plots of xmin/ymin (skipping symmetric cases)

p1 <- p + geom_point(aes(xmin,xmax)) + theme_minimal()
p2 <- p + geom_point(aes(xmin,z))  + theme_minimal()
p3 <- p + geom_point(aes(xmax,xmax))  + theme_minimal()
p4 <- p + geom_point(aes(xmax,ymin))  + theme_minimal()
p5 <- p + geom_point(aes(ymin,z)) + theme_minimal()

p1
`````` ``````p2
`````` ``````p3
`````` ``````p4
`````` ``````p5
`````` Created on 2020-09-12 by the reprex package (v0.3.0)

These plots show the origin of the concentration of points at x \lor y \approx 11

Given the behavior of `dat` viewed piecewise, a decision can be made as to the way to plot f(x,y) = Z that is not visually dominated by the values around the attractor.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.