简体   繁体   English

如何使用ggplot2在两行之间绘制密度图?

[英]How to plot a density map between two lines with ggplot2?

I'm learning R and ggplot2. 我正在学习R和ggplot2。 I can plot single variable functions (using geom_path ) and density maps (using geom_raster ). 我可以绘制单个变量函数(使用geom_path )和密度图(使用geom_raster )。 However I can't find a way to combine them to produce a figure like the one below. 但是,我找不到一种将它们组合起来以产生如下图所示方法的方法。 Is that even possible with ggplot2? ggplot2甚至有可能吗?

密度图限制在两条线之间的区域。

Of course. 当然。 Maybe: 也许:

library(tidyverse)

expand.grid(x = seq(0, 1, .001),    # make a grid of x and y values
            y = seq(0, 1, .001)) %>% 
    filter(y < x, y > x ^ 2) %>%    # filter to rows between curves
    ggplot(aes(x, y, fill = x + y)) + 
    geom_raster() + 
    stat_function(fun = identity, color = 'red') +    # add y = x
    stat_function(fun = function(x){x^2}, color = 'red', linetype = 'dashed') + 
    scale_fill_gradient(low = 'black', high = 'white') + 
    coord_equal()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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