简体   繁体   中英

R, Format date on X axis using ggplot

After aggregating, "filtering" and parsing a column with date and hours by using as.Date, also transforming it by using melt function, my data looks like this:

DATE    variable value
1 13-09-20  Billete_50 20405
2 14-09-20  Billete_50 19808
3 13-09-20 Billete_100 27787
4 14-09-20 Billete_100 20361
5 13-09-20       Total 48192
6 14-09-20       Total 40169

I want a linear graph to show the data, but it looks like this: 线性图

It's in spanish, I'm sorry, but the date is almost the same, it may say only September (Sep), but it is showing October, January, April, July, then back to October.

This is my ggplot line:

ggplot(plotCajero1Melted, aes(x=DATE, y=value, col=variable)) + geom_line() +xlab("Fecha") +ylab("Saldo") +ggtitle("Cajero 1")

What should I do? It's something with the date? I was thinking in the year, it is represented in a short way, probably it is making ggplot to have a weird behavior, or maybe a ggplot option that i'm missing?

You're right, your date format is not read as it should be (it seems to be read as Year-Month-Day).

You can modify the date format for example by using the function dmy from lubridate package to indicate r to read the date as Day-Month-Year:

library(lubridate)
df$DATE <- dmy(df$DATE)

ggplot(df, aes(x = DATE, y = value, color = variable))+
  geom_line()

在此处输入图片说明

Is it what you are looking for ?

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