Eco*_*o06 1 r function between fill ggplot2
我试图找到如何遮蔽ggplot中由函数定义的两行之间的区域的可能性.我找到了一些使用geom_area或geom_ribbon的解决方案,但在这两种情况下,您都需要一个数据库,您可以在其中定义ymin和ymax.还有其他可能性吗?在定义ymin和ymax的方式中,也使用与行相同的函数?
这是我的例子:
myplot <- ggplot(data.frame(x=c(0, 100)), aes(x=x)) +
stat_function(fun= function(x)20*sqrt(x), geom="line", colour= "black", size= 1) +
stat_function(fun= function(x)50*sqrt(x), geom="line", colour= "black", size= 1)
myplot
Run Code Online (Sandbox Code Playgroud)
提前谢谢你的帮助.
尝试将函数放入为图形提供数据的数据框中.然后您可以使用geom_ribbon填充两个功能之间的区域.
mydata = data.frame(x=c(0:100),
func1 = sapply(mydata$x, FUN = function(x){20*sqrt(x)}),
func2 = sapply(mydata$x, FUN = function(x){50*sqrt(x)}))
ggplot(mydata, aes(x=x, y = func2)) +
geom_line(aes(y = func1)) +
geom_line(aes(y = func2)) +
geom_ribbon(aes(ymin = func2, ymax = func1), fill = "blue", alpha = .5)
Run Code Online (Sandbox Code Playgroud)