R:如何更改facet_grid中每行的列数

lll*_*lll 5 r ggplot2

通过 ggplot,我使用下面的代码得到了如下图。但如果我这样绘图,我根本看不到 x 轴。我想知道是否有任何方法可以解决这个问题,例如更改每行中的列数。我已经尝试过ncol命令,facet_grid但它不允许我这样做。

ggplot(derivative, aes(x = factor(move), fill = factor(move)), colour = black)+ 
geom_bar()+
facet_grid(Market~Season)+
 scale_fill_discrete(name="Relative Market Move",
                  breaks=c("neg.big", "neg.small", "pos.big", "pos.small"),
                  labels=c("Big Negative", "Small Negative", "Big Positive", "Small Positive"))+
 scale_x_discrete(labels=c("Large Negative", "Small Negative", "large Positive", "Small Positive"))+
labs( x = "") +ylab("Count") 
Run Code Online (Sandbox Code Playgroud)

狭窄的多面情节

eip*_*i10 2

您可能会更好地使用堆积条形图并将“负数”的条形朝下。这将更有效地利用水平空间,并更容易看到时间趋势。例如:

library(reshape2)
Run Code Online (Sandbox Code Playgroud)

首先创建一些假数据:

set.seed(199)
dat = data.frame(index=rep(c("S&P 500","Shanghai","Hang Seng"), each=7),
                 year=rep(paste0(rep(2009:2015,each=2),rep(c("Sp","Au"),7)), 3),
                 replicate(3, sample(50:100,14*3)))
dat$big.neg = 300 - rowSums(dat[,3:5])
names(dat)[3:5] = c("big.pos","small.pos","small.neg")

# Set year order
dat$year = factor(dat$year, levels=dat$year[1:14])

# Melt to long format
dat = melt(dat, id.var=c("year","index"))
Run Code Online (Sandbox Code Playgroud)

现在来说说剧情:

ggplot() +
  geom_bar(data=dat[dat$variable %in% c("big.pos","small.pos"),], 
           aes(x=year, y=value, fill=rev(variable)), stat="identity") +
  geom_bar(data=dat[dat$variable %in% c("big.neg","small.neg"),], 
           aes(x=year, y=-value, fill=variable), stat="identity") +
  geom_hline(yintercept=0, colour="grey40") +
  facet_grid(index ~ .) +
  scale_fill_manual(breaks=c("big.neg","small.neg","small.pos","big.pos"),
                    values=c("red","blue","orange","green")) +
  scale_y_continuous(limits=c(-200,200), breaks=seq(-200,200,100), 
                     labels=c(200,100,0,100,200)) +
  guides(fill=guide_legend(reverse=TRUE)) +
  labs(fill="") + theme_bw() +
  theme(axis.text.x=element_text(angle=-90, vjust=0.5)) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述