我想绘制以下数据:
set.seed(5)
my_df <- data.frame(date = sample(seq(as.Date("2019-07-03"), as.Date("2019-07-30"), by = 1), 10), val_1 = 1:10, val_2 = 11:20)
my_df<- gather(my_df, "type", "value", -date)
ggplot(data=my_df, aes(x= date, y=value, colour = type)) + geom_line()
以上内容仅将某些日期显示为刻度,而不是全部显示10。如何显示所有日期为刻度。我尝试了scale_x_discrete(labels = unique(my_df$date))
,但没有成功。
答案 0 :(得分:2)
治疗日期作为因素
ggplot(data = my_df,
aes(x = as.factor(date), y = value, colour = type, group = type)) +
geom_line()
您还可以使用@ r2evans提到的scale_x_date:
ggplot(data=my_df, aes(x= date, y=value, colour = type)) +
geom_line() +
scale_x_date(breaks = unique(my_df$date), minor_breaks = NULL) +
#Added for readability
theme(axis.text.x = element_text(angle = 30, hjust = 1))