0
votes

I have a ggplot graph and I want it to show the data from down to up. I know how to reorder based on the the number of entry but not in another way. Those are dates used as factor.

This is a normal graph

x <- ggplot(data, aes(y = factor(date), fill=category)) 
y<- x+geom_bar()+ggtitle("Number of articles per date")+
  labs(y = "date", fill = "category")
print(y)

This is what I tried with no effect:

data %>% arrange(date) %>% 
  ggplot(aes(y = factor(date), fill=category))+
geom_bar()+ggtitle("Number of articles per date")+
  labs(y = "date", fill = "category")

Perhaps there is a way to use the column "date" as as.Date() but I don't know how to use the count of entry per date with as.Date.

Any idea?

1

1 Answers

0
votes

I don't have your data, so I made up some:

dates <- seq(as.Date("2001-01-1"), as.Date("2001-01-12"), by = "day")
set.seed(123)
data <- data.frame(date = sample(dates, 200, replace = TRUE),
                 category = sample(LETTERS[1:5], 200, replace = TRUE))

If I understand your question correctly, you want the earliest date at the bottom? You can do that by added a scale_y_discrete and setting the breaks as the reverse of the level of the factor:

y + scale_y_discrete(breaks = rev(levels(factor(data$date))))

enter image description here

If you want the earliest date at the bottom, then do not reverse the levels. Use one of the following:

y + scale_y_discrete(breaks = levels(factor(data$date)))
y + scale_y_discrete(limits = rev(levels(factor(data$date))))