0

My graph has on the y-axis numerical values of my data which is the level of depression and on the the x-axis I have order (numbers from 1-40 because I have 40 observations) But these are in fact quarters as my data is quarterly (2008-2013). So I would like to change the x-axis from an order of 1-40 to Year and Quarter (e.g. 2008 Q1,2008 Q2,..). I am however not sure how I can do that. Any help is greatly appreciated!

tonytonov
  • 25,060
  • 16
  • 82
  • 98
David Resch
  • 113
  • 1
  • 9
  • Please provide a minimal reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – tonytonov Mar 14 '15 at 15:13

1 Answers1

1

Here's an example with your 40 quarters and starting in 2008:

quarter       <- seq(40)
starting.year <- 2008

#create a function
convertToQ <- function(qs, s) {
  d <- c()
  for(q in qs){
    qtr <- (q-1)%%4 +1
    d <- c(d, (paste(s, " Q", qtr, sep = "")))
    if(qtr == 4) s <- s +1
  }
  return(d)
}

# generate data frame
data <- data.frame(depression = runif(40, -5.0, 5.0),
                   quarters   = convertToQ(quarter, starting.year),
                   stringsAsFactors=FALSE)

# plot
ggplot(data, aes(x = quarters, y = depression)) +
  geom_point() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

This produces the following plot: quarter years ggplot geom point

Mehdi Nellen
  • 8,486
  • 4
  • 33
  • 48
  • For the generate data frame part I used : Depressiongraph <- Finalcombined(DepressionIndex = runif(40, -5.0, 5.0), quarters = convertToQ(quarter, starting.year), stringsAsFactors=FALSE) BUT I GOT THE ERROR could not find function "Finalcombined" – David Resch Mar 16 '15 at 13:01
  • Did you make the `Finalcombined` function yourself? It seems not to be defined according to the error. Did you try to change `Finalcombined` to `data.frame`? – Mehdi Nellen Mar 16 '15 at 13:16
  • It worked but how would I be able to get the actual values that I have for depression instead of values from 1-5? Right now the actual values are in a dataframe called Finalcombined and called DepressionIndex – David Resch Mar 16 '15 at 13:34
  • If your dataframe is called **Finalcombined** and the column is called **DepressionIndex** you can just change `runif(40, -5.0, 5.0)` to `Finalcombined$DepressionIndex` (or to `Finalcombined[,3]` where 3 is the column number of your depression data). – Mehdi Nellen Mar 16 '15 at 13:41
  • Note however that the length of your depression data should the same length as the number of quarters you have in your data. otherwise an error will pop up saying something about unequal lengths – Mehdi Nellen Mar 16 '15 at 13:43