查看原文
其他

初学ggplot2(三)

2015-10-28 刘顺祥 每天进步一点点2015

学习ggplot2近一周了,本次画几张有趣的图给大家观望一下,有误的地方还望大家纠正。


一、浏览网页中无意发现一张心形图,于是动起了画心形图的冲动:

#模拟数据

x <- seq(from = -2, to = 2, by = 0.01)

s <- sqrt(1-(abs(x)-1)^2)

u <- -3*sqrt(1-sqrt(abs(x)/2))


用散点图绘制心形图

#加载数据和添加图形属性

p1 <- ggplot(data = NULL) + xlab(NULL) + ylab(NULL)

#绘制散点图

p1 <- p1 + geom_point(mapping = aes(x = x, y = s),

colour = 'red', size = 2)

#累加图层,再次绘制散点图

p1 <- p1 + geom_point(mapping = aes(x = x, y = u),

colour = 'blue', size = 2)

#添加注释

p1 <- p1 + annotate('text', x = -1.5, y = -2.5,

label = 'RedDot:S(x) == sqrt(1-(abs(x)-1)^2)',

parse = TRUE)

p1 <- p1 + annotate('text', x = 1, y = -2.5,

label = 'BlueDot:U(x) == -3*sqrt(1-sqrt(abs(x)/2))',

parse = TRUE)

p1



用线条图绘制心形图

p2 <- ggplot(data = NULL) + xlab(NULL) + ylab(NULL)

#绘制线条

p2 <- p2 + geom_line(mapping = aes(x = x, y = s),

colour = 'red', size = 2)

p2 <- p2 + geom_line(mapping = aes(x = x, y = u),

colour = 'blue', size = 2)

#添加注释

p2 <- p2 + annotate('text', x = -1.5, y = -2.5,

label = 'RedCurve:S(x) == sqrt(1-(abs(x)-1)^2)',

parse = TRUE)

p2 <- p2 + annotate('text', x = 1, y = -2.5,

label = 'BlueCurve:U(x) == -3*sqrt(1-sqrt(abs(x)/2))',

parse = TRUE)

p2


当然也可以画一个实实在在的红心图


二、面积图

#模拟数据

date <- seq(from = as.Date('2015-01-01'),

to = as.Date('2015-10-28'), by = 'day')

industry <- sample(x = c('A','B','C'),

size = length(date),

replace = TRUE)

my.df <- data.frame(date = date, industry = industry)

set.seed(1234)

my.df <- within(data = my.df, {

unemploy.rate = numeric(0)

unemploy.rate[industry == 'A'] = runif(sum(industry == 'A'))

unemploy.rate[industry == 'B'] = runif(sum(industry == 'B'))

unemploy.rate[industry == 'C'] = runif(sum(industry == 'C'))

})

#绘制面积图(在y轴方向填充了下方面积的图形)

p3 <- ggplot(data = my.df,

mapping = aes(x = date, y = unemploy.rate))

p3 <- p3 + geom_area(mapping = aes(colour = industry,

fill = industry))

p3


三、绘制直方图、核密度曲线和正态分布曲线

#模拟数据

set.seed(1234)

x <- rt(1000,5)

#生成正态分布密度值

x.norm <- dnorm(seq(from = min(x), to = max(x),

by = 0.001), mean = mean(x),

sd = sd(x))

#绘制直方图

p4 <- ggplot(data = NULL, aes(x = x))

p4 <- p4 + geom_histogram(aes(y = ..density..),

fill = 'steelblue', binwidth = 0.5)

#添加核密度曲线和正态分布曲线

p4 <- p4 + geom_density(colour = 'black')

+ geom_line(mapping = aes(x = seq(from = min(x),

to = max(x), by = 0.001),

y = x.norm), col = 'red')

p4


四、绘制箱线图

p5 <- ggplot(data = economics,

mapping = aes(x = 1, y = unemploy/pop)) + xlab(NULL)

p5 <- p5 + geom_boxplot(fill = 'blue')

#定制标签

label <- round(quantile(economics$unemploy/economics$pop,

probs = c(0.25, 0.5, 0.75)),3)

#添加注释

p5 <- p5 + annotate('text', x = 1,

y = quantile(economics$unemploy/economics$pop,

probs = c(0.25, 0.5, 0.75)),

label = label)

p5


五、绘制地图

#加载所需的R包

library(ggplot2)

library(maptools)

#加载GIS地理数据

china_map<-readShapePoly("C:\\Users\\admin\\Desktop\\ArcGIS矢量数据\\Lambert\\省级行政区.shp")

#挑选数据地理信息数据中的某几列

x <- china_map@data[,c('AREA', 'POPU', 'NAME')]

#将地理信息数据集转化为数据框格式数据

df.map <- fortify(china_map)

#设置各省份编码

xs<-data.frame(x,id=seq(0:33)-1)

#合并数据集

library(plyr)

df.map <- join(df.map, xs, type = 'left')

#计算各个省份的中心经纬度(自定义函数)

midpos <- function(x) mean(range(x,na.rm=TRUE))

centres <- ddply(.data = df.map , .variables = .(NAME), .fun = colwise(.fun = midpos, .cols = .(long,lat)))

#加载已经定制好的地理数据,并设置好图形属性

p6 <- ggplot(data = df.map, mapping = aes(x = long, y = lat, group = group, fill = NAME))

#绘制地图

p6 <- p6 + geom_polygon() + geom_path(colour = "blue")

#为不同的省份设置不同的颜色

p6 <- p6 + scale_fill_manual(values=colours(),guide=FALSE)

#为各个省添加标签

long <- centres$long

lat <- centres$lat

label <- centres$NAME

p6 <- p6 + annotate('text', x = long, y = lat, label = label)

p6



在地图中绘制热图

p7 <- ggplot(data = df.map, mapping = aes(x = long, y = lat, group = group, fill = POPU))

p7 <- p7 + geom_polygon() + geom_path(colour = "blue") + scale_fill_gradient(low="lightskyblue",high="steelblue") +

theme(

panel.grid = element_blank(),

panel.background = element_blank(),

axis.text = element_blank(),

axis.ticks = element_blank(),

axis.title = element_blank()

)

p7


本文所涉及到的数据集和脚本可以到如下链接中下载:

http://yunpan.cn/cFbmNEUV8bLyX 访问密码 bb13


参考资料:

http://research.stowers-institute.org/efg/R/Color/Chart/

http://chuansong.me/n/1448600

ggplot2:数据分析与图形艺术



您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存