Make each Nth axis label bold with ggplot2

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

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

I could not find anything on this topic, any help would be appreciated!

EDIT: Here is a sample code. I expanded it on the last line to increase the size of all ticks. I would like every Nth shortcut to be larger. For example, every seventh. Defining one in seven manually will not work, as I work 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 @Jimbou! Using axis.ticks.x = element_line(colour = "black", size = c(2, rep(1,6))) leads to this. Ideal for daily data! enter image description here

+5
source share
1 answer

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. You can also use your breaks and shortcuts, but you should use the following, as you duplicate all breaks and labels, and they must be deleted earlier.

 scale_x_date(labels = lbls[!duplicated(lbls)], breaks = brks[!duplicated(lbls)]) 
+4
source

Source: https://habr.com/ru/post/1275536/


All Articles