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("")
head(diamonds)
names(diamonds)
diamonds [1:50,]
mydata = diamonds [1:50,]
mydata
dim(mydata)
class(mydata)
mydata[,c(“carat”,“price”)]
str(mydata$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”)
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”)
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 ($)”)
head(mtcars)
library(ggrepel)
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”) ```