24. Data Visualization
24 of 98
http://nycdatascience.com/part4_en/
library(lattice)
wireframe(V1~x*y,data=result6,scales = list(arrows = FALSE),
drape = TRUE, colorkey = F)
25. Data Visualization
25 of 98
http://nycdatascience.com/part4_en/
library(ggplot2)
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy)) + geom_point()
print(p)
26. Data Visualization
26 of 98
http://nycdatascience.com/part4_en/
summary(p)
data: manufacturer, model, displ, year, cyl, trans, drv, cty, hwy, fl, class [234x11]
mapping: x = cty, y = hwy
faceting: facet_null()
----------------------------------geom_point: na.rm = FALSE
stat_identity:
position_identity: (width = NULL, height = NULL)
27. Data Visualization
27 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=factor(year)))
p <- p + geom_point()
print(p)
28. Data Visualization
28 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy,colour=factor(year)))
p <- p + geom_smooth()
print(p)
29. Data Visualization
29 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy)) +
geom_point(aes(colour=factor(year))) +
geom_smooth()
35. Data Visualization
35 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg, mapping=aes(x=cty,y=hwy)) +
geom_point(aes(colour=class,size=displ),
alpha=0.5,position = "jitter") +
geom_smooth() +
scale_size_continuous(range = c(4, 10)) +
facet_wrap(~ year,ncol=1) +
opts(title='Vehicle model and fuel consumption') +
labs(y='Highway miles per gallon',
x='Urban miles per gallon',
size='Displacement',
colour = 'Model')
39. Data Visualization
39 of 98
http://nycdatascience.com/part4_en/
library(ggplot2)
p <- ggplot(data=iris,aes(x=Sepal.Length))+
geom_histogram()
print(p)
40. Data Visualization
40 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(iris,aes(x=Sepal.Length))+
geom_histogram(binwidth=0.1,
# Set the group gap
fill='skyblue', # Set the fill color
colour='black') # Set the border color
44. Data Visualization
44 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(iris,aes(x=Sepal.Length)) +
geom_histogram(aes(y=..density..), # Note: set y to relative frequency
fill='gray60',
color='gray') +
geom_density(color='black',linetype=1,adjust=0.5) +
geom_density(color='black',linetype=2,adjust=1) +
geom_density(color='black',linetype=3,adjust=2)
46. Data Visualization
46 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(iris,aes(x=Sepal.Length,fill=Species)) + geom_density(alpha=0.5,color='gra
print(p)
47. Data Visualization
47 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species)) + geom_boxplot()
print(p)
48. Data Visualization
48 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species)) + geom_violin()
print(p)
49. Data Visualization
49 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(iris,aes(x=Species,y=Sepal.Length,
fill=Species)) +
geom_violin(fill='gray',alpha=0.5) +
geom_dotplot(binaxis = "y", stackdir = "center")
print(p)
51. Data Visualization
51 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(mpg,aes(x=class)) +
geom_bar()
print(p)
52. Data Visualization
52 of 98
http://nycdatascience.com/part4_en/
mpg$year <- factor(mpg$year)
p <- ggplot(mpg,aes(x=class,fill=year)) +
geom_bar(color='black')
54. Data Visualization
54 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(mpg,aes(x=class,fill=year)) +
geom_bar(color='black',
position=position_dodge())
58. Data Visualization
58 of 98
http://nycdatascience.com/part4_en/
set.seed(1)
# Randomly generate 100 wind directions, and divide them into 16 intervals.
dir <- cut_interval(runif(100,0,360),n=16)
# Randomly generate 100 wind speed, and divide them into 4 intensities.
mag <- cut_interval(rgamma(100,15),4)
sample <- data.frame(dir=dir,mag=mag)
# Map wind direction to X-axie, frequency to Y-axie and speed to fill colors. Transfor
p <- ggplot(sample,aes(x=dir,fill=mag)) +
geom_bar()+ coord_polar()
65. Data Visualization
65 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,aes(x=cty,y=hwy)) +
geom_point()
print(p)
66. Data Visualization
66 of 98
http://nycdatascience.com/part4_en/
mpg$year <- factor(mpg$year)
p <- ggplot(data=mpg,aes(x=cty,y=hwy)) + geom_point(aes(color=year))
print(p)
67. Data Visualization
67 of 98
http://nycdatascience.com/part4_en/
mpg$year <- factor(mpg$year)
p <- ggplot(data=mpg,aes(x=cty,y=hwy)) + geom_point(aes(color=year,shape=year))
print(p)
68. Data Visualization
68 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,aes(x=cty,y=hwy)) + geom_point(aes(color=year),alpha=0.5,position
print(p)
69. Data Visualization
69 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,aes(x=cty,y=hwy)) +
geom_point(aes(color=year),alpha=0.5,position = "jitter") +
geom_smooth(method='lm')
print(p)
70. Data Visualization
70 of 98
http://nycdatascience.com/part4_en/
p <- ggplot(data=mpg,aes(x=cty,y=hwy)) +
geom_point(aes(color=year,size=displ),alpha=0.5,position = "jitter") +
geom_smooth(method='lm') +
scale_size_continuous(range = c(4, 10))
94. Data Visualization
94 of 98
http://nycdatascience.com/part4_en/
library(ggmap)
library(XML)
webpage <-'http://data.earthquake.cn/datashare/globeEarthquake_csn.html'
tables <- readHTMLTable(webpage,stringsAsFactors = FALSE)
raw <- tables[[6]]
data <- raw[,c(1,3,4)]
names(data) <- c('date','lan','lon')
data$lan <- as.numeric(data$lan)
data$lon <- as.numeric(data$lon)
data$date <- as.Date(data$date, "%Y-%m-%d")
#Read the map data from Google by the ggmap package, and mark the previous data on the
earthquake <- ggmap(get_googlemap(center = 'china', zoom=4,maptype='terrain'),extent='
geom_point(data=data,aes(x=lon,y=lan),colour = 'red',alpha=0.7)+
theme(legend.position = "none")