【发布时间】:2017-11-27 06:08:37
【问题描述】:
我想创建一个分组条形图,其中组以特定顺序出现。这是一个详细的例子。
df <- data.frame(Groups = c("B","B","B","C","C","A","A","A","A","A"),
Ages = c(3,4,4,5,3,4,5,3,3,5))
df_cast <- dcast(data = df, formula = Groups ~ Ages)
df_bars <- melt(data = df_cast, id.vars = 'Groups')
ggplot(data = df_bars, aes( x = Groups, y = value, fill = variable ) ) +
geom_bar( stat = 'identity', position = 'dodge' ) +
labs(title="Groups ages", x = "Groups", y = "Frecuency") +
labs(fill = "Ages") + theme(plot.title = element_text(hjust = 0.5))
这些组是 B、C 和 A,我希望它们按顺序出现在条形图中,上面的命令按字母顺序排列它们。
【问题讨论】: