Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. Region1 Region2
  2. 2007 17 55
  3. 2008 26 43
  4. 2009 53 70
  5. 2010 96 58
  6.  
  7. > test <- read.table("/tmp/data.txt")
  8. > png(filename="/tmp/test.png", height=1000, width=750, bg="white", res=300)
  9. > plot(test$Region1, type="b", col="blue", ylim=c(0,100), lwd=3)
  10. > lines(test$Region2, type="b", col="red", lwd=3)
  11. > dev.off()
  12.  
  13. > d <- read.table("/tmp/data.txt")
  14. > png <- png(filename="/tmp/test.png", height=1000, width=750)
  15. > myLayout <- loadPredefinedLayout("NiceKeynoteLayout")
  16. > coolplot(d, layout=myLayout, out=png)
  17.  
  18. data <- data.frame(Year=seq(as.Date("2007-01-01"),
  19. as.Date("2010-01-01"), by="year"),
  20. Region1=c(17,26,53,96), Region2=c(55,43,70,58))
  21.  
  22. library(ggplot2)
  23.  
  24. # Convert data to a form optimised for visualisation, not
  25. # data entry
  26. data2 <- melt(data, measure = c("Region1", "Region2"))
  27.  
  28. # Define the visualisation you want
  29. ggplot(data2, aes(x = Year, y = value, colour = variable)) +
  30. geom_line()
  31.  
  32. test <- read.table("/tmp/test.txt", header=TRUE)
  33. png(filename="/tmp/test.png", height=750, width=1000,
  34. bg="white", res=300)
  35. par(mar=c(2.5,2.5,0.75,0.75),
  36. family="Gill Sans", font=1, # font 2 would be bold
  37. cex=0.75, cex.lab=0.75, cex.axis=0.75)
  38. mymax <- max(test$Region1, test$Region2)*1.25
  39.  
  40. plot(test$Region1, type="b", col="#304E67",
  41. ylim=c(0, mymax), lwd=3,
  42. bty="l", axes=FALSE, ann=FALSE, cex=1.0, tck=1)
  43.  
  44. axis(1, lwd.ticks=0, at=1:length(test$Year), lab=test$Year)
  45. axis(2, lwd=0, las=1, at=c(0,25,50,75,100), yaxp=c(0,100,4))
  46. # grid(nx = NA, ny = 5, col = "lightgray") # wrong, see axTicks
  47. for(y in c(25, 50, 75, 100)) {
  48. lines(rep(y, length(test$Region1)), type="l", col="lightgray", lwd=1)
  49. }
  50.  
  51. lines(test$Region1, type="b", col="#304E67", lwd=3)
  52. lines(test$Region2, type="b", col="#974449", lwd=3)
  53.  
  54. # title(xlab="Year", col.lab=rgb(0,0.5,0))
  55. # title(ylab="Output", col.lab=rgb(0,0.5,0))
  56. legend(1, mymax+8, c("Region 1","Region 2"), cex=0.75,
  57. col=c("#304E67" ,"#974449"),
  58. pch=1:1, # circles
  59. lty=1:1, # solid
  60. lwd=1.5, # line width
  61. bty="n") # no box around
  62.  
  63. dev.off()
  64.  
  65. Year Region1 Region2
  66. 2007 17 55
  67. 2008 26 43
  68. 2009 53 70
  69. 2010 96 58
  70.  
  71. R> data <- data.frame(Year=seq(as.Date("2007-01-01"),
  72. as.Date("2010-01-01"), by="year"),
  73. Region1=c(17,26,53,96), Region2=c(55,43,70,58))
  74. R> data
  75. Year Region1 Region2
  76. 1 2007-01-01 17 55
  77. 2 2008-01-01 26 43
  78. 3 2009-01-01 53 70
  79. 4 2010-01-01 96 58
  80. R> par(mar=c(3,4,1,1))
  81. R> plot(data$Year, data$Region1, type='l', col='blue', ylab="Values")
  82. R> lines(data$Year, data$Region2, col='red')
  83. R>
  84.  
  85. plot <- ggplot(data2, aes(Year, value, group = variable,
  86. colour = variable)) + geom_line(size = 1) +
  87. opts(legend.position = "none")
  88. plot <- plot + geom_point () + opts(legend.position = "none")
  89. plot + geom_text(data = data2[data2$year == 2010,
  90. ], aes(label = variable), hjust = 1.2, vjust = 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement