0

I'm sorry if variables will not be displayed as usually, but I'm new at this...

I'm trying to create a stacked barchart using ggplot2 and I've made almost everything so far except plot it on DD-MM-YYYY format. I've been able to plot the chart on Julian Day using

ggplot(DataFrame, aes(fill=GroupVariable, y=YVar, x=JulianDay))

but every time I try something to change it to DD-MM-YYYY format it returns "Error in as.Date.numeric(value) : 'origin' must be supplied". I've also tried to set the origin to 1970-01-01 but it returns the same error message. Bellow I present some different attempts I've made:

ggplot(DataFrame, aes(fill=GroupVariable, y=YVar, x=as.Date(JulianDay, origin=as.Date("1970-01-01"))))

using lubridate:

ggplot(DataFrame, aes(fill=GroupVariable, y=YVar, x=ymd(Time_variable_in_YYYYMMDD_format)))

using epitools:

 ggplot(DataFrame, aes(fill=GroupVariable, y=YVar, x=julian2date(JulianDay, origin=as.Date("1970-01-01"))))

I do not want to plot it by turning the date into factor because then the graphs will loose the time scale and the data is not evenly spaced.

As asked, I present an example of my data frame bellow

Value Group Date Date_Julian1900 Date_Julian1970
0.002683715 A 2018-05-07 43227.00 17658.00
0.363345653 A 2018-06-06 43257.00 17688.00
0.211011887 A 2018-07-04 43285.00 17716.00
0.699667394 A 2018-08-06 43318.00 17749.00
0.080374412 A 2018-09-05 43348.00 17779.00
0.186650813 A 2018-10-02 43375.00 17806.00
0.030873721 A 2018-10-31 43404.00 17835.00
0.057359736 A 2018-11-30 43434.00 17865.00
0.000583098 A 2019-01-03 43468.00 17899.00
0 A 2019-01-29 43494.00 17925.00
0 A 2019-03-27 43551.00 17982.00
0 A 2018-05-17 43237.00 17668.00
0.138461307 A 2018-07-13 43294.00 17725.00
0.274086982 A 2018-08-10 43322.00 17753.00
0.145111397 A 2018-09-09 43352.00 17783.00
0.33842957 B 2018-05-07 43227.00 17658.00
0.32761246 B 2018-06-06 43257.00 17688.00
0.414862573 B 2018-07-04 43285.00 17716.00
0.172168493 B 2018-08-06 43318.00 17749.00
0.156903282 B 2018-09-05 43348.00 17779.00
0.203908458 B 2018-10-02 43375.00 17806.00
0.120373644 B 2018-10-31 43404.00 17835.00
0.101954311 B 2018-11-30 43434.00 17865.00
0.11928644 B 2019-01-03 43468.00 17899.00
0.03338635 B 2019-01-29 43494.00 17925.00
0.1703545 B 2019-03-27 43551.00 17982.00
0.019091472 B 2018-05-17 43237.00 17668.00
0.369557828 B 2018-07-13 43294.00 17725.00
0.12126825 B 2018-08-10 43322.00 17753.00
0.133006126 B 2018-09-09 43352.00 17783.00
0.070069201 C 2018-05-07 43227.00 17658.00
0.115976922 C 2018-06-06 43257.00 17688.00
0.096136808 C 2018-07-04 43285.00 17716.00
0.026000904 C 2018-08-06 43318.00 17749.00
0.063052453 C 2018-09-05 43348.00 17779.00
0.067014627 C 2018-10-02 43375.00 17806.00
0.868220389 C 2018-10-31 43404.00 17835.00
0.063531995 C 2018-11-30 43434.00 17865.00
0.043645255 C 2019-01-03 43468.00 17899.00
0.023917014 C 2019-01-29 43494.00 17925.00
0 C 2019-03-27 43551.00 17982.00
0.003265612 C 2018-05-17 43237.00 17668.00
0.132452726 C 2018-07-13 43294.00 17725.00
0.035795901 C 2018-08-10 43322.00 17753.00
0.067606397 C 2018-09-09 43352.00 17783.00
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly is in `DataFrame`? – MrFlick Dec 11 '20 at 20:39
  • I added a part of the data frame – Rui Cereja Dec 11 '20 at 20:58
  • It would be better to post a `dput()` because the text version doesn't tell us how it was imported into R. What does `class(Dataframe$Date)` return? It's confusing that your code variable names don't match the names in the data.frame you posted. – MrFlick Dec 11 '20 at 21:01
  • Are you aiming to turn the `Date_Julian` columns into dates (by adding number of days to origin presumably) or get the `Date` column plotted on the x-axis with the labels being in DD-MM-YYYY format? There are two different steps - either turning a column into dates or making a date display a certain way. The second one of those you only do in formatting your plot labels. Can post an example if that helps? – Andy Baxter Dec 11 '20 at 21:19
  • Thank you all for your help, the problem was exactly that there was columns that were wrongly classified... Sorry if this was a newbie problem... – Rui Cereja Dec 12 '20 at 17:32

1 Answers1

0

The advice in the comments are good. I am under the impression you just want the labels to be in DD-MM-YYYY format (if this is incorrect, I'll delete the answer).

If your Date column is in Date format, that's great. If not, make sure it is:

DataFrame$Date <- as.Date(DataFrame$Date)

You can change the text label of the x-axis with scale_x_date. Here, you can specify the DD-MM-YYYY format. You can also clarify the distance between breaks, such as "1 month".

library(ggplot2)
 
ggplot(DataFrame, aes(fill = Group, y = Value, x = Date)) +
  geom_bar(stat="identity") +
  scale_x_date(date_labels = "%d-%m-%Y", date_breaks = "1 month")

There may be better alternatives to visualize the data, depending on what you wish to demonstrate here.

Plot

plot with modified date labels

Ben
  • 28,684
  • 5
  • 23
  • 45
  • Sorry if this ended up being a newbie problem, but it was exactly that, some of the columns were wrongly classified. Following your steps founded it and the problem was fixed. Your graph is exactly what I'm trying to make. Thank you all for your help – Rui Cereja Dec 12 '20 at 17:30