First, I don't have producible data, so please don't bother to comment about this.
I was tring to generate a graph using ggplot2().
Then, x-axis is in numberic version. Data frame has also x=1~10, y=blah blah.
Eveything was fine, till now, I need to convert x-axis ticks to DATE.
I have created a "break" as a serie of dates that CORRESPONDING to numeric 1~10, and used scale_x_date.
As I can image, R returns errors, saying "INVALID INPUT: date_trans works with objects of class Date only".
Is this because I draw the lines and graphs in numeric, but add a scale of date as x-axis ticks?
Any help is appreciated.
You have to convert your x-axis to an R recognized format, see:
as.Date()
as.POSIXct()
Related
I am trying to plot a graph of certain values against time using the plot function.
I am simply trying to change the representation of the dots, using the pch= function. However R is simply ignoring me! I have also tried removing the dots so that I can place labels instead, but when I type in type="n" it ignores that too!
I am using the exact same format of code that I have used for other plots but this time it just isn't cooperating.
If I specify other features such as the title or the x/y axis labels, it will add those in but it simply ignores the pch or type commands.
This is my basic code:
plot(Differences ~ Time, data=subsetH)
But if I run
plot(Differences ~ Time, type="n", data=subsetH)
or
plot(Differences ~ Time, pch=2, data=subsetH)
it keeps plotting the same thing.
Is there something obvious I have missed?
I just came across your question because I encountered the same thing - creating an empty plot did not work, as type='n' was always ignored (as well as other type specifications).
With the help of this entry: Plotting time-series with Date labels on x-axis
I realized that my date format needed to be assigned as "date" class (as.Date()).
I know your entry dates back a little bit already, but maybe it's still useful.
can anyone help me with this. I had a ggplot where the values in the x-axis are dates. I've converted to a plotly plot using ggplotly. When I use box select or lasso select on my plotly plot the x-axis values are being picked up as numerical dates. I give some of them here
event$x <- c(149653440000, 149662080000, 149679360000)
converting these back to dates doenst work by simply applying as.Date. I've read alot of answers to similar question where as.Date.POSIXct is used but that doesnt work with my dates. Can anyone give help on how to convert these numerical numbers back to real dates?
I can answer my own question now. Plotly is reading my dates in in javascript milliseconds. To covert to a date divide by 1000 by 60 by 60 and by 24 then use as.Date to convert that number back to a date. Basically do the opposite of whats shown in the solution shown here:
https://community.plot.ly/t/manual-date-range-on-x-axis-time-series/1316/2
According to the highcharts Docs it should be possible to supply series data to a heatmap in the form [datetime, value], but every jsfiddle example I've seen uses the cartesian form of [x, y, value].
My datetime timestamps are unix time in milliseconds, so all of the required data is available, i.e. date, time and corresponding value, so there is no reason why highcharts should need this time-stamped data to be manipulated into an increasing index.
This guy's question seems to be the closest example I can find, but he himself reverts to using a workaround function
Unexplected results with Highcharts Heatmap
When I followed a similar approach (slightly different for me as I've got 15min intervals) Highcharts seems to interpret the unix timestamp for x-axis labelling correctly, but not for the tooltip or y-axis.
Surely there is a more sensible way of doing this?
I am trying to make a chart in ggplot2 (barplot). There are several days in between data points in my data set. I'd like to only graph the dates where variables are and omit the empty dates.
I have been searching for an hour trying to find an answer to this. I found some replies here indicating that the fix is this:
scale_x_date(format = '%d%b', major='days')
However, those arguments inside of scale_x_date don't seem to work anymore. Does anyone have a quick and easy solution?
I would be remiss if I didn't mention the ggplot2 docs which are a fantastic resource, packed full of well-documented examples. They are often the best place to start when you don't know how to do something with ggplot.
The "translation" for the code you quoted above in the current framework is:
scale_x_date(breaks='days', labels=date_format("%d%b"))
which will require the package scales. Unless you have fairly few dates, you probably also would want an adjustment like
theme(axis.text.x = element_text(angle=45))
If you only want to label the dates for which you have data, try
scale_x_date(breaks=df$date, labels = date_format("%m/%d"))
where df$date is the column of your dataframe containing the dates, and of course you can use your preferred date format string.
I'm working on a set of scatter plots in R, and I want to add a vertical line to all of them. The x-axis is in Date type, and I think that's the problem for me. When I call
abline(v=movie_revenues$X, col="orange")
the line does not show up on the plot. The column "X" is the movie's premiere date, in this form: "11-Dec-2011". When I try to cast it as a date using as.Date(), it gives me an error saying that the text is in ambiguous form.
From this, I guess there are two points where I could have gone wrong.
R doesn't recognize 11-Dec-2011 as a date, but tries to plot it anyway without throwing an error, resulting in a missing vertical line; or,
the date is recognized correctly, but I've called abline
incorrectly, or abline's plotted the line too thin/small for me to
see (the y-axis on these plots are very large).
I'm new to R, so please let me know if there's anything silly I've missed.
Thanks!
The issue was resolved when I used the lubridate package for BOTH my x-axis and my vertical line. For some reason, when I used as.Date() for the x-axis points but lubridate for the vertical line coordinate, the line would not appear.