###Quiz1
head(iris)
library (ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
ggplot(data = iris,
mapping = aes(x=Species,
y = Sepal.Length)) +
geom_boxplot(aes(fill = Species), show.legend = T) +
scale_fill_manual (values = cm.colors(3))+
theme_classic()+
ylab("Sepal.Length (cm)")+
xlab("Species")
### Quiz2
head(diamonds)
names(diamonds)
diamonds [1:50,]
## # A tibble: 50 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
## # ... with 40 more rows
mydata = diamonds [1:50,]
mydata
## # A tibble: 50 x 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
## # ... with 40 more rows
par(mfrow = c(1,2))
dim(mydata)
## [1] 50 10
class(mydata)
## [1] "tbl_df" "tbl" "data.frame"
mydata[,c("carat","price")]
## # A tibble: 50 x 2
## carat price
## <dbl> <int>
## 1 0.23 326
## 2 0.21 326
## 3 0.23 327
## 4 0.29 334
## 5 0.31 335
## 6 0.24 336
## 7 0.24 336
## 8 0.26 337
## 9 0.22 337
## 10 0.23 338
## # ... with 40 more rows
str(mydata$cut)
## Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
par(mfrow = c(1,1))
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 ($)")
###dd
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 ($)")
Price and Carat
install.packages(“RColorBrewer”) library(RColorBrewer) display.brewer.all()
p<-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
###Method 1
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"))
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
#### Method2
ggplot(data = mydata, mapping = aes(x = carat,
y = price, col = cut)) +
geom_point() +
geom_smooth(method ="lm")
## `geom_smooth()` using formula 'y ~ x'
Figure 5
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"))
## Warning: Using shapes for an ordinal variable is not advised
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
Figure 6
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 = "right")
Figure 7
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 ($)")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
The change of price
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.0.5
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")
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
the changes of qsec along with mpg, change of qsec
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")
## `geom_smooth()` using formula 'y ~ x'
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
The change of gear