Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 2.31 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Passing variable with line types to ggplot linetype
  2. Error: Aesthetics must either be length one, or the same length as the dataProblems:lty
  3.        
  4. > simulation_long_index[16:24,]    
  5.       year    geography    value
  6. 16    2018    sfr_2    101.1871
  7. 17    2019    sfr_2    101.1678
  8. 18    2020    sfr_2    101.2044
  9. 19    2012    sfr_3    100.0000
  10. 20    2013    sfr_3    100.1038
  11. 21    2014    sfr_3    100.2561
  12. 22    2015    sfr_3    100.0631
  13. 23    2016    sfr_3    100.8071
  14. 24    2017    sfr_3    101.2405
  15.        
  16. lty <- data.frame(lty=letters[1:12][sample(1:12, 35,replace=T)])
  17.  
  18. g3<-ggplot(data=simulation_long_index,
  19.    aes(
  20.      x=as.factor(year),
  21.      y=value,
  22.      colour=geography,
  23.      group=geography,
  24.      linetype=lty$lty))+
  25.        geom_line(size=.65) +
  26.        scale_colour_manual(values=manyColors(35)) +
  27.      geom_point(size=2.5) +
  28.      opts(title="growth")+
  29.      xlab("Year") +
  30.      ylab(paste("Indexed Value (Rel. to 2012")) +
  31.      opts(axis.text.x=theme_text(angle=90, hjust=0))
  32.  
  33. print(g3)
  34.        
  35. scale_linetype_manual("",values=lty$lty) +
  36.        
  37. ## some dummy data
  38. simulations<- expand.grid(year = 2012:2020, geography = paste0('a',1:35))
  39. library(plyr)
  40. library(RColorBrewer)
  41. simulation_long_index <- ddply(simulations, .(geography), mutate,
  42.   value = (year-2012) * runif(1,-2, 2) + rnorm(9, mean = 0, sd = runif(1, 1, 3)))
  43. ## create a manyColors function
  44. manyColors <- colorRampPalette(brewer.pal(name = 'Set3',n=11))
  45.        
  46. lty <- setNames(sample(1:12,35,T), levels(simulation_long_index$geography))
  47.        
  48. lty
  49. ## a1  a2  a3  a4  a5  a6  a7  a8  a9 a10 a11 a12 a13 a14 a15 a16
  50. ## 7   5   8  11   2  10   3   2   5   4   6   6  11   8   2   2
  51. ## a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32
  52. ## 12   7   6   8  11   5   1   1   8  12   8   1  12   2   3   5
  53. ## a33 a34 a35
  54. #7   1   3
  55.        
  56. ggplot(data=simulation_long_index,
  57.         aes(
  58.           x=as.factor(year),
  59.           y=value,
  60.           colour=geography,
  61.           group=geography,
  62.           linetype = geography))+
  63.             geom_line(size=.65) +
  64.             scale_colour_manual(values=manyColors(35)) +
  65.             geom_point(size=2.5) +
  66.             opts(title="growth")+
  67.             xlab("Year") +
  68.             ylab(paste("Indexed Value (Rel. to 2012")) +
  69.             opts(axis.text.x=theme_text(angle=90, hjust=0)) +
  70.             scale_linetype_manual(values = lty)