如何在 facet_grid 中增加 ggplot 箱线图的内边距?
How to increase the inner margin of a ggplot boxplot in facet_grid?
我正在设置箱线图以在 ggplot2 中呈现这些图 facet_grid
,我想增加内边距。
不幸的是,我无法增加到小平面框架的距离。
如何增加蓝色箭头所示的内边距(左侧和右侧)?
require(ggplot2)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)),
data.frame(approach=1,product=2,value=seq(5,15,0.3)),
data.frame(approach=1,product=3,value=seq(5,17,0.2)),
data.frame(approach=2,product=1,value=seq(1,13,0.3)),
data.frame(approach=2,product=2,value=seq(3,18,0.5)),
data.frame(approach=2,product=3,value=seq(4,25,0.7)),
data.frame(approach=3,product=1,value=seq(1,15,0.6)),
data.frame(approach=3,product=2,value=seq(3,16,0.5)),
data.frame(approach=3,product=3,value=seq(1,10,0.1)))
gg1 <- ggplot(dat, aes(group =product, y = value)) +
geom_boxplot() +
ylab("size (cm)")+
theme(panel.spacing = unit(0.1, 'lines')) +
theme(plot.background = element_rect(fill ="lightgrey" )) +
scale_fill_grey(start = 0.0, end = 1) +
theme_bw()+
xlab("") +
facet_grid(cols=vars(approach)) +
theme(axis.text.x = element_text(colour="black")) +
theme(axis.text.y=element_text(colour="black"))+
theme(panel.spacing=unit(0,"lines")) +
guides(fill=guide_legend(title="Products")) +
theme(plot.background = element_rect(fill ="lightgrey" ))
gg1
此外,它如何适用于离散量表?
require(ggplot2)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)),
data.frame(approach=1,product=2,value=seq(5,15,0.3)),
data.frame(approach=1,product=3,value=seq(5,17,0.2)),
data.frame(approach=2,product=1,value=seq(1,13,0.3)),
data.frame(approach=2,product=2,value=seq(3,18,0.5)),
data.frame(approach=2,product=3,value=seq(4,25,0.7)),
data.frame(approach=3,product=1,value=seq(1,15,0.6)),
data.frame(approach=3,product=2,value=seq(3,16,0.5)),
data.frame(approach=3,product=3,value=seq(1,10,0.1)))
dat$product<-as.factor(dat$product)
gg1<-ggplot(dat, aes(x =product, y = value)) +
geom_boxplot() +
ylab("size (cm)")+
theme(panel.spacing = unit(0.1, 'lines')) +
theme(plot.background = element_rect(fill ="lightgrey" )) +
scale_fill_grey(start = 0.0, end = 1) +
theme_bw()+ xlab("") +
facet_grid(cols=vars(approach)) +
theme(axis.text.x = element_text(colour="black")) +
theme(axis.text.y=element_text(colour="black"))+
theme(panel.spacing=unit(0,"lines")) +
guides(fill=guide_legend(title="Products")) +
theme(plot.background = element_rect(fill ="lightgrey" ))
gg1
您正在查看的部分是由比例控制的,而不是小平面或主题边距。
以下任一方法都可以。在这种情况下,它们的结果相似,因为您的 x 值范围在 (-1, 1)
附近。更一般地说,查找 ?expand_scale
的帮助文件以获取乘法与加法扩展因子的示例
gg1 + scale_x_continuous(expand = c(0.2, 0)) # expand scales by a multiple of 20%
gg1 + scale_x_continuous(expand = c(0, 0.2)) # expand scales by an addition of 0.2
我正在设置箱线图以在 ggplot2 中呈现这些图 facet_grid
,我想增加内边距。
不幸的是,我无法增加到小平面框架的距离。
如何增加蓝色箭头所示的内边距(左侧和右侧)?
require(ggplot2)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)),
data.frame(approach=1,product=2,value=seq(5,15,0.3)),
data.frame(approach=1,product=3,value=seq(5,17,0.2)),
data.frame(approach=2,product=1,value=seq(1,13,0.3)),
data.frame(approach=2,product=2,value=seq(3,18,0.5)),
data.frame(approach=2,product=3,value=seq(4,25,0.7)),
data.frame(approach=3,product=1,value=seq(1,15,0.6)),
data.frame(approach=3,product=2,value=seq(3,16,0.5)),
data.frame(approach=3,product=3,value=seq(1,10,0.1)))
gg1 <- ggplot(dat, aes(group =product, y = value)) +
geom_boxplot() +
ylab("size (cm)")+
theme(panel.spacing = unit(0.1, 'lines')) +
theme(plot.background = element_rect(fill ="lightgrey" )) +
scale_fill_grey(start = 0.0, end = 1) +
theme_bw()+
xlab("") +
facet_grid(cols=vars(approach)) +
theme(axis.text.x = element_text(colour="black")) +
theme(axis.text.y=element_text(colour="black"))+
theme(panel.spacing=unit(0,"lines")) +
guides(fill=guide_legend(title="Products")) +
theme(plot.background = element_rect(fill ="lightgrey" ))
gg1
此外,它如何适用于离散量表?
require(ggplot2)
dat <- rbind(data.frame(approach=1,product=1,value=seq(1,20,0.5)),
data.frame(approach=1,product=2,value=seq(5,15,0.3)),
data.frame(approach=1,product=3,value=seq(5,17,0.2)),
data.frame(approach=2,product=1,value=seq(1,13,0.3)),
data.frame(approach=2,product=2,value=seq(3,18,0.5)),
data.frame(approach=2,product=3,value=seq(4,25,0.7)),
data.frame(approach=3,product=1,value=seq(1,15,0.6)),
data.frame(approach=3,product=2,value=seq(3,16,0.5)),
data.frame(approach=3,product=3,value=seq(1,10,0.1)))
dat$product<-as.factor(dat$product)
gg1<-ggplot(dat, aes(x =product, y = value)) +
geom_boxplot() +
ylab("size (cm)")+
theme(panel.spacing = unit(0.1, 'lines')) +
theme(plot.background = element_rect(fill ="lightgrey" )) +
scale_fill_grey(start = 0.0, end = 1) +
theme_bw()+ xlab("") +
facet_grid(cols=vars(approach)) +
theme(axis.text.x = element_text(colour="black")) +
theme(axis.text.y=element_text(colour="black"))+
theme(panel.spacing=unit(0,"lines")) +
guides(fill=guide_legend(title="Products")) +
theme(plot.background = element_rect(fill ="lightgrey" ))
gg1
您正在查看的部分是由比例控制的,而不是小平面或主题边距。
以下任一方法都可以。在这种情况下,它们的结果相似,因为您的 x 值范围在 (-1, 1)
附近。更一般地说,查找 ?expand_scale
的帮助文件以获取乘法与加法扩展因子的示例
gg1 + scale_x_continuous(expand = c(0.2, 0)) # expand scales by a multiple of 20%
gg1 + scale_x_continuous(expand = c(0, 0.2)) # expand scales by an addition of 0.2