固定在ggplot面的顺序(Fixing the order of facets in ggplot

2019-07-18 01:38发布

数据:

type    size    amount  
T   50%     48.4    
F   50%     48.1    
P   50%     46.8    
T   100%    25.9    
F   100%    26.0    
P   100%    24.9    
T   150%    21.1    
F   150%    21.4    
P   150%    20.1    
T   200%    20.8    
F   200%    21.5    
P   200%    16.5

我需要使用ggplot绘制上述数据的条形图(x轴 - >的“类型”,y轴 - >“量”,按“尺寸”)。 当我用下面的代码,我没有得到在数据显示的顺序的变量“类型”和以及“大小”。 请参阅图。 我用下面的代码为。

 ggplot(temp, aes(type, amount , fill=type, group=type, shape=type, facets=size)) + 
  geom_bar(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(.~size) + 
  theme_bw() + 
  scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"), 
                    labels = c("T", "F", "P"))

用于固定的顺序问题,我已经用于可变“类型”使用以下的因子的方法。 请参阅图也。

temp$new = factor(temp$type, levels=c("T","F","P"), labels=c("T","F","P")) 

不过,现在我不知道如何解决的变量“大小”命令。 它应该是50%,100%。 150%,和200%。

Answer 1:

让你的尺寸的因素在您的数据帧由:

temp$size_f = factor(temp$size, levels=c('50%','100%','150%','200%'))

然后改变facet_grid(.~size)facet_grid(.~size_f)

然后情节:

该图表现在是在正确的顺序。



文章来源: Fixing the order of facets in ggplot