get working directory for current project
getwd()
set working directory for current project
(put your cursor between the two quotes below and press the "tab" key)
setwd(dir = "")
list the files in your working directory
list.files()
install ggplot2
install.packages("ggplot2")
load ggplot2 into the current workspace
library(ggplot2)
load the diamonds dataset into the current workspace
data(diamonds)
show the first few lines of diamonds data
head(diamonds)
view the diamonds data
View(diamonds)
show the documentation for the diamonds data
help(diamonds)
summarize the diamonds data
summary(diamonds)
use ggplot2 to show the distribution of diamond clarity types
bar1 <- ggplot(diamonds, aes(x=clarity)) + geom_bar() + labs(title="Counts of diamond clarities")
show plot
bar1
plot the amount of each type of diamond cut within each type of diamond clarity
bar2 <- ggplot(diamonds, aes(x=clarity, fill=cut)) + geom_bar() + labs(title="Counts of diamond clarities filled by cut count")
show plot
bar2
create a scatterplot showing the relationship between a diamonds weight and price
plot1 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point()
show plot
plot1
use the color of each dot to show a third variable, diamond clarity
plot2 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point()
show plot
plot2
replace the third variable with color
plot3 <- ggplot(diamonds, aes(x=carat, y=price, color=color)) + geom_point()
show plot
plot3
use the size of each dot to plot a fourth variable, diamond cut
plot4 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity, size=cut)) + geom_point()
show plot
plot4
or use the shape of each dot to plot a fourth variable
plot5 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity, shape=cut)) + geom_point()
show plot
plot5
show a smoothing trend of the relationship between two variables
plot6 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + geom_smooth()
show plot
plot6
remove the confidence interval
plot7 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + geom_smooth(se=FALSE)
show plot
plot7
show the linear model instead of the curve
plot8 <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point() + geom_smooth(se=FALSE, method="lm")
show plot
plot8
add a third variable to create smoothing curves for each category of the new variable
plot9 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_point() + geom_smooth(se=FALSE)
show plot
plot9
show only the layer of the different smoothing curves
plot10 <- ggplot(diamonds, aes(x=carat, y=price, color=clarity)) + geom_smooth(se=FALSE)
show plot
plot10
facet three variables over the different categories of a fourth variable, start with three variables
plot11 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()
show plot
plot11
add the facet function for the fourth variable, clarity
plot11 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() + facet_wrap(~ clarity)
show plot
plot11
give the plot a title
plot11 <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point() + facet_wrap(~ clarity) + ggtitle("Diamond carat, price, and cut by level of diamond clarity")
show plot
plot11
save the plot as a .png in your current working directory
ggsave(filename="diamonds.png", plot11)
save the plot as a .jpg in your current working directory
ggsave(filename="diamonds.jpg", plot11)
save the plot as a .pdf in your current working directory
ggsave(filename="diamonds.pdf", plot11)
save the plot as a .png in your current working directory
ggsave(filename="diamonds.png", plot11)
save the plot as a .jpg in your current working directory
ggsave(filename="diamonds.jpg", plot11)
save the plot as a .pdf in your current working directory
ggsave(filename="diamonds.pdf", plot11)