【问题标题】:ordering dates in ggplot bar plotggplot条形图中的订购日期
【发布时间】:2019-07-27 23:32:03
【问题描述】:

我有一个生日数据框,想按从最早到最远的日期排序。他们似乎不想按日期顺序绘制图表。

这是数据框:

df <- data.frame(name = c("Sara", "Joe", "Matt", "Katie", "Ryan", "Sam"), date = c("2003-03-27", "2004-05-16", "2001-02-02", "2004-05-16", "2002-09-03", "2003-1-17"))

到目前为止我尝试过的是:

ggplot(df[order(df$date),]) +  
geom_bar(aes(x=name, y=date), color = 'black', fill = 'blue', stat = 'identity')

【问题讨论】:

    标签: r ggplot2 geom-bar


    【解决方案1】:

    试试:

    df$name <- with(df, factor(name, levels = name[order(date, name)]))
    
    ggplot(df) +  
      geom_bar(aes(x=name, y=date), color = 'black', fill = 'blue', stat = 'identity')
    

    编辑:

    解决方案在这里https://github.com/tidyverse/ggplot2/issues/3462

    ggplot(tt, aes(x = data, y = value, colour = variable)) + scale_y_datetime(breaks = tt$value, date_labels = "%H:%M:%S") + geom_point()

    【讨论】:

    • 这很好用。你能解释一下为什么订单需要按日期和名称而不是日期吗?
    • 我使用name[order(date, name)]而不是name[order(date)]以防平局,但因为在这种情况下你不能只使用第二个
    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2020-09-04
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多