我们继续来看看接下来的内容!
11.对图形如何进行渐变色的填充?
如下图所示,随着数值越大,颜色越发蓝色。但这种颜色不是我们喜欢的类型,如何更改呢?
ggplot(heightweight,aes(ageYear,heightIn,fill=weightLb))+
geom_point(shape=21,size=3)
这里,我们使用scale_fill_gradient()函数将颜色区间变成从蓝色到红色
ggplot(heightweight,aes(ageYear,heightIn,fill=weightLb))+
geom_point(shape=21,size=3)+
scale_fill_gradient(low = "blue",high = "red")
12.如何绘制双图形——点图+箱线图?
如果我们想要左边展示数据点,右边想要展示箱线图,如何展示呢?下面给出了解答。
ggplot(heightweight,aes(sex,heightIn))+
geom_boxplot(aes(as.numeric(sex)-0.2,group=sex),width=.25)+
geom_dotplot(aes(as.numeric(sex)+0.2,group=sex),
binaxis = "y",binwidth = 0.5,stackdir = "center")+
scale_x_continuous(breaks = 1:nlevels(heightweight$sex),
labels = levels(heightweight$sex))
13.X和Y轴如何交换呢?
在绘图的时候,有时需要交换x和y轴,如何做呢?直接使用coord_flip()函数,交换坐标轴即可。
ggplot(PlantGrowth,aes(group,weight))+
geom_boxplot()+
coord_flip()
14.坐标轴如何翻转呢?或者颠倒顺序?
假设我们需要将x或者y坐标轴进行翻转或者颠倒顺序,该怎么做呢?
ggplot(PlantGrowth,aes(group,weight))+
geom_boxplot()+
scale_x_discrete(limits=rev(levels(PlantGrowth$group)))
15.上述坐标轴翻转,是针对因子型x轴进行翻转,那么如果是连续型的y坐标轴呢?
连续型坐标轴的翻转如何做?
ggplot(PlantGrowth,aes(group,weight))+
geom_boxplot()+
scale_y_reverse()
或者使用下面的办法也行,通过调整y轴的范围进行逆转
ggplot(PlantGrowth,aes(group,weight))+
geom_boxplot()+
ylim(6.5,3.5)
16.坐标轴的范围以及间距如何调整?
想要设置坐标轴的间距和范围如何设置?
通过使用scale_x_continuous(breaks=seq())来设置,0(起点),420(终点),30(间距)
library(gcookbook)
ggplot(marathon,aes(Half,Full))+
geom_point()+
scale_x_continuous(breaks = seq(0,420,10))+
scale_y_continuous(breaks = seq(0,420,30))
17.绘图中增加文字,太长了如何换行?
在绘图中,我们使用的文字太长了,如何进行换行展示呢?如下所示
library(gcookbook)
ggplot(marathon,aes(Half,Full))+
geom_point()+
scale_x_continuous(breaks = seq(0,420,10))+
scale_y_continuous(breaks = seq(0,420,30))+
xlab("看这里,没错,这就是x轴!")
使用换行符(\n)进行换行:在标签中使用\n进行断行即可
library(gcookbook)
ggplot(marathon,aes(Half,Full))+
geom_point()+
scale_x_continuous(breaks = seq(0,420,10))+
scale_y_continuous(breaks = seq(0,420,30))+
xlab("看这里,没错,\n这就是x轴!")
18.如何使得X和Y的起点重合在一起?
在x和y轴加粗后,起点位置会间断,无法重合,如下所示
ggplot(marathon,aes(Half,Full))+
geom_point()+
theme(axis.line = element_line(colour = "red",size=5))
我们在主题中增加语法lineend="square",使其重合
ggplot(marathon,aes(Half,Full))+
geom_point()+
theme(axis.line = element_line(colour = "red",size=5,lineend = "square"))
19.想把柱状图变成一幅环形图,如何做?
柱状图如何快速变成环形图?
ggplot(mtcars,aes(mpg,fill=factor(cyl)))+
geom_histogram(binwidth=1)
使用coord_polar()快速变换即可
ggplot(mtcars,aes(mpg,fill=factor(cyl)))+
geom_histogram(binwidth=1)+
coord_polar()
20.控制主题元素和文本集合对象的参数都有什么?
主题元素 | 文本几何对象 | 说明 |
family | family | Helvetica(无衬线),Times(衬线),Courier(等宽) |
face | fontface | plain(普通),bold(粗体),italic(斜体),bold.italic(粗斜体) |
colour | colour | 文本颜色 |
size | size | 字体大小 |
hjust | hjust | 横向对齐:0=左对齐,0.5=居中,1=右对齐 |
vjust | vjust | 纵向对齐:0=底部对齐,0.5=居中,1=顶部对齐 |
angle | angle | 旋转角度,单位为度 |
lineheight | lineheight | 行间距倍数 |
21.如何移除图例?
一幅图的图例,有时候会不需要,如何移除?如下所示
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()
使用下列语法进行移除即可
- guides(fill=FALSE)
- scale_fill_discrete(guide=FALSE)
- theme(legend.position="none")
三种方式均可以达到相同的效果
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()+
guides(fill=FALSE)
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()+
scale_fill_discrete(guide=FALSE)
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()+
theme(legend.position = "none")
22.调整Legend
如何对Legend的位置,背景,边框和顺序进行调整。
- 使用legend.position=c()进行指定Legend在图中的绘制
- 使用legend.background=element_blank()对Legend的背景进行调整
- 使用legend.key=element_blank()可以将多余的边框进行删除
- 使用scale_fill_discrete(limits=c(" ", "", ""))可以进行三个标签的顺序调整
- 使用labs(fill=" ")对Legend进行命名
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()+
theme(legend.position = c(.85,.2),
legend.background = element_blank(),
legend.key = element_blank())+
scale_fill_discrete(limits=c("trt1","trt2","ctrl"))+
labs(fill="这里是Legend")
23.想用颜色,R语言中如何查看?
安装加载RColorBrewer包,如下图,左边为色带的名称,右边为颜色带
library(RColorBrewer)
display.brewer.all()
24.数据相差太大,怎么把?
在绘制谱系图或者热图时,如果数据相差太大导致一部分数据被压缩,可以使用scale()函数进行数据标准化
使用mtcars举例,标准化之前
使用scale()标准化之后
scale(mtcars)
25.绘图完成,如何保存图片?
- 适用于任何绘图:比如png图,那就是png("XX.png")+绘图+dev.off()
pdf("箱线图.pdf",width=4,height=4)
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()
dev.off()
- 适用于ggplot包
直接在最后使用ggsave()进行保存即可,仅对最后一张图生效
ggplot(PlantGrowth,aes(group,weight,fill=group))+
geom_boxplot()
ggsave("xiangxiantu.png")
- 其他图片格式包括:tiff,svg,wmf,jpg等均可
赶紧来学习吧!