R语言ggplot2——折线图

r 复制代码
BMI <- read.table('/Users/zhangzhishuai/Downloads/33 lesson33 ggplot2散点图(一)/33_ggplot2/BMI.txt', header = T,sep = '\t', row.names = 1)
library(ggplot2)
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line() # 折线图

# 加文字
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line() +
  geom_text(label = rownames(BMI)) #加名字(用行名)

# 设置文字颜色
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line() + 
  geom_text(label = rownames(BMI), colour = BMI$age) # 根据年龄标注颜色
  
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line() + 
  geom_text(label = rownames(BMI), aes(colour = age)) + # 根据年龄标注颜色(将年龄放入aes中)
  scale_colour_gradient(low = 'yellow', high = 'red') # 改变图注渐变色

# 使用geom_label加文字,会在文字周围加一个边框,其他和geom_text一样
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line() + 
  geom_label(label = rownames(BMI), aes(colour = age)) + # 根据年龄标注颜色(将年龄放入aes中)
  scale_colour_gradient(low = 'yellow', high = 'red') # 改变图注渐变色

# 改变线的属性和点的属性
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line(colour = 'red', linetype=2 #控制线类型相当于lty
            , size=2 # 控制线粗细相当于lwd
            ) +
  geom_point(size = 8, shape = 20) # 控制点的大小和形状

# 根据连续变量改变点的颜色
ggplot(BMI, aes(x=weight,y=height)) +
  geom_line(colour = 'red', linetype=2, size=2) +
  geom_point(size = 8, shape = 20, aes(color = age) # 连续变量控制颜色
             ) + 
  scale_colour_gradient(low = 'yellow', high = 'red') 

# 分组划折线
ggplot(BMI, aes(x=weight,y=height, group = gender)) +
  geom_line(aes(color = gender, linetype = gender), size = 1) + # 改变线的分组颜色
  geom_point(aes(color = gender, shape = gender), size = 3) # 改变点的分组颜色

# 划多条折线
BMI$name = rownames(BMI)
ggplot(BMI,aes(x=1:6,y=height)) +
  geom_line(color='red',linetype=1) +
  geom_point(color = 'red') +
  geom_line(aes(x=1:6,y=weight),linetype=2,size=2) +
  geom_point(aes(x=1:6,y=weight),size=2)

ggplot(BMI,aes(x=name,group=1)) +
  geom_line(aes(y=weight),color='red',linetype=1) +
  geom_line(aes(y=height),color='steelblue',linetype='twodash') +
  geom_line(aes(y=age),color='green',linetype='dashed') + 
  geom_line(aes(y=BMI),color='purple',linetype='dotdash')

# 一次性划多条折线
library('reshape2')
library('ggplot2')
bmi = melt(BMI[,-3],id='name')  
ggplot(bmi,aes(x=name,y=value,group=variable,linetype=variable,color = variable)) +
  geom_line(size=1) + 
  geom_point(aes(shape=variable,color=variable),size=2)

# 保存图片
ggsave(file = '/Users/zhangzhishuai/Downloads/33 lesson33 ggplot2散点图(一)/33_ggplot2/line.pdf',width = 7,height = 9)

示例文件BMI.txt:

html 复制代码
name	height	weight	gender	BMI	age
tom	180	75	male	23.14814815	38
cindy	165	58	female	21.30394858	45
jimmy	175	72	male	23.51020408	43
sam	173	68	male	22.72043837	35
lucy	160	60	female	23.4375	32
lily	163	55	female	20.2020202	28
相关推荐
maizeman1262 分钟前
R语言——散点图
开发语言·r语言·可视化·散点图
BanyeBirth9 分钟前
C++高精度算法(加、减、乘)
开发语言·c++·算法
Aerkui16 分钟前
Python面向对象-开闭原则(OCP)
开发语言·python·开闭原则
"_rainbow_"20 分钟前
Qt中的鼠标事件
开发语言·qt
缘来的精彩29 分钟前
kotlin 多个fragment beginTransaction容器添加使用
android·开发语言·kotlin
安小牛30 分钟前
Kotlin 学习-集合
android·开发语言·学习·kotlin
Peter_chq36 分钟前
selenium快速入门
linux·开发语言·chrome·python·selenium
双叶83641 分钟前
(51单片机)串口通讯(串口通讯教程)(串口接收发送教程)
c语言·开发语言·c++·单片机·嵌入式硬件·microsoft·51单片机
_x_w1 小时前
【12】数据结构之基于线性表的排序算法
开发语言·数据结构·笔记·python·算法·链表·排序算法
不爱学英文的码字机器1 小时前
Rust 的征服:从系统编程到全栈开发的 IT 新宠
开发语言·后端·rust