avatarBenjamin Obi Tayo Ph.D.

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3740

Abstract

ics (BBY), Walgreens Pharmaceuticals (WGN), & Walmart Retail (WMT) over a 5-month period using the marketing_emails.csv dataset.</p><p id="9050">The code for this example can be downloaded from this repository: <a href="https://github.com/bot13956/barplot_marketing_emails">https://github.com/bot13956/barplot_marketing_emails</a>.</p><p id="cca0"><b>Import Necessary Libraries</b></p><div id="2ae0"><pre><span class="hljs-function"><span class="hljs-title">library</span><span class="hljs-params">(readr)</span></span> <span class="hljs-function"><span class="hljs-title">library</span><span class="hljs-params">(tidyverse)</span></span></pre></div><p id="6961"><b>Data Importation and Preparation</b></p><div id="2ac5"><pre><span class="hljs-built_in">df</span><-read.csv(<span class="hljs-string">"marketing_emails.csv"</span>) <span class="hljs-built_in">df</span><-<span class="hljs-built_in">df</span>%>%mutate(digit = <span class="hljs-built_in">seq</span>(5,1,-1)) <span class="hljs-built_in">df</span>%>%<span class="hljs-built_in">head</span>() df2<-data.frame(digit=c(<span class="hljs-built_in">df</span><span class="hljs-variable">digit</span>,<span class="hljs-built_in">df</span><span class="hljs-variable">digit</span>,<span class="hljs-built_in">df</span><span class="hljs-variable">digit</span>),<span class="hljs-built_in">type</span>=<span class="hljs-built_in">sort</span>(as.vector(replicate(5,c(<span class="hljs-string">"BBY"</span>,<span class="hljs-string">"WGN"</span>,<span class="hljs-string">"WMT"</span>)))), quantity=c(<span class="hljs-built_in">df</span><span class="hljs-variable">Best</span>,<span class="hljs-built_in">df</span><span class="hljs-variable">Walgreen</span>,<span class="hljs-built_in">df</span><span class="hljs-variable">Walmart</span>)) df2%>%<span class="hljs-built_in">head</span>(n=10)</pre></div><p id="7d76"><b>Generate Barplots for Data Visualization</b></p><div id="eadd"><pre>df2%>%ggplot(aes(digit,quantity,<span class="hljs-attribute">fill</span>=type))+geom_col(position = <span class="hljs-string">"dodge"</span>, width = 0.75)+ xlab(<span class="hljs-string">"month"</span>)+ ylab(<span class="hljs-string">"quantity"</span>)+ scale_x_continuous(breaks = as.integer(seq(1,5)), labels = c(<span class="hljs-string">'Jul'</span>,<span class="hljs-string">'Aug'</span>,<span class="hljs-string">'Sep'</span>,<span class="hljs-string">'Oct'</span>,<span class="hljs-string">'Nov'</span>))+ ggtitle(<span class="hljs-string">"quantity of marketing emails from BBY, WGN, & WMT in 2018"</span>)+theme_classic()+ theme( plot.title = element_text(<span class="hljs-attribute">color</span>=<span class="hljs-string">"black"</span>, <span class="hljs-attribute">size</span>=11, <span class="hljs-attribute">hjust</span>=0.5, <span class="hljs-attribute">face</span>=<span class="hljs-string">"bold"</span>), axis.title.x = element_text(<span class="hljs-attribute">color</span>=<span class="hljs-string">"black"</span>, <span class="hljs-attribute">size</span>=11, <span class="hljs-attribute">face</span>=<span class="hljs-string">"bold"</span>), axis.text.x = element_text(<span class="hljs-attribute">color</span>=<span class="hljs-string">"black"</span>, <span class="hljs-attribute">size</span>=11, <span class="hljs-attribute">face</span>=<span class="hljs-string">"bold"</span>), axis.text.y = element_text(<span class="hljs-attribute">color</span>=<span class="hljs-string">"black"</span>, <span class="hljs-attribute">size</span>=11, <span class="hljs-attribute">face</span>=<span class="hljs-string">"bold"</span>), axis.title.y = element_text(<span class="hljs-attribute">color</span>=<span cl

Options

ass="hljs-string">"black"</span>, <span class="hljs-attribute">size</span>=11, <span class="hljs-attribute">face</span>=<span class="hljs-string">"bold"</span>), legend.title = element_blank() )</pre></div><figure id="195c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KUHbCHwhBmyed1cXt6qPHw.png"><figcaption></figcaption></figure><h1 id="25ea">3. 2016 GDP Barplot for Selected Countries</h1><p id="5ec6">This code generates a barplot for 2016 GDP for selected countries using the gdp.csv dataset which can be obtained from this website: <a href="https://data.oecd.org/gdp/gross-domestic-product-gdp.htm">https://data.oecd.org/gdp/gross-domestic-product-gdp.htm</a>.</p><p id="104e">The code for this example can be downloaded from this repository: <a href="https://github.com/bot13956/GDP_barplot_using_R">https://github.com/bot13956/GDP_barplot_using_R</a>.</p><p id="9af6"><b>Import Necessary Libraries</b></p><div id="8385"><pre><span class="hljs-function"><span class="hljs-title">library</span><span class="hljs-params">(tidyverse)</span></span> <span class="hljs-function"><span class="hljs-title">library</span><span class="hljs-params">(scales)</span></span></pre></div><p id="b2a6"><b>Data Importation and Preparation</b></p><div id="bac2"><pre><span class="hljs-title">data</span><-read.csv(<span class="hljs-string">"gdp.csv"</span>) <span class="hljs-built_in">head</span>(<span class="hljs-title">data</span>) names(<span class="hljs-title">data</span>)[<span class="hljs-number">1</span>]=<span class="hljs-string">"location"</span> ind<-order(<span class="hljs-title">data</span>$Value) <span class="hljs-title">data</span><-<span class="hljs-title">data</span>%>%mutate(location=location[ind],Value=Value[ind])</pre></div><p id="257a"><b>Generate Barplot for Data Visualization</b></p><div id="1e0b"><pre><span class="hljs-selector-tag">data</span>%>%<span class="hljs-selector-tag">ggplot</span>(<span class="hljs-built_in">aes</span>(<span class="hljs-built_in">reorder</span>(location,Value),Value))+<span class="hljs-selector-tag">geom_col</span>(fill=<span class="hljs-string">"blue"</span>)+ <span class="hljs-selector-tag">scale_y_continuous</span>(breaks=<span class="hljs-built_in">seq</span>(<span class="hljs-number">0</span>,<span class="hljs-number">100000</span>,<span class="hljs-number">10000</span>), labels=<span class="hljs-built_in">c</span>(<span class="hljs-string">"0"</span>,<span class="hljs-string">"10k"</span>,<span class="hljs-string">"20k"</span>,<span class="hljs-string">"30k"</span>,<span class="hljs-string">"40k"</span>,<span class="hljs-string">"50k"</span>,<span class="hljs-string">"60k"</span>,<span class="hljs-string">"70k"</span>,<span class="hljs-string">"80k"</span>,<span class="hljs-string">"90k"</span>,<span class="hljs-string">"100k"</span>))+ <span class="hljs-selector-tag">theme</span>(axis.text.x = <span class="hljs-built_in">element_text</span>(angle = <span class="hljs-number">45</span>, hjust = <span class="hljs-number">1</span>,size=<span class="hljs-number">6</span>))+ <span class="hljs-selector-tag">theme</span>(axis.title.x=<span class="hljs-built_in">element_blank</span>())+ <span class="hljs-selector-tag">theme</span>(axis.title.y=<span class="hljs-built_in">element_blank</span>())+ <span class="hljs-selector-tag">ggtitle</span>(<span class="hljs-string">"Gross Domestic Product (GDP), Total, US dollars/Capita, 2016 "</span>)</pre></div><figure id="0da8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8roUlACKsawaz_mbhjo7tA.png"><figcaption></figcaption></figure><p id="9da1">In summary, we’ve shown how barplots can be generated using R’s ggplot package.</p></article></body>

Barplot using R ggplot | Towards AI

Tutorial on Barplots using R’s ggplot Package

Using R’s “ggplot” package for barplots of three different datasets

This tutorial will discuss how bar plots can be generated using R’s ggplot package using 3 examples. Another tutorial on data visualization using python’s matplotlib package can be found here: Tutorial on Data Visualization: Weather Data.

1. 2016 Market Share for Electric Vehicles

This code plots the global market share for electric vehicles (EV) for selected countries using the global_EV_2016.csv dataset: The global EV data obtained from this report: https://www.iea.org/publications/freepublications/publication/GlobalEVOutlook2017.pdf.

The code for this example can be downloaded from this repository: https://github.com/bot13956/2016_market_share_electric_cars_using_R.

Import Necessary Libraries

library(readr)
library(tidyverse)

Data Importation and Preparation

data<-read_csv("global_EV_2016.csv",col_names = FALSE)
head(data)
data<-data[-c(1,2),]
names(data)<-c("country","sales_bev","stock_bev","sales_phev","stock_phev","shares")
head(data)

Generate Barplot for Data Visualization

data%>%drop_na(shares)%>%mutate(shares=parse_number(shares))%>%
       filter(shares>=0.91)%>%
       ggplot(aes(reorder(country, shares),shares))+
       geom_col(fill="blue")+
       coord_flip()+ theme(axis.title.x=element_blank())+
       theme(axis.title.y=element_blank())+
       ggtitle("Market share of electric vehicles in selected countries (%)")

2. Barplot for personal marketing emails

This code produces barplots of personal marketing emails from Best Buy Electronics (BBY), Walgreens Pharmaceuticals (WGN), & Walmart Retail (WMT) over a 5-month period using the marketing_emails.csv dataset.

The code for this example can be downloaded from this repository: https://github.com/bot13956/barplot_marketing_emails.

Import Necessary Libraries

library(readr)
library(tidyverse)

Data Importation and Preparation

df<-read.csv("marketing_emails.csv")
df<-df%>%mutate(digit = seq(5,1,-1))
df%>%head()
df2<-data.frame(digit=c(df$digit,df$digit,df$digit),type=sort(as.vector(replicate(5,c("BBY","WGN","WMT")))), 
                quantity=c(df$Best,df$Walgreen,df$Walmart))
df2%>%head(n=10)

Generate Barplots for Data Visualization

df2%>%ggplot(aes(digit,quantity,fill=type))+geom_col(position = "dodge", width = 0.75)+
  xlab("month")+ ylab("quantity")+
  scale_x_continuous(breaks = as.integer(seq(1,5)), labels = c('Jul','Aug','Sep','Oct','Nov'))+
  ggtitle("quantity of marketing emails from BBY, WGN, & WMT in 2018")+theme_classic()+
  theme(
    plot.title = element_text(color="black", size=11, hjust=0.5, face="bold"),
    axis.title.x = element_text(color="black", size=11, face="bold"),
    axis.text.x = element_text(color="black", size=11, face="bold"),
    axis.text.y = element_text(color="black", size=11, face="bold"),
    axis.title.y = element_text(color="black", size=11, face="bold"),
    legend.title = element_blank()
  )

3. 2016 GDP Barplot for Selected Countries

This code generates a barplot for 2016 GDP for selected countries using the gdp.csv dataset which can be obtained from this website: https://data.oecd.org/gdp/gross-domestic-product-gdp.htm.

The code for this example can be downloaded from this repository: https://github.com/bot13956/GDP_barplot_using_R.

Import Necessary Libraries

library(tidyverse)
library(scales)

Data Importation and Preparation

data<-read.csv("gdp.csv")
head(data)
names(data)[1]="location"
ind<-order(data$Value)
data<-data%>%mutate(location=location[ind],Value=Value[ind])

Generate Barplot for Data Visualization

data%>%ggplot(aes(reorder(location,Value),Value))+geom_col(fill="blue")+
scale_y_continuous(breaks=seq(0,100000,10000),               labels=c("0","10k","20k","30k","40k","50k","60k","70k","80k","90k","100k"))+
theme(axis.text.x = element_text(angle = 45, hjust = 1,size=6))+
theme(axis.title.x=element_blank())+
theme(axis.title.y=element_blank())+
ggtitle("Gross Domestic Product (GDP), Total, US dollars/Capita, 2016 ")

In summary, we’ve shown how barplots can be generated using R’s ggplot package.

Data Science
Data Visualization
Programming
R
Barplot
Recommended from ReadMedium