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

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.34 KB  |  hits: 10  |  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. Combined positive and negative stacked line plots
  2. p <- ggplot(dataForm, aes(x=Time,y=Value,group=Type),colour=factor(Type))
  3.     p + geom_line(aes(fill = Type),position = "stack")
  4.        
  5. p <- ggplot(dataForm, aes(x=Time,y=Value,group=Type),colour=factor(Type))
  6.     p + geom_line(data = data1,aes(fill = Type),position = "stack")
  7.     p + geom_line(data = data1,aes(fill = Type),position = "stack")
  8.        
  9. library(ggplot2)
  10. library(plyr)
  11.  
  12. data = read.table(text="Time    Type    Value
  13. 1   a   8
  14. 2   a   10
  15. 3   a   10
  16. 4   a   5
  17. 5   a   3
  18. 1   b   9
  19. 2   b   5
  20. 3   b   7
  21. 4   b   8
  22. 5   b   3
  23. 1   c   -3
  24. 2   c   -1
  25. 3   c   -5
  26. 4   c   -4
  27. 5   c   -7
  28. 1   d   -11
  29. 2   d   -3
  30. 3   d   -9
  31. 4   d   -6
  32. 5   d   -6", header=TRUE)
  33.  
  34. p <- ggplot(data, aes(x=Time))
  35. p <- p + geom_line(subset = .(Type %in% c('a', 'b')),
  36.                    aes(y=Value, colour = Type),
  37.                    position = 'stack')
  38. p <- p + geom_line(subset = .(Type %in% c('c', 'd')),
  39.                    aes(y=Value, colour = Type),
  40.                    position = 'stack')
  41. p
  42.        
  43. p <- ggplot(data, aes(x=Time))
  44. p <- p + geom_area(subset = .(Type %in% c('a', 'b')),
  45.                    aes(y=Value, fill=Type),
  46.                    position = 'stack')
  47. p <- p + geom_area(subset = .(Type %in% c('c', 'd')),
  48.                    aes(y=Value, fill = Type),
  49.                    position = 'stack')
  50. p <- p + geom_hline(yintercept=0)
  51. p