r - 如何在ggplot中将每一天添加为x轴标签

标签 r ggplot2

我想用 x 轴上标记的每一天绘制一个图。

这是我的数据

my_data <- read.table(text="day value
11/15/19    0.23633
11/16/19    0.28485
11/17/19    0.63127
11/18/19    0.15434
11/19/19    0.47964
11/20/19    0.65967
11/21/19    0.48741
11/22/19    0.84541
11/23/19    0.10123
11/24/19    0.78169
11/25/19    0.23189
11/26/19    0.86665
11/27/19    0.55184
11/28/19    0.81410
11/29/19    0.25821
11/30/19    0.23576
12/1/19 0.46397
12/2/19 0.55764
12/3/19 0.95645
12/4/19 0.63954
12/5/19 0.76766
12/7/19 0.74505
12/8/19 0.65515
12/9/19 0.58222
12/10/19    0.17294", header=TRUE, stringsAsFactors=FALSE)

这是我的代码

  my_data %>% 
  ggplot(aes(day, value)) +
  geom_line() +
  scale_x_continuous(breaks = seq(1, nrow(my_data)),
                     labels = my_data$day)

它给了我这个错误:as.Date.numeric(value) 中的错误:必须提供 'origin'

我想让每一天都显示在 x 轴上,默认情况下它只显示包含在该数据范围内的几天。

最佳答案

尝试使用 scale_x_date 而不是 scale_x_continuous

my_data %>% 
  ggplot(aes(x = mdy(day), value)) +
  geom_line() +
  scale_x_date(date_breaks = "1 day")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

关于r - 如何在ggplot中将每一天添加为x轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290492/

相关文章:

regex - R从字符串中提取第一个数字

r - 使用 geom_bar 按列中一个特定值的计数对 y 轴进行排序

r - 列明智 [R] 矩阵乘法

r - R : Leaving out undefined columns when subsetting a dataframe

r - 取决于 R 中多个条件的混合数据帧的总和

r - 如何将抖动添加到 ggpairs 中的散点图矩阵?

r - 如何使用ggplot将两个不同比例的y轴放在图的同一侧?

xml - 将 Google Finance 投资组合拉入 R

r - 垂直对齐 ggplot2 绘图

Python ggplot 和 ggplotly