Plot dates with regular interval in R
up vote
-1
down vote
favorite
I am trying to plot a graph in R. A snippet of the data set is shown below.
DAY B C
2017-06-01 2946 197.5053
2017-06-02 5215 489.7401
2017-06-03 6305 740.6357
2017-06-04 6442 867.5795
2017-06-05 5758 1016.4603
2017-06-06 5037 1156.0188
The graph I am trying to develop is a plot of DAY (X-Axis) vs B OR C...as illustrated below.
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
The variables runs for about 3 months. I am having challenges representing the intervals on the x-Axis as I want.
What I want to do is:
format: show day and month;
intervals: every 5 days.
It has been difficult finding a solution on Base R. Most recommendations point to some complicated package and code, which is difficult to understand or apply.
Anyone with a simple solution?
Thanks.
r plot
add a comment |
up vote
-1
down vote
favorite
I am trying to plot a graph in R. A snippet of the data set is shown below.
DAY B C
2017-06-01 2946 197.5053
2017-06-02 5215 489.7401
2017-06-03 6305 740.6357
2017-06-04 6442 867.5795
2017-06-05 5758 1016.4603
2017-06-06 5037 1156.0188
The graph I am trying to develop is a plot of DAY (X-Axis) vs B OR C...as illustrated below.
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
The variables runs for about 3 months. I am having challenges representing the intervals on the x-Axis as I want.
What I want to do is:
format: show day and month;
intervals: every 5 days.
It has been difficult finding a solution on Base R. Most recommendations point to some complicated package and code, which is difficult to understand or apply.
Anyone with a simple solution?
Thanks.
r plot
ggplot2 is actually really straightforward if you take the time to work through the examples (I'm assuming that's the "complicated" package). And there are numerous examples on SO for how to use it to change axis labels.
– hrbrmstr
Nov 11 at 11:41
It actually is the complicated package I had in mind. I so far do most of my programming on base R. Maybe I would have another look at it @hrbrmstr thanks
– SBlow
Nov 11 at 12:20
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am trying to plot a graph in R. A snippet of the data set is shown below.
DAY B C
2017-06-01 2946 197.5053
2017-06-02 5215 489.7401
2017-06-03 6305 740.6357
2017-06-04 6442 867.5795
2017-06-05 5758 1016.4603
2017-06-06 5037 1156.0188
The graph I am trying to develop is a plot of DAY (X-Axis) vs B OR C...as illustrated below.
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
The variables runs for about 3 months. I am having challenges representing the intervals on the x-Axis as I want.
What I want to do is:
format: show day and month;
intervals: every 5 days.
It has been difficult finding a solution on Base R. Most recommendations point to some complicated package and code, which is difficult to understand or apply.
Anyone with a simple solution?
Thanks.
r plot
I am trying to plot a graph in R. A snippet of the data set is shown below.
DAY B C
2017-06-01 2946 197.5053
2017-06-02 5215 489.7401
2017-06-03 6305 740.6357
2017-06-04 6442 867.5795
2017-06-05 5758 1016.4603
2017-06-06 5037 1156.0188
The graph I am trying to develop is a plot of DAY (X-Axis) vs B OR C...as illustrated below.
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
The variables runs for about 3 months. I am having challenges representing the intervals on the x-Axis as I want.
What I want to do is:
format: show day and month;
intervals: every 5 days.
It has been difficult finding a solution on Base R. Most recommendations point to some complicated package and code, which is difficult to understand or apply.
Anyone with a simple solution?
Thanks.
r plot
r plot
edited Nov 11 at 11:26
asked Nov 11 at 11:10
SBlow
95
95
ggplot2 is actually really straightforward if you take the time to work through the examples (I'm assuming that's the "complicated" package). And there are numerous examples on SO for how to use it to change axis labels.
– hrbrmstr
Nov 11 at 11:41
It actually is the complicated package I had in mind. I so far do most of my programming on base R. Maybe I would have another look at it @hrbrmstr thanks
– SBlow
Nov 11 at 12:20
add a comment |
ggplot2 is actually really straightforward if you take the time to work through the examples (I'm assuming that's the "complicated" package). And there are numerous examples on SO for how to use it to change axis labels.
– hrbrmstr
Nov 11 at 11:41
It actually is the complicated package I had in mind. I so far do most of my programming on base R. Maybe I would have another look at it @hrbrmstr thanks
– SBlow
Nov 11 at 12:20
ggplot2 is actually really straightforward if you take the time to work through the examples (I'm assuming that's the "complicated" package). And there are numerous examples on SO for how to use it to change axis labels.
– hrbrmstr
Nov 11 at 11:41
ggplot2 is actually really straightforward if you take the time to work through the examples (I'm assuming that's the "complicated" package). And there are numerous examples on SO for how to use it to change axis labels.
– hrbrmstr
Nov 11 at 11:41
It actually is the complicated package I had in mind. I so far do most of my programming on base R. Maybe I would have another look at it @hrbrmstr thanks
– SBlow
Nov 11 at 12:20
It actually is the complicated package I had in mind. I so far do most of my programming on base R. Maybe I would have another look at it @hrbrmstr thanks
– SBlow
Nov 11 at 12:20
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
library(tidyverse)
set.seed(2018-11-11)
data_frame(
DAY = seq(as.Date("2017-06-01"), as.Date("2017-08-31"), "1 day"),
B = sample(1000:6000, length(DAY)),
C = abs(rnorm(length(DAY))) * 1000
) -> sample_data
ggplot(sample_data) +
geom_point(aes(x = DAY, y = B), shape = 21) +
scale_x_date(date_breaks = "5 days", date_labels = "%bn%d") +
labs(x = "Date", y = "Revenue") +
theme_bw() +
theme(panel.grid = element_blank())
You claim:
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
is easier. I'll grant you it defaults to a scatterplot but now you want to do something that's going to require calling more base plot functions to customize the plot so you don't get much for free.
ggplot
is only _two more lettersthan
plot`- You get to specify the data frame once vs have to constantly
$
reference columns plot
defaults to shape 21 but you'd need to use that same parameter in base plots to use different shapes so there's no differencegeom_point()
sounds pretty straightforward to me- the
scale_x_date()
line looks pretty straightforward to me - theme-ing does take some reading and practice, just like anything worthwhile
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
library(tidyverse)
set.seed(2018-11-11)
data_frame(
DAY = seq(as.Date("2017-06-01"), as.Date("2017-08-31"), "1 day"),
B = sample(1000:6000, length(DAY)),
C = abs(rnorm(length(DAY))) * 1000
) -> sample_data
ggplot(sample_data) +
geom_point(aes(x = DAY, y = B), shape = 21) +
scale_x_date(date_breaks = "5 days", date_labels = "%bn%d") +
labs(x = "Date", y = "Revenue") +
theme_bw() +
theme(panel.grid = element_blank())
You claim:
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
is easier. I'll grant you it defaults to a scatterplot but now you want to do something that's going to require calling more base plot functions to customize the plot so you don't get much for free.
ggplot
is only _two more lettersthan
plot`- You get to specify the data frame once vs have to constantly
$
reference columns plot
defaults to shape 21 but you'd need to use that same parameter in base plots to use different shapes so there's no differencegeom_point()
sounds pretty straightforward to me- the
scale_x_date()
line looks pretty straightforward to me - theme-ing does take some reading and practice, just like anything worthwhile
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
add a comment |
up vote
0
down vote
library(tidyverse)
set.seed(2018-11-11)
data_frame(
DAY = seq(as.Date("2017-06-01"), as.Date("2017-08-31"), "1 day"),
B = sample(1000:6000, length(DAY)),
C = abs(rnorm(length(DAY))) * 1000
) -> sample_data
ggplot(sample_data) +
geom_point(aes(x = DAY, y = B), shape = 21) +
scale_x_date(date_breaks = "5 days", date_labels = "%bn%d") +
labs(x = "Date", y = "Revenue") +
theme_bw() +
theme(panel.grid = element_blank())
You claim:
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
is easier. I'll grant you it defaults to a scatterplot but now you want to do something that's going to require calling more base plot functions to customize the plot so you don't get much for free.
ggplot
is only _two more lettersthan
plot`- You get to specify the data frame once vs have to constantly
$
reference columns plot
defaults to shape 21 but you'd need to use that same parameter in base plots to use different shapes so there's no differencegeom_point()
sounds pretty straightforward to me- the
scale_x_date()
line looks pretty straightforward to me - theme-ing does take some reading and practice, just like anything worthwhile
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
add a comment |
up vote
0
down vote
up vote
0
down vote
library(tidyverse)
set.seed(2018-11-11)
data_frame(
DAY = seq(as.Date("2017-06-01"), as.Date("2017-08-31"), "1 day"),
B = sample(1000:6000, length(DAY)),
C = abs(rnorm(length(DAY))) * 1000
) -> sample_data
ggplot(sample_data) +
geom_point(aes(x = DAY, y = B), shape = 21) +
scale_x_date(date_breaks = "5 days", date_labels = "%bn%d") +
labs(x = "Date", y = "Revenue") +
theme_bw() +
theme(panel.grid = element_blank())
You claim:
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
is easier. I'll grant you it defaults to a scatterplot but now you want to do something that's going to require calling more base plot functions to customize the plot so you don't get much for free.
ggplot
is only _two more lettersthan
plot`- You get to specify the data frame once vs have to constantly
$
reference columns plot
defaults to shape 21 but you'd need to use that same parameter in base plots to use different shapes so there's no differencegeom_point()
sounds pretty straightforward to me- the
scale_x_date()
line looks pretty straightforward to me - theme-ing does take some reading and practice, just like anything worthwhile
library(tidyverse)
set.seed(2018-11-11)
data_frame(
DAY = seq(as.Date("2017-06-01"), as.Date("2017-08-31"), "1 day"),
B = sample(1000:6000, length(DAY)),
C = abs(rnorm(length(DAY))) * 1000
) -> sample_data
ggplot(sample_data) +
geom_point(aes(x = DAY, y = B), shape = 21) +
scale_x_date(date_breaks = "5 days", date_labels = "%bn%d") +
labs(x = "Date", y = "Revenue") +
theme_bw() +
theme(panel.grid = element_blank())
You claim:
plot(data$DAY, data$B, cex=0.5, xlab = "Date", ylab = "Revenue")
is easier. I'll grant you it defaults to a scatterplot but now you want to do something that's going to require calling more base plot functions to customize the plot so you don't get much for free.
ggplot
is only _two more lettersthan
plot`- You get to specify the data frame once vs have to constantly
$
reference columns plot
defaults to shape 21 but you'd need to use that same parameter in base plots to use different shapes so there's no differencegeom_point()
sounds pretty straightforward to me- the
scale_x_date()
line looks pretty straightforward to me - theme-ing does take some reading and practice, just like anything worthwhile
answered Nov 11 at 11:54
hrbrmstr
59.5k584143
59.5k584143
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
add a comment |
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
It actual is much easier. Thanks again. This works.
– SBlow
Nov 11 at 12:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248127%2fplot-dates-with-regular-interval-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
ggplot2 is actually really straightforward if you take the time to work through the examples (I'm assuming that's the "complicated" package). And there are numerous examples on SO for how to use it to change axis labels.
– hrbrmstr
Nov 11 at 11:41
It actually is the complicated package I had in mind. I so far do most of my programming on base R. Maybe I would have another look at it @hrbrmstr thanks
– SBlow
Nov 11 at 12:20