6

Is it possible to make every Nth tick on an axis bold using ggplot2? I only want to have the axis tick bold (the small line), not the text.

This would be useful for highlighting every 7th tick when showing daily data in a plot. I'd like to keep the tick marks for every day.

I haven't been able to find anything regarding this topic, any help would be appreciated!

EDIT: Here is a code example. I've extended it at the last line to increase the size of all ticks. What I'd like is to have every Nth label larger. For example every 7th. Defining every 7th one manually wouldn't work since I'm working with data that changes every day.

library(ggplot2)
library(lubridate)
theme_set(theme_bw())

df <- economics_long[economics_long$variable %in% c("psavert", "uempmed"), ]
df <- df[lubridate::year(df$date) %in% c(1967:1981), ]

# labels and breaks for X axis text
brks <- df$date[seq(1, length(df$date), 12)]
lbls <- lubridate::year(brks)

# plot
ggplot(df, aes(x=date)) + 
  geom_line(aes(y=value, col=variable)) + 
  labs(title="Time Series of Returns Percentage", 
       subtitle="Drawn from Long Data format", 
       caption="Source: Economics", 
       y="Returns %", 
       color=NULL) +  # title and caption
  scale_x_date(labels = lbls, breaks = brks) +  # change to monthly ticks and labels
  scale_color_manual(labels = c("psavert", "uempmed"), 
                     values = c("psavert"="#00ba38", "uempmed"="#f8766d")) +  # line color
  theme(axis.text.x = element_text(angle = 90, vjust=0.5, size = 8),  # rotate x axis text
        panel.grid.minor = element_blank(),
        axis.ticks.x = element_line(colour = "black", size = 2))  

Solution: Thanks to @Jimbou! Using axis.ticks.x = element_line(colour = "black", size = c(2, rep(1,6))) leads to this. Perfect for daily Data! enter image description here

Nieuwefn
  • 83
  • 8
  • 1
    Hi, welcome to *Stack Overflow*, in order that we can help you, please provide example data and the steps you've tried so far. Consider [*How to make a great reproducible example*](https://stackoverflow.com/a/5963610/6574038), thank you. – jay.sf Feb 23 '18 at 13:57
  • 1
    Did you try [this approach](https://stackoverflow.com/a/20609878/9247058)? – lisah Feb 23 '18 at 14:00
  • Hey @lisah, I've added an example. Lisah maybe I could do something with ifelse to apply the last line of my example to only every 7th tick. Any ideas how I would go about doing that? – Nieuwefn Feb 23 '18 at 15:05
  • 1
    Hey @jaySf I've added an example. See above comment – Nieuwefn Feb 23 '18 at 15:06
  • @Jimbou How would that change the size of the actual axis tick. I don't want to change the text of the tick, I want to change the actual size of the tick itself. – Nieuwefn Feb 23 '18 at 15:19

1 Answers1

6

You can try:

ggplot(df, aes(x=date, y=value, color=variable)) + 
     geom_line() + 
     scale_color_manual(values = c("#00ba38", "#f8766d")) +
     scale_x_date(date_breaks = "1 year", date_labels ="%Y") +
     theme(axis.ticks.x = element_line(colour = "black", size = c(5, rep(1,5))),
       axis.text.x = element_text(angle = 90, vjust=0.5, size = c(15,rep(8,5))))

enter image description here

used every 6th and text size for illustration purposes. You can also use your breaks and labels but then you have to use following instead as you duplicated all breaks and labels and they must priorly removed.

scale_x_date(labels = lbls[!duplicated(lbls)], breaks = brks[!duplicated(lbls)]) 
Roman
  • 14,297
  • 1
  • 25
  • 40
  • OP wants the axis ticks to be changed, not the text size. – jay.sf Feb 23 '18 at 16:06
  • 1
    Hey, thanks a ton! I applied your logic to the ticks element to make it work! `axis.ticks.x = element_line(colour = "black", size = c(5, rep(1,5)))` When using it on daily data it would be `rep(1,6)` – Nieuwefn Feb 23 '18 at 16:06
  • I've accepted your answer but it would be nice if you could change your answer to increase the size of the tick itself not of the text. Just apply what I did in my previous comment. – Nieuwefn Feb 23 '18 at 16:11
  • Follow up question: Do you think it's also possible to change the length(downwards) of the tick itself? Answer to own question: Yes it is: using axis.ticks.length – Nieuwefn Feb 23 '18 at 16:33