使用ggplot2使每个Nth轴标签加粗 [英] Make every Nth axis label bold with ggplot2

查看:410
本文介绍了使用ggplot2使每个Nth轴标签加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用ggplot2使轴上的第N个勾号加粗?我只想让轴打勾粗线(小线),而不是文本.

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.

当在图表中显示每日数据时,这对于突出显示每个第7个滴答很有用.我想每天保留刻度线.

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!

编辑:这是一个代码示例.我在最后一行扩展了它,以增加所有刻度的大小.我想要的是将每个Nth标签都变大.例如,每7日一次.手动定义每7个数字将不起作用,因为我正在处理每天都会变化的数据.

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))  

解决方案::感谢@Jimbou! 使用axis.ticks.x = element_line(colour = "black", size = c(2, rep(1,6))) 导致这一点.非常适合每日数据!

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!

推荐答案

您可以尝试:

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))))

将每个6th和文本大小用于说明目的. 您也可以使用中断和标签,但是在复制所有中断和标签时,必须事先使用跟随,而必须先删除它们.

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)]) 

这篇关于使用ggplot2使每个Nth轴标签加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆