在ggplot条形图中排序日期
ordering dates in ggplot bar plot
我有一个生日数据框,想按最快日期到最远日期排序。他们似乎不想按日期顺序绘制图表。
这是数据框:
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')
尝试:
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()
我有一个生日数据框,想按最快日期到最远日期排序。他们似乎不想按日期顺序绘制图表。
这是数据框:
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')
尝试:
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()