library(dplyr)
library(readr)
library(ggplot2)
?mtcars
head(mtcars)
tail(mtcars)
summary(mtcars)
mpg cyl disp hp drat
Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0 Min. :2.760
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5 1st Qu.:3.080
Median :19.20 Median :6.000 Median :196.3 Median :123.0 Median :3.695
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7 Mean :3.597
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0 3rd Qu.:3.920
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0 Max. :4.930
wt qsec vs am gear
Min. :1.513 Min. :14.50 Min. :0.0000 Min. :0.0000 Min. :3.000
1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:3.000
Median :3.325 Median :17.71 Median :0.0000 Median :0.0000 Median :4.000
Mean :3.217 Mean :17.85 Mean :0.4375 Mean :0.4062 Mean :3.688
3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:4.000
Max. :5.424 Max. :22.90 Max. :1.0000 Max. :1.0000 Max. :5.000
carb cylFactor
Min. :1.000 4:11
1st Qu.:2.000 6: 7
Median :2.000 8:14
Mean :2.812
3rd Qu.:4.000
Max. :8.000
mean(mtcars$cyl)
[1] 6.1875
# scatter plot shape 1
ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_point(shape=1)
# scatter plot shape 19
ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_point(shape=19)
# shape identity
ggplot(mtcars,aes(x=mpg, y=wt, shape=cyl)) +
geom_point() +
scale_shape_identity()
mtcars$cylFactor <- factor(mtcars$cyl)
ggplot(mtcars,aes(x=mpg, y=wt, shape=cylFactor)) +
geom_point()
# color parameter
ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_point(shape=19, color="blue")
# shape 19
ggplot(mtcars, aes(x=mpg, y=wt, color=cyl)) +
geom_point(shape=19)
ggplot(mtcars,aes(x=mpg, y=wt, color=cylFactor)) +
geom_point(shape=19)
# rename the legend
ggplot(mtcars, aes(x=mpg, y=wt, color=cylFactor)) +
geom_point(shape=19) +
labs(color="Cylinders")
# add new labels and title
ggplot(mtcars, aes(x=mpg, y=wt, color=cylFactor)) +
geom_point(shape=19) +
xlab("Miles per Gallon") +
ylab("Weight") +
labs(color="Cylinders") +
ggtitle("Scatterplot")
?EuStockMarkets
EuStockDF <- as.data.frame(EuStockMarkets)
head(EuStockDF)
# DAX stock price
ggplot(EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=DAX)) +
geom_line()
# line width
ggplot(EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=DAX)) +
geom_line(size=1.5) +
labs(x="Stocks")
# color
ggplot(EuStockDF,aes(x=c(1:nrow(EuStockDF)), y=DAX)) +
geom_line(size=1.5, colour="light blue") +
labs(x="Time", y="Stocks")
# plot the 2 stocks
dax_smi_plot <- ggplot() +
geom_line(data=EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=DAX), size=1.5, colour="light blue") +
geom_line(data=EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=SMI), size=1.5, colour="red") +
labs(x="Time", y="Stocks")
print(dax_smi_plot)
# all stocks
all_stocks <- ggplot() +
geom_line(data = EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=DAX), size=1, colour="light blue") +
geom_line(data = EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=SMI), size=1, colour="red") +
geom_line(data = EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=CAC), size=1, colour="purple") +
geom_line(data = EuStockDF, aes(x=c(1:nrow(EuStockDF)), y=FTSE), size=1, colour="green") +
labs(x="Time", y="Stocks")
print(all_stocks)
# rename the labels
legend_stocks <- all_stocks +
xlab("Days") +
ylab("Price") +
ggtitle("Eu Stocks")
print(legend_stocks)
ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_point(shape=19) +
geom_smooth(method="lm", se=FALSE, color="red")
#se = TRUE -> confidence interval appear (default = true)
ggplot(mtcars, aes(x=mpg, y=wt, color=cylFactor)) +
geom_point(shape=19) +
geom_smooth(method="lm", se=TRUE, color="red")
# add the labels
ggplot(mtcars, aes(x=mpg, y=wt, color=cylFactor)) +
geom_point(shape=19) +
geom_smooth(method="lm", se=TRUE, color="red") +
xlab("Miles per Gallon") +
ylab("Weight") +
labs(color="Cylinders") +
ggtitle("Linear Regression")
ggplot(mtcars, aes(x=mpg, y=wt, color=cylFactor)) +
geom_point(shape=19) +
geom_smooth(method="auto", se=TRUE, color="red") +
xlab("Miles per Gallon") +
ylab("Weight") +
labs(colour="Cylinders") +
ggtitle("Gaussian Regression")