簡體   English   中英

如何使用ggplot2在兩行之間繪制密度圖?

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

我正在學習R和ggplot2。 我可以繪制單個變量函數(使用geom_path )和密度圖(使用geom_raster )。 但是,我找不到一種將它們組合起來以產生如下圖所示方法的方法。 ggplot2甚至有可能嗎?

密度圖限制在兩條線之間的區域。

當然。 也許:

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