>options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
#安装R包
>if(!require(ggplot2))install.packages('ggplot2',update = F,ask = F)
>if(!require(ggpubr))install.packages('ggpubr',update = F,ask = F)
>if(!require(eoffice))install.packages("eoffice",update = F,ask = F)
>if(!require(patchwork))install.packages("patchwork",update = F,ask = F)
#加载以检查是否安装成功
>library(ggplot2)
>library(ggpubr)
>library(eoffice)
>library(patchwork)
> plot(iris[,1],iris[,3],col = iris[,5])
#x轴-iris[,1],y轴-iris[,3]
> text(6.5,4, labels = 'hello')
#6.5,4-坐标 labels = 'xxx' 添加标签
>dev.off() #关闭画板
> library(ggplot2)
> ggplot(data = iris)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))
> library(ggpubr)
> ggscatter(iris,
+ x="Sepal.Length",
+ y="Petal.Length",
+ color="Species")
>library(ggplot2)
>ggplot(data = <DATA>)+
<GEOM_FUNCTION>(mapping=aes(<MAPPINGS>))
#<大写>代表着模版不是具体的代码
#<GEOM_FUNCTION>代表画图的函数
#<MAPPINGS>写横纵坐标 列名不带“”
#“+”代表ggplot()和geom_point()两个函数是同一张图
#ggplot2的特殊语法:列名不加引号,行末写加号
> ggplot(data = iris)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
> ggplot(data = iris) +
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length),
+ color = "blue")
>ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length),
size = 5, # 点的大小5mm
alpha = 0.5, # 透明度 50%
shape = 8) # 点的形状
> ggplot(data = iris)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))
> ggplot(data = iris)+
## Q1 能不能自行指定映射的具体颜色?
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))+
scale_color_manual(values = c("blue","grey","red"))
#映射 aes()
>ggplot(data = iris)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species)
#这个color是aes的参数,aes是不带引号的,存在于数据中的列名 ····映射
#手动设置
> ggplot(data = iris) +
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length),
+ color = "blue")
#这个color是geom_point的参数,是具体的颜色···手动设置
> ggplot(data = iris)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 17)
#shape=17号,实心的例子
> ggplot(data = iris)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 2)
#shape=2号,空心的例子
ggplot(data = test)+geom_point(mapping = aes(x=a,
y=b,
color=change))
ggplot(data = test)+geom_point(mapping = aes(x=a,
y=b,
color=change))+
scale_color_manual(values = c("darkgreen","grey","red"))
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species) #分面代码,根据species的取值分开
#原本该数据中是没有group这一组的
dat = iris #不推荐用data做变量名称,data是个函数
dat$Group = sample(letters[1:5],150,replace = T)
#sample(letters[1:5],150,replace = T)
#sample()函数的意思是随机抽样,默认replace = F即不放回取样,无重复值
ggplot(data = dat) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_grid(Group ~ Species)
#group取值是横着的,species是竖着的
#一个函数生成的所有的点所组成的图像,可用加号进行叠加
ggplot(data = iris) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#两个图层叠在一起 ——局部设置 仅对当前图层有效
ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+
geom_smooth()+
geom_point()
#两个图层叠在一起 ——全局设置 对所有图层有效
View(diamonds)
table(diamonds$cut) #统计取值
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
#这两个函数效果一样geom开头是画图函数,stat开头是统计变换函数
#x=cut是将cut作为横坐标,默认统计数为纵坐标,不用写y=
#5.1.不统计,是用表里的数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #自己想要写纵坐标 必须写上stat = "identity"
#5.2count改为prop(比例)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
#group=1意思是各自占总比多少
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
color = Species))+
geom_boxplot()
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) + #color=换成fill=空心变实心
geom_boxplot()+
geom_point()
#图一正确函数
geom_jitter()
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
#翻转coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
#极坐标系coord_polar()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar
bar + coord_flip()
bar + coord_polar()
ggplot(data = iris,mapping = aes(x = Species,y = Sepal.Width))+
geom_violin(aes(fill = Species))+
geom_boxplot()+
geom_jitter(aes(shape = Species))+
coord_flip()
#注意图的叠放顺序和函数写顺序有关,先写先放,谁有颜色谁写fill
library(ggpubr)
> ggscatter(iris,x="Sepal.Length",
+ y="Petal.Length",
+ color="Species")
> p <- ggboxplot(iris, x = "Species", #赋值
+ y = "Sepal.Length",
+ color = "Species",
+ shape = "Species",
+ add = "jitter")
> p
> my_comparisons <- list( c("setosa", "versicolor"), #添加p值要求为list格式
+ c("setosa", "virginica"), #哪两个写在一起表示哪两个之间要画p值线
+ c("versicolor", "virginica") )
> p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
+ stat_compare_means(label.y = 9) #指定标签显示的Y轴位置,可使用参数label.y=
pdf("iris_box_ggpubr.pdf") #保存的格式及文件名
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off() #画完了,关闭画板
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png") #保存ggpplot格式
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")
-----来自生信技能树----
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。