简体   繁体   中英

How do I plot a bivariate function equated to 0 in R and ggplot2?

I am trying to plot a bivariate function equated to 0 in R, using no packages other than the basic packages and ggplot2 . Namely, the function is:

f(x,y) = x-log(x)+y-log(y)+C, where C < -2

Can I plot this function equated to 0, using R? . I did this using Desmos online graphing calculator and it worked, but now I can't figure out how to do it in R. I don't need the exact solution of x and y, but just the plot.

Here's a quick brute force approach:

library(tidyverse)

my_func <- function(x, y, C) { x - log(x) + y - log(y) + C }

expand_grid(x = seq(0, 15, by = 0.02),
            y = seq(0, 15, by = 0.02),
            C = seq(-10, -2, by = 1)) %>%
  mutate(error = my_func(x, y, C)) %>%
  filter(abs(error) < 0.1) %>%
  ggplot(aes(x, y, alpha = 1 - abs(error))) +
  geom_tile() +
  guides(alpha = F) +
  facet_wrap(~C)

在此处输入图片说明


EDIT: version using ggplot2 with base R.

library(ggplot2)
output <- expand.grid(x = seq(0, 15, by = 0.02),
            y = seq(0, 15, by = 0.02),
            C = seq(-10, -2, by = 1))
output$error = my_func(output$x, output$y, output$C)
output <- output[abs(output$error) < 0.1,]
ggplot(output, aes(x, y, alpha = 1 - abs(error))) +
  geom_tile() +
  guides(alpha = F) +
  facet_wrap(~C)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM