简体   繁体   中英

ordering dates in ggplot bar plot

I have a dataframe of birthdays and want to order them by soonest date to furthest date. They seem not to want to graph in order of date.

Here is the dataframe:

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"))

What i have tried so far is:

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

Try:

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')

EDIT:

Solution here 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM