Vizualizace komplexních dat
Týden 3/4 2. - 9.10.2017
Minimalni reseni UKOL1:
library(ggplot2)
library(readr)
cardata = read.table("cars.data", col.names = c("mpg","cylinders","displacement","horsepower","weight","acceleration","model.year","origin"))
carnames = read.table("cars.names", col.names = c("name"))
cars = data.frame(c(carnames,cardata))
View(cars)
ggplot(cars) +
stat_summary(aes(x = model.year, y = mpg), fun.y = mean, geom = "area", fill = "gray", color = "black") +
stat_summary(aes(x = model.year, y = cylinders), fun.y = mean, geom = "line") +
stat_summary(aes(x = model.year, y = cylinders), fun.y = mean, geom = "point")
ggplot(cars) +
aes(x = mpg, y = displacement) +
geom_point() +
facet_wrap(~cylinders)
Bonusove reseni (autor Velich):
library(ggplot2)
library(png)
cardata <- read.table("data/cars.data")
names(cardata) <- c("mpg","cylinders","displacement","horsepower","weight","acceleration","model.year","origin")
carnames <- read.table("data/cars.names")
names(carnames) <- c("name")
cars <- data.frame(c(carnames,cardata))
remove(cardata,carnames)
piston <- readPNG("piston.png")
y <- sort(unique(cars$model.year))
len <- length(y)
years <- c(y[1]-1,y,y[len]+1)
mpgs <- rep(0,len+2)
cyls <- rep(0,len+2)
clrs <- rep(0,len+2)
clrs[years %% 10 == 0] <- "blue"
szs <- rep(0,len+2)
szs[years %% 10 == 0] <- 1
brks <- years[years %% 10 == 0] - 0.05
lbls <- years[years %% 10 == 0] + 1900
for(i in 2:(len+1)){
mpgs[i] <- mean(cars[cars$model.year == years[i],'mpg'], na.rm = T)
cyls[i] <- round(mean(cars[cars$model.year == years[i],'cylinders'], na.rm = T))
}
year_data <- data.frame(years,mpgs,cyls)
names(year_data) <- c("year","mpg","cylinders")
cyl_years <- c()
cyl_data <- c()
for(i in 2:(len+1)){
cyl_years <- append(cyl_years, rep(year_data[i,'year'],year_data[i,'cylinders']))
cyl_data <- append(cyl_data, 1:year_data[i,'cylinders'])
}
mpg_plot <- ggplot(data = year_data, aes(x = year, y = mpg))
mpg_plot +
geom_area(fill = 'grey', color = '#71767c') +
coord_cartesian(ylim = c(-5, 110)) +
theme_classic() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
#axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 90, face = "bold"),
legend.position = "none",
panel.grid.major.y = element_line(colour = "grey50", linetype = 3)) +
geom_point(aes(color=szs,x=year, y=rep(-5,len+2), size=szs)) +
scale_x_continuous(breaks = brks, labels = lbls) +
scale_y_continuous(breaks = seq(0,30,10)) +
scale_color_gradient(low = "red", high = "blue") +
scale_size(range=c(2,4)) +
mapply(
function(i,j){
annotation_raster(piston,
xmin = i-0.5, xmax = i+0.5,
ymin = 110-(10*j), ymax = 120-(10*j))
},
cyl_years,cyl_data
)
ggsave("novy_plot1.png")
library(ggplot2)
library(readr)
cardata = read.table("cars.data", col.names = c("mpg","cylinders","displacement","horsepower","weight","acceleration","model.year","origin"))
carnames = read.table("cars.names", col.names = c("name"))
cars = data.frame(c(carnames,cardata))
View(cars)
ggplot(cars) +
stat_summary(aes(x = model.year, y = mpg), fun.y = mean, geom = "area", fill = "gray", color = "black") +
stat_summary(aes(x = model.year, y = cylinders), fun.y = mean, geom = "line") +
stat_summary(aes(x = model.year, y = cylinders), fun.y = mean, geom = "point")
ggplot(cars) +
aes(x = mpg, y = displacement) +
geom_point() +
facet_wrap(~cylinders)
Bonusove reseni (autor Velich):
library(ggplot2)
library(png)
cardata <- read.table("data/cars.data")
names(cardata) <- c("mpg","cylinders","displacement","horsepower","weight","acceleration","model.year","origin")
carnames <- read.table("data/cars.names")
names(carnames) <- c("name")
cars <- data.frame(c(carnames,cardata))
remove(cardata,carnames)
piston <- readPNG("piston.png")
y <- sort(unique(cars$model.year))
len <- length(y)
years <- c(y[1]-1,y,y[len]+1)
mpgs <- rep(0,len+2)
cyls <- rep(0,len+2)
clrs <- rep(0,len+2)
clrs[years %% 10 == 0] <- "blue"
szs <- rep(0,len+2)
szs[years %% 10 == 0] <- 1
brks <- years[years %% 10 == 0] - 0.05
lbls <- years[years %% 10 == 0] + 1900
for(i in 2:(len+1)){
mpgs[i] <- mean(cars[cars$model.year == years[i],'mpg'], na.rm = T)
cyls[i] <- round(mean(cars[cars$model.year == years[i],'cylinders'], na.rm = T))
}
year_data <- data.frame(years,mpgs,cyls)
names(year_data) <- c("year","mpg","cylinders")
cyl_years <- c()
cyl_data <- c()
for(i in 2:(len+1)){
cyl_years <- append(cyl_years, rep(year_data[i,'year'],year_data[i,'cylinders']))
cyl_data <- append(cyl_data, 1:year_data[i,'cylinders'])
}
mpg_plot <- ggplot(data = year_data, aes(x = year, y = mpg))
mpg_plot +
geom_area(fill = 'grey', color = '#71767c') +
coord_cartesian(ylim = c(-5, 110)) +
theme_classic() +
theme(axis.line = element_blank(),
axis.title = element_blank(),
#axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(angle = 90, face = "bold"),
legend.position = "none",
panel.grid.major.y = element_line(colour = "grey50", linetype = 3)) +
geom_point(aes(color=szs,x=year, y=rep(-5,len+2), size=szs)) +
scale_x_continuous(breaks = brks, labels = lbls) +
scale_y_continuous(breaks = seq(0,30,10)) +
scale_color_gradient(low = "red", high = "blue") +
scale_size(range=c(2,4)) +
mapply(
function(i,j){
annotation_raster(piston,
xmin = i-0.5, xmax = i+0.5,
ymin = 110-(10*j), ymax = 120-(10*j))
},
cyl_years,cyl_data
)
ggsave("novy_plot1.png")