Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #using ggplot
  2. library(ggplot2) # Visualisation
  3. library(dplyr) # data wrangling
  4. library(scales) # formatting
  5.  
  6. #read file
  7. weight.eg = read.csv("Dummy Data.csv", header = FALSE, sep =
  8. ";",encoding = "UTF-8")
  9.  
  10. #change column names
  11. colnames(weight.eg) <- c ("unit","weight","year")
  12.  
  13. #as weight column is factor change into integer
  14. weight.eg$weight = as.numeric(levels(weight.eg$weight))
  15. [as.integer(weight.eg$weight)]
  16. weight.eg$year = as.numeric(levels(weight.eg$year))
  17. [as.integer(weight.eg$year)]
  18. #Nas are introduced, remove
  19. weight.eg <- na.omit(weight.eg)
  20.  
  21. #Sum of the total weight
  22. sum_total_weight = sum(weight.eg$weight)
  23.  
  24. #First layer
  25. firstLevel = weight.eg %>% summarize(total_weight=sum(weight))
  26. sunburst_0 = ggplot(firstLevel) # Just a foundation
  27.  
  28. #this will generate a bar chart
  29. sunburst_1 =
  30. sunburst_0 +
  31. geom_bar(data=firstLevel, aes(x=1, y=total_weight),
  32. fill='darkgrey', stat='identity') +
  33. geom_text(aes(x=1, y=sum_total_weight/2, label=paste("Total
  34. Weight", comma(total_weight))), color='black')
  35. #View
  36. sunburst_1
  37. #this argument is used to rotate the plot around the y-axis which
  38. the total weight
  39. sunburst_1 + coord_polar(theta = "y")
  40.  
  41. sunburst_2=
  42. sunburst_1 +
  43. geom_bar(data=weight.eg,
  44. aes(x=2, y=weight.eg$weight, fill=weight.eg$weight),
  45. color='white', position='stack', stat='identity', size=0.6)
  46. +
  47. geom_text(data=weight.eg, aes(label=paste(weight.eg$unit,
  48. weight.eg$weight), x=2, y=weight.eg$weight), position='stack')
  49.  
  50. sunburst_2 + coord_polar(theta = "y")
  51.  
  52. sunburst_3 =
  53. sunburst_2 +
  54. geom_bar(data=weight.eg,
  55. aes(x=3, y=weight.eg$weight,fill=weight.eg$weight),
  56. color='white', position='stack', stat='identity',
  57. size=0.6)+
  58. geom_text(data = weight.eg,
  59. aes(label=paste(weight.eg$year),x=3,y=weight.eg$weight),position =
  60. 'stack')
  61.  
  62.  
  63. sunburst_3 + coord_polar(theta = "y")
  64.  
  65. sunburst_3 + scale_y_continuous(labels=comma) +
  66. scale_fill_continuous(low='white', high='darkred') +
  67. coord_polar('y') + theme_minimal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement