0

When I am downloading data from Google Trend, the dataset looks like this:

                 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

How can I change the dates in "Week" from this format "Y-m-d - Y-m-d" to a format like "Year-Week"?

Furthermore, how can I tell ggplot, that it only the years are printed on the x-axes instead of all values for x?

@Mattrition: Thank you. I followed your advice:

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

Now, every second year is labeled (2004, 2006, ...) in x-axis. How can I tell ggplot to label every year (2004, 2005, ...)?

tonytonov
  • 25,060
  • 16
  • 82
  • 98
feder80
  • 1,195
  • 3
  • 13
  • 34
  • Your data example is a bit difficult to feed in to R due to the spaces in the "Week" values. Have a look at [How to make a great r reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for other ways to provide an example of the data you are using, particulary in this instance it might be better to use the results of `dput(head(my.data.frame))` – MattLBeck Dec 10 '13 at 14:15

1 Answers1

1

ggplot will understand Date objects (see ?Date) and work out appropriate labelling if you can convert your dates to this format.

You can use something like gsub to extract starting day for each week. This uses regular expressions to match the first argument and return anything inside the set of brackets:

df$startingDay <- gsub("^(\\d+-\\d+-\\d+).+", "\\1", df$Week)

Then call as.Date() on the extracted day strings to convert to Date objects:

df$date <- as.Date(df$startingDay)

You can then use the date objects to plot whatever you wanted to plot:

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

EDIT:

To answer your additional question, add the following to your ggplot object:

library(scales)
g <- g + scale_x_date(breaks=date_breaks(width="1 year"), 
    labels=date_format("%Y"))
MattLBeck
  • 5,701
  • 7
  • 40
  • 56
  • Perfect, thank you! But how can I tell ggplot to plot every second year instead of every year? – feder80 Dec 10 '13 at 14:15
  • @feder80 To answer that you will need to give me an example of how you are using ggplot2 and what kind of graph you are plotting. – MattLBeck Dec 10 '13 at 14:25
  • 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") – feder80 Dec 10 '13 at 14:30
  • @feder80 Please show your working from when you read in your data to when you produce your graph (you can edit it in to your question). It's always useful to see any transformations you have made on your data. I can at least see you seem to be making some sort of time series line graph, but why would you want to plot every other year? This would leave gaps in the time series. – MattLBeck Dec 10 '13 at 14:36
  • @feder80 I have updated my answer in response to your last question. – MattLBeck Dec 10 '13 at 15:20