Skip to content

Instantly share code, notes, and snippets.

@zoltanctoth
Created May 9, 2017 04:43
Show Gist options
  • Save zoltanctoth/e8d2aa028a288bd8070117e808c2d720 to your computer and use it in GitHub Desktop.
Save zoltanctoth/e8d2aa028a288bd8070117e808c2d720 to your computer and use it in GitHub Desktop.
Here you can find the solutions for the exercises we used at the class.
library(ggplot2)
# Take a look at our example dataset
View(diamonds)
# Make a chart from scratch
x = ggplot() +
layer(
data = diamonds, mapping = aes(x=carat,y=price),
stat='identity', position="identity", geom="point"
) +
scale_x_continuous() +
scale_y_continuous() +
coord_cartesian()
x
# Show only diamonds with at least 4 carat
min4carat = diamonds[diamonds$carat >= 4,]
ggplot() +
layer(
data = min4carat, mapping = aes(x=carat,y=price),
stat='identity', position="identity", geom="point"
) +
scale_x_continuous() +
scale_y_continuous() +
coord_cartesian()
# TODO: Convert this into a jitter plot
# TODO: Is this a power curve?
# add 1. y <- ..., and x and y log scale
x + scale_x_log10() + scale_y_log10()
# Get rid of layer definitintions
ggplot(data = diamonds, mapping = aes(x=carat,y=price)) +
geom_point()
ggplot(data = diamonds, mapping = aes(x=carat,y=price)) +
geom_smooth()
# Add up 2 layers
ggplot(data = diamonds, mapping = aes(x=carat,y=price)) +
geom_point() + geom_smooth()
####
# Switch to qplot
qplot(carat,price,data=diamonds,geom=c('point','smooth'))
# set axis log='xy'
qplot(carat,price,data=diamonds,geom=c('point','smooth'),log='xy')
# Let's change the theme to bw
qplot(carat,price,data=diamonds,geom=c('point','smooth'),log='xy') +
theme_bw()
# A FEW EXTRA FEATURES
# Strong defaults
qplot(price, data=diamonds)
qplot(price, data=diamonds, binwidth=100)
# Histogram by cut
qplot(cut,data=diamonds)
# Histogram as barchart
ggplot(diamonds,aes(x="",fill=cut)) + geom_bar(width=1)
# Pie Chart
ggplot(diamonds,aes(x="",fill=cut)) + geom_bar(width=1) +
coord_polar(theta='y')
# Faceting
qplot(carat,price,data=diamonds,geom=c('point','smooth'),
log='xy') + facet_grid(. ~ cut)
# Colors
qplot(carat,price,data=diamonds,geom='point',
color=cut,
size=clarity)
# Sizes
qplot(carat,price,data=diamonds[diamonds$carat >= 3,],geom='point',
color=clarity,
size=cut)
# Show point, jitter, boxplot, violin plot
qplot(x=cut, y=price, data=diamonds, geom='point')
# Exercises
# Visualize these and write a sentence
# about what the chart tells yo about the data
###########
# - Create a histogram of "carat"
qplot(carat, data=diamonds)
# - Set the bin width of the histogram to 0.01
qplot(carat, data=diamonds, binwidth=0.01)
# Make a scatterplot: carat vs price, set the color to clarity
qplot(carat, price, data=diamonds, color=clarity)
# Make a scatterplot: carat vs price, set the color to clarity. Also add trendline to the plot
qplot(carat, price, data=diamonds, color=clarity) + geom_smooth()
# Make a scatterplot: carat vs price,
# Facet it by clarity.
qplot(carat, price, data=diamonds, color=clarity) + facet_grid(clarity ~ .)
# - Show carat vs cut, make a jitter, a violin and a boxplot.
# What can you see?
qplot(clarity, price, data=diamonds, geom="boxplot")
@cagdasyetkin
Copy link

qplot(x=cut, y=carat, data=diamonds, geom='jitter')
qplot(x=cut, y=carat, data=diamonds, geom='violin')
qplot(x=cut, y=carat, data=diamonds, geom='boxplot')

We can see that highest 'carat' variation is observed in 'fair cut' although it has the smallest sample size.

Boxplot:

boxplot_sample

Violin:

violin_sample

Jitter:

jitter_sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment