1

Hello I would like to create a plot with abbreviated month labels on the x-axis.

This is my data:

df <- structure(list(site = structure(c(33L, 34L, 35L, 36L, 37L, 38L,  39L, 40L, 41L, 42L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,  42L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 33L, 34L,  35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 33L, 34L, 35L, 36L, 37L,  38L, 39L, 40L, 41L, 42L), .Label = c("INDU01", "INDU02", "INDU03",  "INDU04", "INDU05", "INDU06", "INDU07", "INDU08", "INDU09", "INDU10",  "MISS01", "MISS02", "MISS03", "PIRO01", "PIRO02", "PIRO03", "PIRO04",  "PIRO05", "PIRO06", "PIRO07", "PIRO08", "PIRO09", "PIRO10", "SLBE03",  "SLBE04", "SLBE08", "SLBE09", "SLBE11", "V1DA1", "V3DA2", "V8DC2",  "VNPBP70", "APIS01", "APIS02", "APIS03", "APIS04", "APIS05",  "APIS06", "APIS07", "APIS08", "APIS10", "APIS11", "MISS04", "MISS08",  "MISS09", "SLBE01", "SLBE02", "SLBE05", "SLBE06", "SLBE07", "V12DA2",  "V4DC1", "V9DB1", "V9DC2", "VNPBP29", "VNPBP42", "GRPO02", "GRPO03",  "GRPO04", "GRPO05", "GRPO06", "GRPO07", "ISRO01", "ISRO02", "ISRO03",  "ISRO04", "ISRO05", "ISRO06", "ISRO07", "ISRO08", "ISRO09", "ISRO10",  "MISS05", "MISS06", "MISS07", "MISS10", "V10DA4", "V11DB4", "V12DC3",  "V5DA1", "V7DE2", "V7DF2", "GRPO01"), class = "factor"), log.start = structure(c(16218,  16218, 16218, 16218, 16218, 16218, 16218, 16218, 16218, 16218,  16540, 16541, 16550, 16553, 16542, 16543, 16542, 16553, 16553,  16552, 16905, 16905, 16905, 16906, 16905, 16905, 16905, 16905, 16905, 16905, 17270, 17270, 17290, 17290, 17289, 17289, 17291,  17288, 17290, 17290, 17622, 17622, 17655, 17655, 17655, 17655,  17655, 17655, 17655, 17655), class = "Date"), log.end = structure(c(16278,  16271, 16317, 16317, 16271, 16280, 16317, 16317, 16277, 16317,  16664, 16694, 16666, 16667, 16671, 16658, 16647, 16660, 16670,  16667, 17048, 17046, 17010, 17124, 17108, 17098, 17124, 17145,  17008, 17000, 17274, 17379, 17382, 17389, 17459, 17406, 17389,  17459, 17389, 17378, 17780, 17756, 17785, 17784, 17784, 17784,  17756, 17784, 17785, 17713), class = "Date"), year = c("2014",  "2014", "2014", "2014", "2014", "2014", "2014", "2014", "2014",  "2014", "2015", "2015", "2015", "2015", "2015", "2015", "2015",  "2015", "2015", "2015", "2016", "2016", "2016", "2016", "2016",  "2016", "2016", "2016", "2016", "2016", "2017", "2017", "2017",  "2017", "2017", "2017", "2017", "2017", "2017", "2017", "2018",  "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018",  "2018"), start.month.day = c("05-28", "05-28", "05-28", "05-28",  "05-28", "05-28", "05-28", "05-28", "05-28", "05-28", "04-15",  "04-16", "04-25", "04-28", "04-17", "04-18", "04-17", "04-28",  "04-28", "04-27", "04-14", "04-14", "04-14", "04-15", "04-14",  "04-14", "04-14", "04-14", "04-14", "04-14", "04-14", "04-14",  "05-04", "05-04", "05-03", "05-03", "05-05", "05-02", "05-04",  "05-04", "04-01", "04-01", "05-04", "05-04", "05-04", "05-04",  "05-04", "05-04", "05-04", "05-04"), end.month.day = c("07-27",  "07-20", "09-04", "09-04", "07-20", "07-29", "09-04", "09-04",  "07-26", "09-04", "08-17", "09-16", "08-19", "08-20", "08-24",  "08-11", "07-31", "08-13", "08-23", "08-20", "09-04", "09-02",  "07-28", "11-19", "11-03", "10-24", "11-19", "12-10", "07-26",  "07-18", "04-18", "08-01", "08-04", "08-11", "10-20", "08-28",  "08-11", "10-20", "08-11", "07-31", "09-06", "08-13", "09-11",  "09-10", "09-10", "09-10", "08-13", "09-10", "09-11", "07-01" )), row.names = c(NA, -50L), class = "data.frame")

And when I use ggplot...

library (ggplot2)
p <- ggplot(df, aes(y=year))
p <- p + geom_segment(aes(x=start.month.day,xend=end.month.day,y=year,yend=year),size=2)
p <- p + facet_wrap(~site,ncol=2)

I receive the following plot...

enter image description here

I would like abbreviated monthly labels on the x-axis such as 'Apr' 'Mar' 'Jun', etc.

Thanks

cherrytree
  • 1,561
  • 3
  • 16
  • 33
  • `p + scale_x_datetime(labels = scales::date_format("%b"))` – M-- Apr 04 '19 at 20:32
  • @M-M This will not work because the x-axis data are given as strings and not as date! – J_F Apr 04 '19 at 20:37
  • Correct, and I tried changing the x-axis data to date, but when I converted to a date, it kept including a year which doesn't work. – cherrytree Apr 04 '19 at 20:39
  • @J_F This does not work, but OP can tweak simple things to get this to work. – M-- Apr 04 '19 at 20:39
  • @cherrytree let it add the year. have a bogus year, which won't show up. let's say 1969! :D p.s. it's important to have the same year for all of them. – M-- Apr 04 '19 at 20:40
  • @M-M, if I let it add a year, it will mess up the geom_segment line because it will then try to plot a start date of "05-28-2019", I really need the start.month.day to be year-less. – cherrytree Apr 04 '19 at 20:46

1 Answers1

1

First of all, you want your segments to represent actual length of your period of time, so you need have them as actual dates. Otherwise, segments are not the right representation of periods.

Second, you need to have them as dates to be able to format them as dates.

if you convert the only month-day strings to dates, it will add the year (current year) by default, but you still have your year variable which takes care of y axis.

All these said, the code below works for you:

df[,5] <- as.Date(df[,5], format='%m-%d')
df[,6] <- as.Date(df[,6], format='%m-%d')

ggplot(df, aes(y=year)) + 
 geom_segment(aes(x= start.month.day,xend=end.month.day,
                  y=year,yend=year),size=2) + 
 facet_wrap(~site,ncol=2) 

ggplot will does the nice formatting of the x axis itself.

enter image description here

M--
  • 25,431
  • 8
  • 61
  • 93