ggplot2 作图
ggplot2 作图
ggplot2是著名的R语言作图工具包,gg为Grammar of Graphics的缩写,体现了结构化作图的思想。ggplot2根据图层来作图是非常优秀的思想,官方文档在这里
1、数据(data)
ggplot2要求数据集格式是data.frame,指定数据集之后就可以直接查找需要的列
例
library(ggplot2)
# 选择mtcars数据集
ggplot(data = mtcars, mapping = aes(mpg, wt, color = cyl)) +
geom_point()

2、映射(mapping)
映射使用aes()函数实现,是数据集中的数据关联到相应的图形属性过程中一种对应关系。
属性设置放在aes()里面,表示把数据根据某一属性分开处理;如果放在外面,则是统一处理
例
library(ggplot2)
# 离散分组着色数据类型要是factor
ggplot(data = mtcars, mapping = aes(mpg, wt, color = factor(am))) +
geom_point(color = 'blue') # 统一设置颜色

二级映射实现分组
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = wt, color = factor(gear))) +
geom_point(aes(shape = factor(carb)))

3、几何对象(geom) 与 统计变换(stats)
几何对象可以绘制不同类型的图,形式是gemo_xx
例
library(ggplot2)
ggplot(diamonds, aes(x = clarity, fill = cut)) +
geom_bar(position = "dodge") # 并排排列

4、统计变换(stats)
统计图片中的额外信息,进行一定的计算
例
library(ggplot2)
ggplot(mtcars, aes(x = drat, y = disp, color = factor(gear))) +
stat_sum() # 计数,stat_unique则可以去重

5、标度(scale) 与 坐标(coords)
可以修改坐标轴和图例的参数,作用:
- 修改图例
- 修改图形属性
- 修改坐标轴
例:
library(ggplot2)
ggplot(data = mtcars, aes(wt, mpg, color = qsec, shape = factor(vs))) +
geom_point(size = 3) +
annotate('text', x = 4, y = 25, label = 'text') + # 文本注释
labs(x = 'xx', y = 'yy') + # 添加标题、坐标名
ggtitle('Title') +
geom_vline(xintercept = 3) + # 添加竖线
geom_hline(yintercept = 20) + # 添加水平线
xlim(3, 4) + # 添加范围
scale_x_continuous(breaks = c(3, 3.25, 3.5, 3.75, 4),
labels = c('a', 'b', 'c', 'd', 'e')) + # 修改坐标轴
coord_flip() # 互换坐标轴

7、分面(facet)
即在一个页面上自动摆放多幅图形,是ggplot2非常方便的一个地方,先划分数据集再作图
例
library(ggplot2)
# 单变量分面
ggplot(data = mtcars, aes(wt, mpg)) +
geom_point() +
facet_grid(vs~.) # 根据vs分面
# 双变量分面
ggplot(data = mtcars, aes(wt, mpg)) +
geom_point() +
facet_grid(vs~am) # 根据vs和am分面
# 轴刻度不一致,不同纵(Y)坐标刻度
# scale = 'free'是同时释放x和y轴
ggplot(data = mtcars, aes(wt, mpg)) +
geom_point() +
facet_grid(vs ~., scales = "free_y") # 释放坐标轴

8、主题(theme)
主题不多论述,一班使用ggthemr包会比较省事
不使用ggthemr
library(ggplot2)
small = diamonds[sample(nrow(diamonds), 1000), ]
ggplot(small, aes(x = carat, y = price, shape = cut, colour = color)) +
geom_point() +
scale_y_log10() +
labs(title = 'price vs carat') +
theme(plot.title = element_text(hjust = 0.5)) # 标题居中

使用ggthemr
library(ggplot2)
library(ggthemr)
ggthemr('pale')
small = diamonds[sample(nrow(diamonds), 1000), ]
ggplot(small, aes(x = carat, y = price, shape = cut, colour = color)) +
geom_point() +
scale_y_log10() +
labs(title = 'price vs carat') +
theme(plot.title = element_text(hjust = 0.5)) # 标题居中
ggthemr_reset()

个人感觉配色更好看一些~
ggplot2 作图的更多相关文章
- ggplot2作图详解:入门函数qplot
ggplot2作图详解:入门函数qplot ggplot2的功能不用我们做广告,因为它的作者Hadley Wickham就说ggplot2是一个强大的作图工具,它可以让你不受现有图形类型的限制,创 ...
- ggplot2作图详解7(完):主题(theme)设置
凡是和数据无关的图形设置内容理论上都可以归为主题类,但考虑到一些内容(如坐标轴)的特殊性,可以允许例外的情况.主题的设置相当繁琐,很容易就占用了 大量的作图时间,应尽量把这些东西简化,把注意力主要放在 ...
- ggplot ggplot2 画图
折线图-ggplot2 http://blog.163.com/yugao1986@126/blog/static/6922850820131161531421/http://blog.sina.c ...
- [转]ggplot2用法简单介绍
简介 ggplot2包是基于Wilkinson在<Grammar of Graphics>一书中所提出的图形语法的具体实现, 这套图形语法把绘图过程归纳为data, transformat ...
- ggplot2入门与进阶(上)
出处:http://www.cellyse.com/how_to_use_gggplot2_part1/ ggplot2包是基于Wilkinson在<Grammar of Graphics> ...
- ggplot2绘制Excel所有图
出处:https://brucezhaor.github.io/blog/2016/06/13/excel2ggplot/#%E5%89%8D%E8%A8%80 目录 前言 1.用到的包 2.数据准备 ...
- R语言学习 - 热图绘制heatmap
生成测试数据 绘图首先需要数据.通过生成一堆的向量,转换为矩阵,得到想要的数据. data <- c(1:6, 6:1, 6:1, 1:6, (6:1)/10, (1:6)/10, (1:6)/ ...
- ggplot2 梯度作图
ggplot2是R语言的绘图包 library('ggplot2') df <- data.frame(var=c("a","b","c&quo ...
- 跟我一起ggplot2(1)
ggplot2 R的作图工具包,可以使用非常简单的语句实现非常复杂漂亮的效果. qplot 加载qplot library(ggplot2) # 测试数据集,ggplot2内置的钻石数据 qplot( ...
随机推荐
- cmake方式使用vlfeat
目录 environment statement compile vlfeat with cmake compile example project with cmake 1. make sure c ...
- ubuntu下使用matplotlib绘图无法显示中文label
原因是字体导致的.大家的做法基本都是搞一个windows上的字体文件(simhei.ttf, 点我fq下载)然后刷新一下缓存文件. 只不过百度搜到第一篇CSDN的博客,写的很不靠谱(不是所有的CSDN ...
- Nginx安装配置&反向代理
使用Nginx作为前端服务能够更快更及时的响应静态页面.js.图片等,当客户端请求访问动态页面时由Nginx的反向代理给Apache处理,Apache处理完再交予Nginx返回给客户端. Nginx更 ...
- 一脸懵逼学习基于CentOs的Hadoop集群安装与配置(三台机器跑集群)
1:Hadoop分布式计算平台是由Apache软件基金会开发的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS)和MapReduce(Google MapReduce的开源实现)为核心的 ...
- PHP微信模板消息发送
<?php class Wxtemplate extends Base { function __construct() { $this->appid = config('WXAPP_AP ...
- memcached 配置
Memcached是一款开源.高性能.分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序.它是一个基于内存的“键值对”存储,用于存储数据 ...
- ES6新特性:使用export和import实现模块化(转载)
在ES6前, 前端就使用RequireJS或者seaJS实现模块化, requireJS是基于AMD规范的模块化库, 而像seaJS是基于CMD规范的模块化库, 两者都是为了为了推广前端模块化的工 ...
- JavaMelody 项目性能监控和调优工具
转自 JavaMelody 可以实现对内存.CPU.线程.JDBC 连接数.HTTP 请求执行时间.SQL 执行时间(分析 Top SQL).方法执行时间(分析系统方法性能瓶颈)等等的监控. 配置方式 ...
- tp5数据库链接
1在config/database.php中配置 1.1直接 return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '12 ...
- 在mybatis中调用存储过程的时候,不能加工语句
select count(0) from ({call pkg_business.P_ZZS_LYFPHJSKJQK ('2018-04')}) 这是错误的.