Customizing Your Data Visualization with ggplot2 in R

harug n.w
1 / 16
Embed
Share

Explore the principles of building visualizations using ggplot2 in R, from mapping data to aesthetics to customizing bar plots. Learn how to adjust data classification and create stunning visualizations using the ggplot2 cheat sheet.

  • Data Visualization
  • ggplot2
  • R Programming
  • Data Classification
  • Bar Charts

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. HARUG

  2. Using Basic ggplot2 Principle of building lines of scripts with aesthetics and geoms Using the ggplot2 cheat sheet Data classification Bar charts Scatter graphs R resource https://books.google.co.uk/books/about/Ggplot2.html?id=XgFkDAAAQBAJ&p rintsec=frontcover&source=kp_read_button&redir_esc=y#v=onepage&q&f=f alse

  3. What you need Data Aesthetics maps the data to graphical outputs -> (aes()) Layers (what you see; points lines, bars) -> geom_something()

  4. Adjusting Data to desired classification Some data might need to be changed into factors if it is catagorical your_data$column<-as.factor(your_data$column) In the accompanying script murders$politics<-as.factor(murders$politics)

  5. The Aesthetic function ggplot(data,aes(x=factor)) This line of code describes the data and the aesthetic mapping This generates a blank canvas (with our x axis) This is an object within R ggplot(murders, aes(x=region)) The aes() function connects data with what we see on the graph

  6. Adding a geom (layer) We add to this the layer > ggplot(murders,aes(x=region) +geom_bar() Note: geom_bar() produces a count by default, this can be changed

  7. Using Geom_bar for Y values ggplot(murders,aes(x=region,y=total))+geom_bar(stat="identity") We don t need the x and y labels they will be assumed by ggplot2

  8. Customizing the bar plot with the cheat sheet ggplot(murders, aes(x=region,y=total))+geom_bar(stat="identity", aes(fill=region))+labs(y="Murders per region", title="US Murders") ggplot(murders, aes(x=region,y=total))+geom_bar(stat="identity", aes(alpha=region))+labs(y="Murders per region", title="US Murders") ggplot(murders, aes(x=region,y=total))+geom_bar(stat="identity", fill= hotpink , width=0.4)+labs(y="Murders per region", title="US Murders")

  9. Customizing the bar plot with the cheat sheet ggplot(murders, aes(x=region,y=total))+geom_bar(stat="identity", aes(fill=region))+labs(y="Murders per region", title="US Murders")+coord_flip() ggplot(murders, aes(x=region,y=total))+geom_bar(stat="identity", aes(alpha=region))+labs(y="Murders per region", title="US Murders")+coord_flip()

  10. Back to Counting Within the regions of the US we would like a visualisation off their contentment (totally made up) ggplot(murders, aes(x=region))+geom_bar(aes(fill=contentment))+labs (title = "State happiness within region )

  11. Faceting Why might they be unhappy? ggplot(murders, aes(x=region))+geom_bar(aes(fill=contentment)) +facet_wrap(~politics)+theme(axis.text.x=element_text(angle= 90))

  12. Scatter plots

  13. Coding a scatter plot ggplot(murders, aes(x=population/10^6,y=total)) The above creates an object but not points on the graph ggplot(murders, aes(x=population/10^6,y=total))+geom_point() By adding the geom_point() function on the end the points are rendered onto the plot

  14. Using the cheat sheet to jazz up the scatter graph ggplot(murders, aes(x=population/10^6,y=total))+geom_point(aes(fill=region) ,shape =21,alpha=0.5,size=4)

  15. Adding labels ggplot(murders, aes(x=population/10^6,y=total))+geom_point(aes(fill=region),shape =21,alpha=0.5,size=4)+geom_text(aes(population/10^6,total, label = abb),nudge_x = 2,size=3) The code is starting to get quite long p<-ggplot(murders,aes(x=population/10^6,y=total,label=abb)) p+geom_point(size=4,shape=21,aes(fill=region),alpha=0.5) +geom_text(nudge_x = 2, size=3) Can now use p and add more code to it

  16. Adding stuff to p p<-p+geom_text(nudge_x = 0.075, size=3)+geom_point(size=4,shape=21,aes(fill=region),alpha=0.5) +scale_x_log10()+scale_y_log10() p+xlab("Populations in millions (log scale)")+ylab("Total number of murderes (log scale)")+ggtitle("US Gun Murders in US 2010")

More Related Content