我有一个小面包装,上面有要删除的空白区域(标准答案:set scales = free_y(例如:ggplot faceting - remove empty x-axis labels)),并且我想为我所有的小面变量指定一个顺序(此处“ d”)。 但是似乎我必须选择:
删除有序区域或空白区域。
但是:我要同时删除我的订单和空行。
以下是示例代码:
#gen some example code
c <- c('a', 'a', 'b', 'b')
d <- c('firstsecond', 'firstfirst', 'lowerupper', 'lowerlower')
e <- c(0.2, 0.3, 0.4, 0.5)
f <- c('w', 'v','w', 'v')
df <- cbind(c,d,e,f)
df<- as.data.frame(df)
df$e <- as.numeric(df$e)
orderd <- c( 'firstfirst', 'firstsecond', 'lowerupper', 'lowerlower' )
df<- within(df, d <- factor(d, levels=orderd))
#plotting it with the desired order:
plot_e1 <- ggplot(df, aes(x = d, y = e, color = f)) +
geom_pointrange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), shape = 15) +
theme_bw() +
facet_wrap(c ~ ., scales = "free_y", nrow = 5, strip.position = "left") +
coord_flip() +
#by the way: why do I need to reverse it with rev() ?
scale_x_discrete(drop=TRUE, limits = rev(levels(df$d)))+
scale_colour_viridis_d(begin = 0.75 , end = 0) +
geom_text(aes(label = f), colour = "black", size = 2.5, hjust=1.05, vjust=1.2)
plot_e1 #right side of attachjed picture
#plotting it with the emptry areas removed:
plot_e2 <- ggplot(df, aes(x = d, y = e, color = f)) +
geom_pointrange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), shape = 15) +
theme_bw() +
facet_wrap(c ~ ., scales = "free_y", nrow = 5, strip.position = "left") +
coord_flip() +
scale_x_discrete(limits = rev(levels(df$d)))+
scale_x_discrete(drop=TRUE)+
scale_colour_viridis_d(begin = 0.75 , end = 0) +
geom_text(aes(label = f), colour = "black", size = 2.5, hjust=1.05, vjust=1.2)
# and the warning message is an added bonus of this stupid version :( )
plot_e2#附件图片的左侧
答案 0 :(得分:0)
我不太确定是什么导致了此问题,但这似乎可以解决问题。
首先,反转df中的因子水平:
orderd <- rev(c( 'firstfirst', 'firstsecond', 'lowerupper', 'lowerlower' ))
df<- within(df, d <- factor(d, levels=orderd))
然后此代码以正确的顺序返回没有间隙的图:
ggplot(df, aes(x = d, y = e, color = f)) +
geom_pointrange(aes(min = e - 1.95 * sqrt(e), max = e + 1.95 * sqrt(e)), shape = 15) +
theme_bw() +
facet_wrap(c ~ ., scales = "free", nrow = 5, strip.position = "left") +
coord_flip() +
scale_colour_viridis_d(begin = 0.75 , end = 0) +
geom_text(aes(label = f), colour = "black", size = 2.5, hjust=1.05, vjust=1.2)