-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date example #97
Comments
Hey! Thanks for the example.
with useful choice of ticks / formatting. I will probably first try to find a way to accomplish this in a way that doesn't require the data frame to be able to handle a new data type with
The issue you're seeing here both for the x and y axis is a simple one. For x: your dates are given as strings. This means the data will be interpreted as discrete data (continuous string data is not a useful concept). For a more sane handling converting the dates to timestamps and formatting the labels manually is the correct solution. As timestamps the dates will be treated as continuous data and the number of ticks will be a reasonable value. Custom formatting then allows for nice labels. For y: Full example with a few comments: import ggplotnim, times
let df = toDf(readCsv("./data/finances.csv"))
# perform calculation of the timestamp using `parseTime`
# have to give type hints
.mutate(f{string -> int64: "timestamp" ~ parseTime(df["date"][idx],
"YYYY-MM-dd",
utc()).toUnix})
# alternatively we could do (if `df` is mutable):
# df["timestamp"] = df["date"].toTensor(string).map_inline(
# parseTime(x, "YYYY-MM-dd", utc()).toUnix)
proc formatDate(f: float): string =
## format timestamp to YYYY-MM-dd. We do not only format via
## YYYY, because that will fool us. The ticks will ``not`` be placed
## at year change (31/12 -> 01/01), but rather at the "sensible" positions
## in unix timestamp space. So if you only format via YYYY we end up with
## years not sitting where we expect! That's the major downside of having
## no "understanding" of what dates mean.
result = fromUnix(f.int).format("YYYY-MM-dd")
ggplot(df, aes(x = timestamp, y = yrate)) + # can use raw identifiers if not ambiguous
geom_line() +
geom_point() +
xlab("Date") + ylab("Rate") +
scale_y_continuous() + # force y continuous
# use `formatDate` to format the dates from timestamp
scale_x_continuous(labels = formatDate) +
ggtitle("Rate evolution") +
ggsave("finances.png") This issue will remain open until the handling of such things is more convenient. |
As I said, I'll leave this open as a reminder for myself (and for others to find it easier) until a cleaner solution with less manual work is available. |
Hi Vindaar,
First thank you for this excellent module.
For future reference, this is a common example of use of Date types in R that should improve the plotting ggplotnim capabilities. I believe this is perhaps more suited to be managed in a separate data management module.
So, for the data set:
https://gist.github.com/lf-araujo/5da7266c44b3824b824d578411dee73c
Where dates are arbitrarily entered, but one would usually want to print this out standardised as year, typical R code would use as.Date() and look like the following:
Resulting in:
However, current ggplotnim code would look like:
and
finances.png
looks rather confusing:It is not handling negative values correctly and the dates are not standardised by year. This is currently not possible and not supported. This example is just for future testing.
The text was updated successfully, but these errors were encountered: