ggplot2-条形图和折线图
author: 李丕栋
email: hope-dream@163.com
date: 2016年3月7日http://blog.csdn.net/tanzuozhev/article/details/50822204
本文在 http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2) 的基础上加入了自己的理解.
ggplot2 接受的数据类型必须为data.frame结构,
离散数据作为x轴
对于条形图, 对于高度的设置有两种不同的选择:
- x,y 对应的数值为实际的图上数值, x为横轴标签,y为纵轴高度.这时候使用
geom_bar(stat="identity")
作为图层.
library(ggplot2)
dat <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),total_bill = c(14.89, 17.23)
)
dat
## time total_bill
## 1 Lunch 14.89
## 2 Dinner 17.23
time
列为因子型变量, 表示x轴标签和填充颜色
total_bill
列为y轴的实际数值, 表示高度
ggplot(data=dat, aes(x=time, y=total_bill)) +geom_bar(stat="identity")
# 以time作为颜色填充
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +geom_bar(stat="identity")
## 等同于ggplot(data=dat, aes(x=time, y=total_bill)) +geom_bar(aes(fill=time), stat="identity")
# 添加黑色轮廓线
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +geom_bar(colour="black", stat="identity")
# 去除图例
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +geom_bar(colour="black", stat="identity") +guides(fill=FALSE)
# 添加其他信息 title, narrower bars, fill color, and change axis labels
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") + guides(fill=FALSE) +xlab("Time of day") + ylab("Total bill") +ggtitle("Average bill for 2 people")
- 输入一组数据,对于x轴与y轴的信息需要进行统计计数.x轴为数据去除重复项的保留值,y轴为x轴对应的重复次数.使用
geom_bar(stat="bin")
作为新图层.
# 使用reshape2包的tips数据集
library(reshape2)
# 数据展示
head(tips)
## total_bill tip sex smoker day time size
## 1 16.99 1.01 Female No Sun Dinner 2
## 2 10.34 1.66 Male No Sun Dinner 3
## 3 21.01 3.50 Male No Sun Dinner 3
## 4 23.68 3.31 Male No Sun Dinner 2
## 5 24.59 3.61 Female No Sun Dinner 4
## 6 25.29 4.71 Male No Sun Dinner 4
这里输入的变量只有x,没有y,x轴为day,要使用 stat="bin"
代替 stat="identity"
,数据去重后留下Sun Sat Thur Fri,它们对应的重复次数作为y轴.
# Bar graph of counts
ggplot(data=tips, aes(x=day,fill=day)) +geom_bar(stat="bin")
## 等同于
ggplot(data=tips, aes(x=day)) +geom_bar()# stat参数默认为 bin
折线图
time: x-axis
total_bill: y-axis
# Basic line graph
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +geom_line()
## This would have the same result as above
# ggplot(data=dat, aes(x=time, y=total_bill)) +
# geom_line(aes(group=1))# 折线图添加点
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +geom_line() +geom_point()
# 修改颜色
# Change line type and point type, and use thicker line and larger points
# Change points to circles with white fill
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) + geom_line(colour="red", linetype="dashed", size=1.5) + geom_point(colour="red", size=4, shape=21, fill="white")
# Change the y-range to go from 0 to the maximum value in the total_bill column,
# and change axis labels
# 修改y轴的范围,从0到最大值
ggplot(data=dat, aes(x=time, y=total_bill, group=1)) +geom_line() +geom_point() +expand_limits(y=0) +# 修改y轴的范围,从0到最大值 expand_limits(y = c(1, 9)),y从1到9xlab("Time of day") + ylab("Total bill") +ggtitle("Average bill for 2 people")
更多数据变量
新建数据,这里增加了一个变量sex
dat1 <- data.frame(sex = factor(c("Female","Female","Male","Male")),time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),total_bill = c(13.53, 16.81, 16.24, 17.42)
)
dat1
## sex time total_bill
## 1 Female Lunch 13.53
## 2 Female Dinner 16.81
## 3 Male Lunch 16.24
## 4 Male Dinner 17.42
条形图
变量映射
time: x-axis
sex: color fill
total_bill: y-axis.
# 这里涉及了几个图形的位置摆放
# 默认为堆叠(Stacked bar graph)
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +geom_bar(stat="identity")
# 位置摆放, position_dodge()为分开摆放ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +geom_bar(stat="identity", position=position_dodge())
# Change colors
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) +geom_bar(stat="identity", position=position_dodge(), colour="black") +scale_fill_manual(values=c("#999999", "#E69F00"))# 修改填充的颜色,填充的颜色数组大小必须与fill(sex)的大小一致
修改变量的映射,x轴为sex,颜色填充为time
# Bar graph, time on x-axis, color fill grouped by sex -- use position_dodge()
ggplot(data=dat1, aes(x=sex, y=total_bill, fill=time)) +geom_bar(stat="identity", position=position_dodge(), colour="black")
折线图
变量映射
time: x-axis
sex: line color
total_bill: y-axis.
为了画出多条线,数据必须进行分组, 这里我们对sex
进行分组,就会出现两条线,Female
一条,Male
一条.
# 简单图
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex)) +geom_line() +geom_point()
# 加入颜色
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, colour=sex)) +geom_line() +geom_point()
# Map sex to different point shape, and use larger points
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex)) +geom_line() +geom_point()
# Use thicker lines and larger points, and hollow white-filled points
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex)) + geom_line(size=1.5) + geom_point(size=3, fill="white") +scale_shape_manual(values=c(22,21))# 修改shape的类型
修改变量的映射关系,按照time进行分组,Lunch一组,Dinner一组
ggplot(data=dat1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) +geom_line() +geom_point()
例子
条形图
ggplot(data=dat1, aes(x=time, y=total_bill, fill=sex)) + geom_bar(colour="black", stat="identity",position=position_dodge(),size=.3) + # Thinner linesscale_fill_hue(name="Sex of payer") + # Set legend titlexlab("Time of day") + ylab("Total bill") + # Set axis labelsggtitle("Average bill for 2 people") + # Set titletheme_bw()
折线图
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line(aes(linetype=sex), size=1) + # Set linetype by sexgeom_point(size=3, fill="white") + # Use larger points, fill with whiteexpand_limits(y=0) + # 设置x y轴的起止范围,这里是y从0开始scale_colour_hue(name="Sex of payer", # Set legend titlel=30) + # Use darker colors (lightness=30)scale_shape_manual(name="Sex of payer",values=c(22,21)) + # Use points with a fill colorscale_linetype_discrete(name="Sex of payer") +xlab("Time of day") + ylab("Total bill") + # Set axis labelsggtitle("Average bill for 2 people") + # Set titletheme_bw() + # 设置主题theme(legend.position=c(.7, .4)) # 设置图例的位置
这幅折线图中, 使用了颜色scale_colour_hue
,形状 scale_shape_manual
,线型scale_linetype_discrete
三种属性,应该有3个图例,但是因为图例的名称相同所以归为一类,如果三个图例的名称不同,就会出现3个图例.
ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line(aes(linetype=sex), size=1) + # Set linetype by sexgeom_point(size=3, fill="white") + # Use larger points, fill with whiteexpand_limits(y=0) + # 设置x y轴的起止范围,这里是y从0开始scale_colour_hue(name="Sex of payer1", # Set legend titlel=30) + # Use darker colors (lightness=30)scale_shape_manual(name="Sex of payer2",values=c(22,21)) + # Use points with a fill colorscale_linetype_discrete(name="Sex of payer3") +xlab("Time of day") + ylab("Total bill") + # Set axis labelsggtitle("Average bill for 2 people") + # Set titletheme_bw() + # 设置主题theme(legend.position=c(.7, .4)) # 设置图例的位置
连续型数据做x轴
新建数据
datn <- read.table(header=TRUE, text='
supp dose lengthOJ 0.5 13.23OJ 1.0 22.70OJ 2.0 26.06VC 0.5 7.98VC 1.0 16.77VC 2.0 26.14
')
dose作为x轴, 这里dose为numeric,视为连续型变量
ggplot(data=datn, aes(x=dose, y=length, group=supp, colour=supp)) +geom_line() +geom_point()
当把dose作为连续型变量时,尽管dose只有 0.5, 1.0, 2.0 三类,x轴也必须显示0.5,1.0,1.5,2.0甚至更多的点.
离散型数据做x轴
这里我们将dose数据转化为factor类型,就成了离散型, 0.5, 1.0, 2.0就只是单纯的类别名称.
# Copy the data frame and convert dose to a factor
datn2 <- datn
datn2$dose <- factor(datn2$dose)
ggplot(data=datn2, aes(x=dose, y=length, group=supp, colour=supp)) +geom_line() +geom_point()
# 直接在ggplot中转换格式也是可以的
ggplot(data=datn, aes(x=factor(dose), y=length, group=supp, colour=supp)) +geom_line() +geom_point()
连续型数据和离散型用于条形图, 得到了相同的图.
# Use datn2 from above
ggplot(data=datn2, aes(x=dose, y=length, fill=supp)) +geom_bar(stat="identity", position=position_dodge())
# 直接使用factor转化
ggplot(data=datn, aes(x=factor(dose), y=length, fill=supp)) +geom_bar(stat="identity", position=position_dodge())
ggplot2-条形图和折线图相关推荐
- 地图图表、柱状图、条形图、折线图、中国地图、世界地图、省市地图、仪表盘、雷达图、饼图、散点图、气泡图、瀑布图、堆叠图、热力图、桑基图、关系图、漏斗图、Axure原型、rp原型、产品原型
地图图表.柱状图.条形图.折线图.中国地图.世界地图.省市地图.仪表盘.雷达图.饼图.散点图.瀑布图.气泡图.堆叠图.热力图.桑基图.关系图.漏斗图.Axure原型.rp原型.产品原型.大屏设计必备组 ...
- 可视化框架、Axure原型、大屏可视化、图表组件、图表元件库、统计图表、数据可视化模板、条形图、折线图、散点图、时间轴、仪表盘、饼图、散点图、雷达图、高山图、登录模板、弹窗、弹幕、预警、散点图
可视化框架.数据可视化综合管理平台.大屏可视化.图表组件.图表元件库.统计图表.数据可视化模板.条形图.折线图.散点图.时间轴.仪表盘.饼图.散点图.雷达图.高山图.登录模板.弹窗.弹幕.预警.散点图 ...
- Excel实例:Excel图表可视化:条形图、折线图、散点图和步骤图
原文链接:http://tecdat.cn/?p=16539 Excel提供了相当广泛的功能来创建图形,即Excel所谓的 图表.您可以通过选择插入>图表来访问Excel的图表功能 .我们将 ...
- Android 开发第三方框架制作条形图、折线图、饼状图、水平条形图
Android 第三方框架MPAndroidChart-v3.1.0的简单使用 这里写目录标题 Android 第三方框架MPAndroidChart-v3.1.0的简单使用 前言 引入库 布局 初始 ...
- 如何用pyecharts绘制柱状图,条形图,折线图,饼图,环形图,散点图
简介 pyecharts是一个由百度开源的数据可视化,凭借着良好的互交性,精巧的图表设计,得到了众多开发者的认可,而python是一门富有表达力的语言,很适合用于数据处理.当数据分析遇上数据可视化时, ...
- python实现直方图、条形图、折线图、饼图(参数详情)
,注意:条形图是用条形的长度表示各类别频数的多少,其宽度(表示类别)则是固定的: 直方图是用面积表示各组频数的多少 1.直方图hist()函数 hist()函数: 直方图的参数只有一个X 参数说明: ...
- excel基础图形笔记(柱状图、条形图、折线图、饼图、散点图、箱线图、漏斗图)
excel画图小记: 1. 高:10 :宽:16.17:(符合0.618黄金比例) 具体操作:选中图表->右键->设置图标区域格式->大小与属性: 2.看销量排名,一般用条形图(横放 ...
- 20180416-E · Global Mortality · ggplot2 plotly 动态折线图 · R 语言数据可视化 案例 源码
所有作品合集传送门: Tidy Tuesday 2018 年合集传送门: 2018 Global Mortality What do people die from? 在过去的几个世纪里,世界发生了很 ...
- 【python数据分析】用python进行数据探索1(含各种数据基础分析方法以及直方图、条形图、折线图等基本画法)
从这周开始,我将在此记录我对<python数据分析与挖掘实战>(第二版)的跟读情况,将我认为的值得学习的点记录在这里,有时候也会对相关知识进行拓展,保持每周更新3-4次的频率,争取在下次开 ...
- POI Word 图表、柱状图、条形图、折线图、饼图
poi Excel 图表:https://blog.csdn.net/u014644574/article/details/105695787 1.pom.xml <dependency> ...
最新文章
- 识骨寻踪:少年,我看你骨骼清奇,不如来看看这本书。
- mysql init file_关于MySQL的init-file选项的用法实例
- Java中空值处理的感受
- java多线程 异常处理_Java8多线程ForkJoinPool:处理异常
- 201312-5 I’m stuck!
- linux 编译c q64,Ubuntu 12.04 LTS 64位搭建Qt4终端编译环境
- 2021-07-01样式和图片标签
- Linux下的uniq,head,tail,cut,paste
- 网络之路--【第六章】——ICMP协议
- Java中string,map,json之间的常用转换方法(json转map,map转json,json转string等)
- SQL 正则表达式 匹配方式
- 2022年申请亳州市发明专利材料,专利说明书摘要写作技巧
- forge下载java_我的世界forge1.9.4
- CA数字证书是什么意思?SSL证书与CA数字证书有什么区别?
- STM32串口屏应用
- XILINX GTX/GTP 使用小结
- 上下文切换频繁,导致load average 过高
- 触宝笔试面试回忆版(不完整)-16暑期实习
- zabbix wechat 报警
- 计算机个性化桌面后总是恢复,电脑显示我的电脑需要恢复怎么办_开机显示你的电脑需要修复如何解决-系统城...
热门文章
- Android中通过来电转移实现“电话已关机”,“此号码已停机”等
- VisionBank机器视觉软件-工业机器视觉检测
- .NET中LinkButton的使用
- Rancher部署日志
- HDU 4416 后缀自动机
- 2008年6月it公司红黑榜/口碑榜
- 5.5leecode刷题记录(leecode704.二分查找,leecode.27移除元素)
- 为降低普及门槛,鑫洋泉将全景环视做成“算法引擎”
- perl中bless的理解
- LDK3读书笔记(第二章:从内核出发)