Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #using ggplot
- library(ggplot2) # Visualisation
- library(dplyr) # data wrangling
- library(scales) # formatting
- #read file
- weight.eg = read.csv("Dummy Data.csv", header = FALSE, sep =
- ";",encoding = "UTF-8")
- #change column names
- colnames(weight.eg) <- c ("unit","weight","year")
- #as weight column is factor change into integer
- weight.eg$weight = as.numeric(levels(weight.eg$weight))
- [as.integer(weight.eg$weight)]
- weight.eg$year = as.numeric(levels(weight.eg$year))
- [as.integer(weight.eg$year)]
- #Nas are introduced, remove
- weight.eg <- na.omit(weight.eg)
- #Sum of the total weight
- sum_total_weight = sum(weight.eg$weight)
- #First layer
- firstLevel = weight.eg %>% summarize(total_weight=sum(weight))
- sunburst_0 = ggplot(firstLevel) # Just a foundation
- #this will generate a bar chart
- sunburst_1 =
- sunburst_0 +
- geom_bar(data=firstLevel, aes(x=1, y=total_weight),
- fill='darkgrey', stat='identity') +
- geom_text(aes(x=1, y=sum_total_weight/2, label=paste("Total
- Weight", comma(total_weight))), color='black')
- #View
- sunburst_1
- #this argument is used to rotate the plot around the y-axis which
- the total weight
- sunburst_1 + coord_polar(theta = "y")
- sunburst_2=
- sunburst_1 +
- geom_bar(data=weight.eg,
- aes(x=2, y=weight.eg$weight, fill=weight.eg$weight),
- color='white', position='stack', stat='identity', size=0.6)
- +
- geom_text(data=weight.eg, aes(label=paste(weight.eg$unit,
- weight.eg$weight), x=2, y=weight.eg$weight), position='stack')
- sunburst_2 + coord_polar(theta = "y")
- sunburst_3 =
- sunburst_2 +
- geom_bar(data=weight.eg,
- aes(x=3, y=weight.eg$weight,fill=weight.eg$weight),
- color='white', position='stack', stat='identity',
- size=0.6)+
- geom_text(data = weight.eg,
- aes(label=paste(weight.eg$year),x=3,y=weight.eg$weight),position =
- 'stack')
- sunburst_3 + coord_polar(theta = "y")
- sunburst_3 + scale_y_continuous(labels=comma) +
- scale_fill_continuous(low='white', high='darkred') +
- coord_polar('y') + theme_minimal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement