繁体   English   中英

如何在对数刻度的ggplot R上对由坡度指定的两行之间的区域进行着色和截距

[英]How can I color region between two lines specified by slope and intercept on a ggplot R in a log scale

我正在尝试在由斜率和截距指定的两行之间阴影区域。 我需要阴影的区域在灰线之间指定。 我尝试使用geom_ribbon,但是找到这些点总是失败。

代码和数据如下:

pl=seq(1,12,1)
px=c(231071, 134666, 16420, 100681, 3178, 28996, 121, 222, 149101,119436, 24013, 745)
py=c(948, 941, 744, 660, 363, 78, 50, 5, 254, 17, 5, 293)
data_point<-data.frame(cbind(px,py,pl))

library(ggplot2)
p <- qplot(px, py, data = data_point)
    p+geom_abline(slope=0.56646,intercept=-0.22814,colour="black",fill="black",size=1)+
   geom_abline(slope=0.54266,intercept=-0.36599,colour="darkgrey",
 fill="darkgrey",size=1,alpha=0.6)+
 geom_abline(slope=0.59026,intercept=-0.14705,colour="darkgrey",
fill="darkgrey",size=1.5,alpha=0.6)+
scale_y_log10(breaks=c(1,2,5,10,25,50,100,200,400,600,1000))+
 scale_x_log10(breaks=c(20,50,300,2000,10000,50000,200000,300000)) +labs(x="N",y="h")+theme_bw()+
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text=element_text(size=12),
axis.title=element_text(size=15),
axis.title.y = element_text(angle = 0))

下面列出了我用来查找x,y坐标的代码。 由于我使用以10为底的对数刻度,因此在使用斜率和截距计算坐标时会考虑到它。

x=c(1,15000,300000)
res <- data.frame(x = x,
                  y = (10 ^ (-0.22814)) + (x ** 0.56646),
                  ymin = (10 ^ (-0.36599)) +  (x ** 0.54266),
                  ymax = (10 ^ (-0.14705)) + (x ** 0.59026) )
res$x[which.min(res$x)] <- -Inf
res$x[which.max(res$x)] <- Inf

我花了很多时间来添加灰色区域,但是还没有运气。 我将非常感谢所有帮助。 提前致谢。

这似乎有效...

hi <- list(slope=0.59026,intercept=-0.14705)
lo <- list(slope=0.54266,intercept=-0.36599)
f  <- function(x,p) with(p,10^(intercept+slope*log10(x)))
library(ggplot2)
ggplot(data_point,aes(x=px,y=py))+
  geom_abline(slope=0.56646,intercept=-0.22814,colour="black",fill="black",size=1)+
  geom_abline(slope=0.54266,intercept=-0.36599,colour="darkgrey",
              fill="darkgrey",size=1,alpha=0.6)+
  geom_abline(slope=0.59026,intercept=-0.14705,colour="darkgrey",
              fill="darkgrey",size=1.5,alpha=0.6)+
  geom_ribbon(aes(ymax=f(px,hi),ymin=f(px,lo)),fill="green",alpha=0.2)+
  scale_y_log10(breaks=c(1,2,5,10,25,50,100,200,400,600,1000))+
  scale_x_log10(breaks=c(20,50,300,2000,10000,50000,200000,300000),expand=c(0,0)) +
  labs(x="N",y="h")+theme_bw()+
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.text=element_text(size=12),
        axis.title=element_text(size=15),
        axis.title.y = element_text(angle = 0))

注意在scale_x_log10(...)使用expand=c(0,0) scale_x_log10(...) 这将导致x比例限制与range(x)相同(默认值是在任一侧将它们扩展一点)。 如果使用默认值,则颜色不会一直延伸到绘图的两端。

另外,请不要使用df <- data.frame(cbind(px,py,...)) 更好地使用: df<- data.frame(px,py,...)

暂无
暂无

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

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