ggplot in action!
Published in
3 min readJan 29, 2022
Your cheatsheat to ggplot!
First, install the pandas
and plotnine
packages to ensure they are available.
!pip install pandas plotnine
To produce a plot with the ggplot
class from plotnine
, we must provide three things:
- A data frame containing our data.
- How the columns of the data frame can be translated into positions, colors, sizes, and shapes of graphical elements (“aesthetics”).
- The actual graphical elements to display (“geometric objects”).
Scatter Plot
fig(12,8)
ggplot(titanic, aes(x=Age, y=Fare)) +
geom_point() +
labs(y="Fare",
x="Age",
title="Titanic - Age vs Fare")+
theme_gray()+
theme(plot.title = element_text(size=22),axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15), axis.title=element_text(size=18))
Bar Plot
fig(12,8)
ggplot(university[university$score>60,], aes(country))+
geom_bar(stat="count", width = 0.5, fill="darkblue")+
labs(x="Country",
y="Score",
title="Country vs Score above 60 ")+
theme_bw()+
theme(plot.title = element_text(size=22),axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15), axis.title=element_text(size=18))
Horizontal Bar Plot
fig(12,8)
ggplot(hosp_code, aes(x=hospital_code,y=count))+
geom_bar(stat="identity",width = 0.5,aes(fill=count))+
scale_fill_gradient(low = "red", high = "blue")+
coord_flip()+
labs(x="Hospital Code",
y="Count",
title="Distribution of Hospital Type Code")+
theme_bw()+
theme(plot.title = element_text(size=22),axis.text.x= element_text(size=15,angle=90),
axis.text.y= element_text(size=15), axis.title=element_text(size=18))
Pie Plot
fig(12,8)
pie <- ggplot(netflix, aes(x = "", fill = factor(type))) +
geom_bar(width = 1) +
theme(axis.line = element_blank(),
plot.title = element_text(hjust=0.5,size=22)) +
labs(fill="class",
x=NULL,
y=NULL,
title="Pie Chart of Netflix shows")
pie + coord_polar(theta = "y", start=0)
Time Series Chart
fig(12,8)
ggplot(aus_deaths, aes(x=ObservationDate)) +
geom_line(aes(y=total)) +
labs(x="Date",
y="Number of Deaths",
title="Distribution of COVID Deaths in Australia")+
theme_bw()+
theme(plot.title = element_text(size=22)
,axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15),
axis.title=element_text(size=18))
Box Plot
fig(12,8)
ggplot(german_university, aes(y=score))+
geom_boxplot(varwidth=T, fill="lightblue") +
labs(x="",
y="Score",
title="Distribution of German Universities Score")+
theme_bw()+
theme(plot.title = element_text(size=22)
,axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15),
axis.title=element_text(size=18))
Histogram Plot
fig(12,8)
ggplot(com_df, aes(x=salary)) +
geom_histogram(binwidth=10000,fill="magenta")+
labs(x="Salary",
y="Count",
title="Distribution of Salaries for Comm&Mgmt")+
theme_bw()+
theme(plot.title = element_text(size=22)
,axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15),
axis.title=element_text(size=18))
Density Plot
fig(12,8)
ggplot(death_by_state, aes(x=freq))+
geom_density(color="darkblue", fill="lightblue",alpha=0.7)+
labs(x="Deaths Per State",
y="Count",
title="Distribution of Victims in United States")+
theme_bw()+
theme(plot.title = element_text(size=22)
,axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15),
axis.title=element_text(size=18))
Heatmap Plot
fig(12,8)
ggplot(sample_n(health,30000), aes(Age,Stay , fill=Admission_Deposit)) +
geom_tile()+
labs(y="Stay",
x="Age",
title="Distribution of Age & Stay with Admission Deposit)")+
theme_bw()+
theme(plot.title = element_text(size=22)
,axis.text.x= element_text(size=15),
axis.text.y= element_text(size=15),
axis.title=element_text(size=18))
Do reach out and comment if you get stuck!
Other articles that might be interested in:
- Pandas 10 minute guide. This will serve as a basic guide to get… | by Sam | Geek Culture | Jan, 2022 | Medium
- Your go to Numpy checklist. A quick glance at all the important… | by Sam | Geek Culture | Jan, 2022 | Medium
- Getting started with Apache Spark — I | by Sam | Geek Culture | Jan, 2022 | Medium
- Streamlit and Palmer Penguins. Binged Atypical last week on Netflix… | by Sam | Geek Culture | Medium
Cheers and do follow for more such content! :)
You can now buy me a coffee too if you liked the content!
samunderscore12 is creating data science content! (buymeacoffee.com)