library(ggplot2)
df <- data.frame(Treatment=c("A", "B","A","B"), Value=c(3,4, 20,2), SE=c(1,1,5,1), Type=c("c1","c1","c2","c2"))
p1 <- ggplot(df, aes(Treatment, Value,fill=Treatment)) +
geom_bar(stat="identity", col="Black") +
geom_errorbar(aes(ymax = Value + SE, ymin=Value), col="black") +
facet_wrap(~Type, scale="free_y") +
theme(panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA),
strip.background = element_blank())
p1
我想删除bar和x轴之间的空白区域:
p1 + scale_y_continuous(expand = c(0,0))
这也消除了geom和y轴最大值之间的空白区域。防止这种情况的一种方法是控制y轴的极限
p1 + scale_y_continuous(expand = c(0,0), limits=c(0,26))
但是现在面板c1有很多空格,因为limits
显然会覆盖scale="free"
,当面板按列排列时,这尤其有效。
所以,我需要同时激活expand
- 参数和scale="free"
。有可能吗?
答案 0 :(得分:1)
您可能想要使用geom_blank()
:
请体谅并尽量不要混淆用户!这种转变可能很危险!
library(ggplot2)
df <- data.frame(Treatment=c("A", "B","A","B"), Value=c(3,4, 20,2), SE=c(1,1,5,1), Type=c("c1","c1","c2","c2"))
ggplot(df, aes(Treatment, Value,fill=Treatment)) +
geom_bar(stat="identity", col="Black") +
geom_errorbar(aes(ymax = Value + SE, ymin=Value), col="black") +
geom_blank(aes(y=Value + SE + 1)) +
facet_wrap(~Type, scale="free_y") +
theme(panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA),
strip.background = element_blank()) +
scale_y_continuous(expand = c(0,0))
或者:
ggplot(df, aes(Treatment, Value,fill=Treatment)) +
geom_bar(stat="identity", col="Black") +
geom_errorbar(aes(ymax = Value + SE, ymin=Value), col="black") +
geom_blank(aes(y=Value + 2 * SE)) +
facet_wrap(~Type, scale="free_y") +
theme(panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA),
strip.background = element_blank()) +
scale_y_continuous(expand = c(0,0))
由reprex package(v0.2.0)创建于2018-05-17。