2017-04-21 1 views
0

나는 R과 ggplot2를 배우고 있습니다. 단일 변수 함수 (geom_path 사용) 및 밀도 맵 (geom_raster 사용)을 그릴 수 있습니다. 그러나 나는 아래 그림과 같은 그림을 만들기 위해 그들을 결합하는 방법을 찾을 수 없습니다. ggplot2에서도 가능합니까? 물론ggplot2를 사용하여 두 줄 사이에 밀도 맵을 플로팅하는 방법은 무엇입니까?

A density map restricted to the region between two lines.

+1

당신이 있습니까 무엇 코드를 공유 할 수 있습니까? – atclaus

답변

4

. 미정 :

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() 

관련 문제