R x轴上的日期是随机排序的,即使我使用的数据集中的时间顺序是正确的

R x轴上的日期是随机排序的,即使我使用的数据集中的时间顺序是正确的,r,ggplot2,R,Ggplot2,我有以下数据集 “日期”列(从2017年11月29日到2018年1月16日)在我的数据集中的顺序正确。但是,当我使用ggplot2进行条形图时,x轴上的日期会被随机重新排序。我尝试过reorder()方法,但它不起作用。我已经附上了我现在拥有的代码和数据。非常感谢你的帮助。提前谢谢你 nest_men_women <- melt(mydata[, c('Date', 'Women', 'Men')], id.vars = 1) view(nest_men_women) ggplot(n

我有以下数据集

“日期”列(从2017年11月29日到2018年1月16日)在我的数据集中的顺序正确。但是,当我使用ggplot2进行条形图时,x轴上的日期会被随机重新排序。我尝试过reorder()方法,但它不起作用。我已经附上了我现在拥有的代码和数据。非常感谢你的帮助。提前谢谢你

nest_men_women <- melt(mydata[, c('Date', 'Women', 'Men')], id.vars = 1)
view(nest_men_women)

ggplot(nest_men_women, aes(Date, value)) +
  geom_bar(aes(fill = variable, position = position_dodge(width = 1), 
               stat = "identity", width = 0.8)) +

  scale_fill_manual(values = c("Women"="lightpink", "Men" = "steelblue3"))+

  scale_y_continuous(breaks = c(20,40,60,80,100))+

  theme_light()+

  theme(axis.text.x = element_text(angle = 90, hjust = 1), 
        legend.background = element_blank(), legend.justification = c(1,1),
        axis.line.x.bottom = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"),
        axis.line.y.left = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"),
        axis.line.x.top = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"),
        axis.line.y.right = element_line(colour = "lightgrey", color = "lightgrey", linetype = "solid"))+

  geom_text(data = nest_men_women, 
            aes(Date, label = value), 
            position = position_dodge(width = 0.8), colour="black", 
            check_overlap = TRUE, vjust = -1, hjust = 1, size = 3.8, 
            inherit.aes = TRUE)

nest\u men\u women
Date
列的类型是什么?您的图像太小,无法读取,但我猜日期列的值已被读取为因子,而不是日期。查看
str(nest\u men\u women)
返回的内容。请使用例如
dput()
而不是图像将数据作为纯文本包含,以便我们可以复制/粘贴它。添加
nest\u men\u women$Date@fmarm类型将显示为一个因子。然后将日期列转换为数据类型Date应该会有所帮助。