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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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 谷歌趋势与周,ggplot2_R_Date_Ggplot2 - Fatal编程技术网

R 谷歌趋势与周,ggplot2

R 谷歌趋势与周,ggplot2,r,date,ggplot2,R,Date,Ggplot2,当我从Google Trend下载数据时,数据集如下所示: Week nuclear atomic nuclear.weapons unemployment 2004-01-04 - 2004-01-10 11 11 1 15 2004-01-11 - 2004-01-17 11 13 1 13 2004-01-18 - 2004-01-24 10 11 1 1

当我从Google Trend下载数据时,数据集如下所示:

                 Week   nuclear atomic  nuclear.weapons unemployment
2004-01-04 - 2004-01-10    11    11        1    15
2004-01-11 - 2004-01-17    11    13        1    13
2004-01-18 - 2004-01-24    10    11        1    13
如何将“周”中的日期从格式“Y-m-d-Y-m-d”更改为类似“年-周”的格式

此外,我如何告诉ggplot,只有年份打印在x轴上,而不是x的所有值

@马特里翁:谢谢。我听从了你的建议:

trends <- melt(trends, id = "Woche", 
    measure = c("nuclear", "atomic", "nuclear.weapons", "unemployment"))
trends$Week<- gsub("^(\\d+-\\d+-\\d+).+", "\\1", trends$Week)
trends$Week <- as.Date(trends$Week)

ggplot(trends, aes(Week, value, colour = variable, group=variable)) + 
  geom_line() + 
  ylab("Trends") +
  theme(legend.position="top", legend.title=element_blank(), 
      panel.background = element_rect(fill = "#FFFFFF", colour="#000000"))+
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9", "#009E73"))+
  stat_smooth(method="loess")

trends
ggplot
将理解
Date
对象(请参见
?Date
),并制定适当的标签(如果您可以将日期转换为此格式)

您可以使用类似于
gsub
的方法提取每周的开始日期。这将使用正则表达式匹配第一个参数并返回括号集内的任何内容:

df$startingDay <- gsub("^(\\d+-\\d+-\\d+).+", "\\1", df$Week)
然后,您可以使用日期对象打印想要打印的内容:

g <- ggplot(df, aes(date, as.numeric(atomic))) + geom_line()
print(g)

g太好了,谢谢!但是我怎么能让ggplot每两年而不是每年绘制一次呢?@feder80回答说,你需要给我一个例子,说明你是如何使用ggplot2的,以及你正在绘制什么样的图形。ggplot(趋势,aes(周,值,颜色=变量,组=变量))+geom_线()+ylab(“趋势”)+主题(legend.position=“top”,legend.title=element_blank(),panel.background=element_rect(fill=“#FFFFFF”,color=“#000000”)+scale_color_手册(value=c(“#999999”、“#E69F00”、“#56B4E9”、“#009E73”)+stat#平滑(method=“leash”)@feder80请显示从读取数据到生成图形的工作时间(您可以根据问题编辑)。查看您对数据所做的任何转换总是很有用的。我至少可以看到你似乎在制作某种时间序列折线图,但你为什么要每隔一年绘制一次呢?这将在时间序列中留下空白。@feder80我已更新了我的答案以回答您的上一个问题。由于“周”值中有空格,您的数据示例有点难以输入到R中。请查看提供您正在使用的数据示例的其他方法,特别是在本例中,最好使用
dput(head(my.data.frame))
g <- ggplot(df, aes(date, as.numeric(atomic))) + geom_line()
print(g)
library(scales)
g <- g + scale_x_date(breaks=date_breaks(width="1 year"), 
    labels=date_format("%Y"))