2
library(tidyverse)

df <- data.frame(date = as.Date(c("2017-12-01", "2018-01-01", "2018-02-01", 
                                  "2018-03-01", "2018-04-01", "2018-05-01", 
                                  "2018-06-01", "2018-07-01", "2018-08-01", 
                                  "2018-09-01", "2018-10-01", "2018-11-01")), 
                 value = c(0.567859562, 0.514907158, 0.035399304, 0.485728823, 
                           0.925127361, 0.237531067, 0.301930968, 0.133373326, 
                           0.082275426, 0.464255614, 0.2366749, 0.652084264))

ggplot(df, aes(date, value)) + 
  geom_col() + 
  scale_x_date(date_breaks = "1 month", 
                 date_labels = "%b") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

I want to maintain my plot shown below, exactly as is, with two exceptions. I want to remove the first Nov on the x-axis label and the last Dec on the x-axis label. I added coord_cartesian(xlim = as.Date(c("2017-12-01", "2018-11-01"))) to my code chunk above, but this eliminates the 'blank space' padding at either end of my x-axis.

How do I simply tell ggplot to delete the text of the first and last x-axis labels? This would be the first Nov and the last Dec. Note that these do not exists in my df data frame at all so dplyr filters probably won't work.

enter image description here

3 Answers 3

2

I think this is what you want. The date_breaks are unnecessary.

ggplot(df, aes(date, value)) + 
  geom_col() + 
  scale_x_date(date_labels = "%b", breaks = df$date) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

Resulting plot

Sign up to request clarification or add additional context in comments.

Comments

2

You could achieve what you want by setting breaks using seq.date:

library(tidyverse);library(lubridate)

df <- data.frame(date = as.Date(c("2017-12-01", "2018-01-01", "2018-02-01", 
                                  "2018-03-01", "2018-04-01", "2018-05-01", 
                                  "2018-06-01", "2018-07-01", "2018-08-01", 
                                  "2018-09-01", "2018-10-01", "2018-11-01")), 
                 value = c(0.567859562, 0.514907158, 0.035399304, 0.485728823, 
                           0.925127361, 0.237531067, 0.301930968, 0.133373326, 
                           0.082275426, 0.464255614, 0.2366749, 0.652084264))



ggplot(df, aes(date, value)) + 
  geom_col() + 
  scale_x_date(
               date_labels = "%b",
               breaks = seq.Date(ymd("2017-12-01"),ymd("2018-11-01"), by = "month")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

enter image description here

1 Comment

Is the sequence necessary in this instance? I can see that it is a useful and more flexible tool for selecting labels, so good to know, but my understanding is that the questioner simply wants to the display the dates that are in their df, rather than the bookend dates added by date_breaks - so they can simply refer to those instead.
0

I would suggest researching the Lubridate package in R - you would be able to convert any messy date values into approrpiate POSIXT format and can also extract month information really easily - you could also convert those dates into a single column of the actual months, and use that as your axis label, which is cleaner as you would have an additional column of just the corresponding month - you can also fill based on that month and do some other cool stuff!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.