R语言使用ggplot绘制点线和区域,并亲自为之着色

临风暖阳 发表于 2022/11/05 10:56:06 2022/11/05
【摘要】 笔者用ggplot()绘制线型和点,并绘制了区域,给区域填充颜色,并为区域添加点。

将点的尺寸设置成5,shape设置成21,再把线和点着色,就得到如下效果:

# Load plyr so we can use ddply() to create the example data set

library(plyr)

library(tidyverse)


ggplot(BOD, aes(x=Time, y=demand)) +
  
  geom_line(size=1, shape=22,color='cyan') +
  
  geom_point(size=5, shape=21, colour="brown", fill="violet")

使用参考文献的数据集,设置线型的颜色为褐色,点的尺寸为5,shape为21,并将点的颜色设置成青色和金麒麟色,效果如下,position_dodge的值主要用来调整两条线之间的间距。

# Load plyr so we can use ddply() to create the example data set

library(plyr)

library(tidyverse)

# Summarize the ToothGrowth data

tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length=mean(len))

# Save the position_dodge specification because we'll use it multiple times

pd <- position_dodge(0.2)

ggplot(tg, aes(x=dose, y=length, fill=supp)) +
  
  geom_line(position=pd,colour='brown') +
  
  geom_point(shape=21, size=5, position=pd) +
  
  scale_fill_manual(values=c("cyan","goldenrod"))

笔者主要为area设置了填充色和边框色,分别为金麒麟色和青色

# Load plyr so we can use ddply() to create the example data set

library(plyr)

library(tidyverse)

sunspotyear <- data.frame(
  
  Year = as.numeric(time(sunspot.year)),
  
  Sunspots = as.numeric(sunspot.year)
)

ggplot(sunspotyear, aes(x=Year, y=Sunspots)) + geom_area(color='cyan',fill='goldenrod')

为区域和线添加点

ggplot(sunspotyear, aes(x=Year, y=Sunspots)) + geom_area(color='cyan',fill='goldenrod')+geom_point()

参考文献:Practical Receipes for Visualizing Data----R Graphics Cookbook —Winston Chang O’REILLY

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。