Quiz1

library (ggplot2)

head(iris)

ggplot(data = iris, mapping = aes(x=Species, y = Sepal.Length)) + geom_boxplot(aes(fill = Species), show.legend = T) + ylab(“Sepal.Length (cm)”)+ xlab(“Species”)

head(iris)

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species, show.legend = T)) + geom_boxplot()+ scale_fill_manual (values = cm.colors(3))+ ylab(“Sepal.Length (cm)”)+ xlab("")

Quiz2

head(diamonds)

names(diamonds)

Subset the first 50 rows of the dataset and save it in the objective named - mydata

diamonds [1:50,]

mydata = diamonds [1:50,]

mydata

Draw a plot to show the relationship between carat and price

dim(mydata)

class(mydata)

mydata[,c(“carat”,“price”)]

str(mydata$cut)

Use different color to show the classification of cut

ggplot(data = mydata, mapping = aes(x=carat, y = price, show.legend=T, color = cut))+ geom_point()+ labs (title = “Relationships of Carat and Price”) + theme(plot.title = element_text(family = “TNR”, face = “italic”, color = “red”))+ theme_classic() + xlab (“Carat (cm)”) + ylab (“Price ($)”)

ggplot(mydata, aes(x = carat, y = price, fill = cut)) + geom_boxplot(show.legend = T)+ scale_fill_manual (values = c(“red”, “yellow”, “blue”, “pink”, “green”))+ theme_classic() + xlab (“Carat (cm)”) + ylab (“Price ($)”)

ggplot(mydata, aes(x = carat, y = price, fill = cut)) + geom_boxplot(show.legend = T)+ scale_fill_manual (values = cm.colors(5))+ theme_classic() + xlab (“Carat (cm)”) + ylab (“Price ($)”)

install.packages(“RColorBrewer”) library(RColorBrewer) display.brewer.all()

ggplot(mydata, aes(x = carat, y = price, fill = cut)) + geom_boxplot(show.legend = T)+ scale_fill_brewer (palette = “Set1”)+ theme_classic() + xlab (“Carat (cm)”) + ylab (“Price ($)”)

p + labs (y=“Price($)”, x = “Carat(cm)”) + theme(axis.title.y = element_text(family = “TNR”, face = “bold”, color = “blue”), axis.title.x = element_text(family = “TNR”, face = “bold”, color = “blue”))

ggplot(data = mydata, mapping = aes(x = carat, y = price, col = cut)) + geom_point() + geom_smooth(method =“lm”)

Use different colors and shapes to show the classification of cut

ggplot(data = mydata, mapping = aes(x=carat, y = price, show.legend=T, group = cut))+ geom_point(aes(shape = cut, color = cut, size = cut))+ labs (title = “Relationships of Carat and Price”) + theme(plot.title = element_text(family = “TNR”, face = “italic”, color = “red”))+ theme_classic() + xlab (“Carat (cm)”) + ylab (“Price ($)”)+ theme(plot.title = element_text(family = “TNR”, face = “italic”, color = “red”))

ggplot(mydata,aes(x=carat,y=price, show.legend = T, group=cut))+ geom_point(aes(shape=cut,color=cut,size=cut))+ scale_shape_manual(values = c(3,16,17,18,19))+ scale_color_manual(values =c(“red”,“green”,“blue”, “pink”, “black”))+ scale_size_manual(values = c(2,3,4,5,6))+ theme(legend.position = “top”)

Use a gradient color to show the changes of price

ggplot(data = mydata, mapping = aes(x = carat, y = price, show.legend=T, color = price))+ geom_point(shape = 2)+ scale_color_gradient(low = “blue”,high = “red”)+ geom_smooth()+ labs (title = “Changes of Price”) + theme(plot.title = element_text(family = “TNR”, face = “italic”, color = “red”))+ xlab (“Carat (cm)”) + ylab (“Price ($)”)

Quiz 3

head(mtcars)

library(ggrepel)

Draw a plot to show the changes of qsec along with mpg

ggplot(mtcars, aes(qsec, mpg, show.legend=T, color = qsec, label = rownames(mtcars)))+ geom_point()+ scale_color_gradient(low = “blue”,high = “red”)+ geom_label_repel(aes(label=row.names(mtcars)), size=2)+ labs(title = “The changes of qsec”)+ theme(plot.title = element_text(family = “TNR”, face = “italic”, color = “red”))+ xlab (“QSEC”)+ ylab (“MPG”)

ggplot(data = mtcars, mapping = aes(gear, mpg, show.legend=T, color = gear))+ geom_point()+ geom_smooth(method = “lm”)+ scale_color_gradient(low = “blue”,high = “red”)+ labs (title = “Changes of gear”) + theme(plot.title = element_text(family = “TNR”, face = “italic”, color = “red”))+ xlab (“GEAR”) + ylab (“MPG”) ```