我有这个情节,我需要围绕y轴,所以看起来是可以接受的除了我想不只是在y轴上显示1值的事实.我想在"scale_y_continuous"函数中添加"limit",以便每个facet图的限制对于各个facet是唯一的.
这里是仅在y轴上显示60和80的图
dat = data.frame(x = c(1,2,3,1,2,3),A=c(80.6, 82,83,60,61,62),A_up =c(81,84,85,62,63,64), A_low =c(79,78,81,59,58,57), group = c("z","z","z","y","y","y"))
ggplot(data=dat , aes(x=as.factor(x), y=A, group = 1)) + #, color =Group, group = Group
geom_line() + geom_point() + # facet_wrap(~COUNTERPARTY_STRATEGY ,ncol=2)
geom_errorbar(aes(ymax = A_up ,ymin = A_low), width = .25) +
scale_y_continuous(breaks = seq( floor( (min(dat$A_low)-11) /10)*10 ,
ceiling( (max(dat$A_up)+11) /10)*10,10 ),
labels = seq( floor( (min(dat$A_low)-11) /10)*10 ,
ceiling( (max(dat$A_up)+11) /10)*10,10 )
) +
facet_wrap(~group ,ncol=2, scales = "free_y")
Run Code Online (Sandbox Code Playgroud)
现在我在比例y连续中添加限制,它全局应用限制.
dat = data.frame(x = c(1,2,3,1,2,3),A=c(80.6, 82,83,60,61,62),A_up =c(81,84,85,62,63,64), A_low =c(79,78,81,59,58,57), group = c("z","z","z","y","y","y"))
ggplot(data=dat , aes(x=as.factor(x), y=A, group = 1)) + #, color =Group, group = Group
geom_line() + geom_point() + # facet_wrap(~COUNTERPARTY_STRATEGY ,ncol=2)
geom_errorbar(aes(ymax = A_up ,ymin = A_low), width = .25) +
scale_y_continuous(breaks = seq( floor( (min(dat$A_low)-11) /10)*10 ,
ceiling( (max(dat$A_up)+11) /10)*10,10 ),
labels = seq( floor( (min(dat$A_low)-11) /10)*10 ,
ceiling( (max(dat$A_up)+11) /10)*10,10 ),
# limits = c( floor( min(dat$A_low[dat$group =="z"]) /10)*10 ,ceiling(max(dat$A_up[dat$group =="z"])/10)*10 )
#limits = c( floor( min(dat$A_low[dat$group =="z"]) /10)*10 ,ceiling(max(dat$A_up[dat$group =="z"])/10)*10 )
limits = c( floor( min(dat$A_low) /10)*10 ,ceiling(max(dat$A_up)/10)*10 )
) +
facet_wrap(~group ,ncol=2, scales = "free_y")
Run Code Online (Sandbox Code Playgroud)
即
c( floor( min(dat$A_low) /10)*10 ,ceiling(max(dat$A_up)/10)*10 )
是50和90
但是我希望限制对于每个方面的情节都是独一无二的
所以正确的情节将有限制
c( floor( min(dat$A_low[dat$group =="y"]) /10)*10 ,ceiling(max(dat$A_up[dat$group =="y"])/10)*10 )
50和70
而左图将有限制
c( floor( min(dat$A_low[dat$group =="z"]) /10)*10 ,ceiling(max(dat$A_up[dat$group =="z"])/10)*10 )
70和90
如何将限制调整为特定于各个方面图?
dat = data.frame(x = c(1,2,3,1,2,3),A=c(80.6, 82,83,60,61,62),A_up =c(81,84,85,62,63,64), A_low =c(79,78,81,59,58,57), group = c("z","z","z","y","y","y"))
dat <- data.table(dat)
dat[, y_min := floor( min(A_low) /10)*10, by = group]
dat[, y_max := ceiling(max(A_up)/10)*10 , by = group]
ggplot(data=dat , aes(x=as.factor(x), y=A, group = 1)) + #, color =Group, group = Group
geom_line() + geom_point() + # facet_wrap(~COUNTERPARTY_STRATEGY ,ncol=2)
geom_errorbar(aes(ymax = A_up ,ymin = A_low), width = .25) +
scale_y_continuous(breaks = seq( floor( (min(dat$A_low)-11) /10)*10 ,
ceiling( (max(dat$A_up)+11) /10)*10,10 ),
labels = seq( floor( (min(dat$A_low)-11) /10)*10 ,
ceiling( (max(dat$A_up)+11) /10)*10,10 )
) +
facet_wrap(~group ,ncol=2, scales = "free_y") +
geom_blank(aes(y = y_min)) + geom_blank(aes(y = y_max))
Run Code Online (Sandbox Code Playgroud)
所以在这里我使用data.table by = group来创建y_min和y_max为每个组.然后使用这些值geom_blank来扩展绘图区域.
显然,这可以自动扩展到任意数量的组/方面.
小智 -4
我没有安装 ggplot,但也许一个简单的if () else可以解决你的问题。我的第一次尝试是这样的:
if {
(dat$group =="y") c( floor( min(dat$A_low[dat$group =="y"]) /10)*10, ceiling(max(dat$A_up[dat$group =="y"])/10)*10 )
else c( floor( min(dat$A_low[dat$group =="z"]) /10)*10, ceiling(max(dat$A_up[dat$group =="z"])/10)*10 )
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1013 次 |
| 最近记录: |