ggplot2 - R - order of legend ggplot -
i have following data frame:
author<-c("university","office", "school","university","office", "school","university","office", "school") typ<-c("text", "text", "text","data", "data","data", "list", "list", "list") number<-c("3","1","6","4","4","2","8","1","1") df<-data.frame(typ,author,number)
if apply:
ggplot(df, aes(x=author, y=number, fill=typ)) + geom_bar(stat='identity') + coord_flip()
then stacked bar plot bars orders in order of date frame, i.e. text
, data
, list
, legend in alphabethic order. there (non brute force, ie. hand) option such can rearrange legend in "given" order of df, i.e. in text
, data
, list
?
(just clarify - have bunch of data frames bigger in sense vectors "typ" (which different in each data frame) have more entries order should not changed , displayed in legend. wrote routine plots these data frames cannot change legends manually - looking routine friendly solution)
you automatically set levels according order how appear in data.frame
:
df$typ <- factor(df$typ, levels = unique(df$typ)) ggplot(df, aes(x=author, y=number, fill=typ)) + geom_bar(stat='identity') + coord_flip()
in way change order of factor
according order in df$typ
:
Comments
Post a Comment