Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R x轴为数字(周数)时每4个月中断一次ggplot2_R_Date_Ggplot2 - Fatal编程技术网

R x轴为数字(周数)时每4个月中断一次ggplot2

R x轴为数字(周数)时每4个月中断一次ggplot2,r,date,ggplot2,R,Date,Ggplot2,我有一个从2000年到2016年的数据框架和一些我想绘制的数据。然而,如果我们连续两年比较,日期是不相等的:首先,日期的值可能不同,其次,我有时在两年之间有一个或多或少的值 2000年、2001年和2002年的数据,带有头部和尾部: numWeek effectif numYear numWeektest numWeek effectif numYear numWeektest numWeek effectif numYear numWeektest 1

我有一个从2000年到2016年的数据框架和一些我想绘制的数据。然而,如果我们连续两年比较,日期是不相等的:首先,日期的值可能不同,其次,我有时在两年之间有一个或多或少的值

2000年、2001年和2002年的数据,带有
头部
尾部

      numWeek effectif numYear numWeektest         numWeek effectif numYear numWeektest        numWeek effectif numYear numWeektest
1: 2000-01-03        1    2000           1   1: 2001-01-01       13    2001           1  1: 2002-01-07       77    2002           1
2: 2000-01-10        0    2000           2   2: 2001-01-08       22    2001           2  2: 2002-01-14       95    2002           2
3: 2000-01-17        0    2000           3   3: 2001-01-15       21    2001           3  3: 2002-01-21       69    2002           3
4: 2000-01-24        0    2000           4   4: 2001-01-22       11    2001           4  4: 2002-01-28       91    2002           4
5: 2000-01-31        0    2000           5   5: 2001-01-29       24    2001           5  5: 2002-02-04       89    2002           5

      numWeek effectif numYear numWeektest       numWeek effectif numYear numWeektest       numWeek effectif numYear numWeektest
48: 2000-11-27       14    2000          48 49: 2001-12-03       53    2001          49 48: 2002-12-02      112    2002          48
49: 2000-12-04       14    2000          49 50: 2001-12-10       65    2001          50 49: 2002-12-09      171    2002          49
50: 2000-12-11       12    2000          50 51: 2001-12-17       66    2001          51 50: 2002-12-16      132    2002          50
51: 2000-12-18       13    2000          51 52: 2001-12-24       17    2001          52 51: 2002-12-23       35    2002          51
52: 2000-12-25        4    2000          52 53: 2001-12-31       28    2001          53 52: 2002-12-30       54    2002          52
numWeektest
是一周的编号。你可以看到,我有时有一年的,
52
53

所以我所做的就是得到一周的数字,按几年的时间来划分,我得到:

它工作得很好,但我想做的是将x轴断开4个月,但我不能这样做,因为我的x轴是
数字
,而不是
日期
。我试图改变x轴,但我得到了以下结果:

那我该怎么办呢

代码:

test_p <-ggplot(test_c, aes(x = numWeek, y = effectif, group = numYear, colour = as.factor(numYear))) + 
  geom_line() +
  geom_point() +
  theme_bw() + 
  scale_y_discrete(expand = c(0,0)) +
  #scale_x_discrete(expand = c(0,0.5)) replace discrete or date according to the x axis values
  scale_x_date(expand = c(0,0.5))

test\u p您可以这样做:

test_c$dateNew <- as.Date(paste("2001-",format(test_c$numWeek, format="%m-%d")), format = "%Y-%m-%d")

test_p <-ggplot(test_c, aes(x = dateNew, y = effectif, group = numYear, colour = as.factor(numYear))) + 
  geom_line() +
  geom_point() +
  theme_bw() + 
  scale_y_discrete(expand = c(0,0)) +
  scale_x_date(date_breaks = "4 months", date_labels = "%B")
其中:


你能详细说明一下x轴的断裂时间是4个月吗,请?@RobinGertenbach哦,是的,我希望x轴上每4个月有一条线,这样它将从1月开始,然后是5月,然后是9月,而不是weeks@RobinGertenbach如果你想知道我打破每4个月的恶作剧是什么意思的话,我会给出预期的结果。这很简单,但很聪明,我没有想过。你的答案有一个小“问题”,我想把每个因子的值保持在同一条x线上,这就是为什么我,首先,试着用周数来做这件事。我不知道你是否得到它,但举例来说,蓝色的第一个值比其他两个值更靠近右边。但我不知道是否有可能做这样的事。(如果你有任何想法,告诉我)。无论如何,谢谢你的回答。:)
test_c$dateNew <- as.Date(paste("2001-",format(test_c$numWeek, format="%m-%d")), format = "%Y-%m-%d")

test_p <-ggplot(test_c, aes(x = dateNew, y = effectif, group = numYear, colour = as.factor(numYear))) + 
  geom_line() +
  geom_point() +
  theme_bw() + 
  scale_y_discrete(expand = c(0,0)) +
  scale_x_date(date_breaks = "4 months", date_labels = "%B")
 scale_x_date(breaks = as.Date(c("2001-01-01","2001-05-01","2001-09-01")), date_labels = "%B")