Working with dates and abline(v) in R - r

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.

Related

Monument.ai only shows bar chart, not line chart

I downloaded monument.ai a while ago and was trying it out with my data. It shows only the bar chart instead of line chart. Does anyone else have this problem? How can I change it?
I have actually resolved this problem by reconstructing my date format. If Monument.ai doesnt recognize the data format, it will not treat it as a time series, thus no line chart.

Trying to make a paneled spaghetti chart with highlighted line

I'm trying to make a graph like the one found here at the very bottom: https://www.data-to-viz.com/caveat/spaghetti.html
unfortunately I have a character variable instead of a numeric one for the x axis (I have four years by months), and I don't get the grey lines as in that graph per panel. Does anyone know how else to obtain that graph?
It's hard to say without more details/an example, but it seems like you should be able to do this using the gghiglight package. There's a facetted example half way down this page.

plot function type=ā€œnā€ is ignored for plot(y~x)?

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.

Failure of producing a histogram in R if using breaks=a numeric vector

Is there any difference between using breaks=c(1,2,3,4,5) and breaks=c(1:5)?
I am using hist(...) function to get a histogram for a dataset.
If I use hist(MyDataFile$V3, breaks=c(1,2,3,4,5)), then I get the correct histogram.
If I use hist(MyDataFile$V3, breaks=c(1:5)) or
hist(MyDataFile$V3, breaks=seq(1,5,1)), then the histogram does not show correct rectangles, and it only gives me a lot of vertical parallel lines at some breaks instead, but not the correct histogram.
I cannot figure out what is wrong with my the value of my breaks.
[UPDATA] It is a silly mistake. The problem is solved if set freq=FALSE. Thank you all for your time.

convert numeric tick to date in ggplot

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()

Resources