r - Fixing the order of facets in ggplot -
data:
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 i need plot bargraph of above data using ggplot (x-axis -> "type", y-axis -> "amount", group "size"). when used following code, not getting variable "type" , "size" in order shown in data. please see figure. have used following code that.
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"))
.
for fixing order issue, have used factor method variable "type" using following. please see figure also.
temp$new = factor(temp$type, levels=c("t","f","p"), labels=c("t","f","p")) 
however, don't know how fix order variable "size". should 50%, 100%. 150%, , 200%.
make size factor in dataframe by:
temp$size_f = factor(temp$size, levels=c('50%','100%','150%','200%')) then change facet_grid(.~size) facet_grid(.~size_f)
then plot: 
the graphs in correct order.
Comments
Post a Comment