# 加载需要的包
library(tidyverse)
library(gcookbook)
21.使用facet_grid(x~.)分页
ggplot(iris,aes(Sepal.Length)) geom_histogram(fill="lightblue",colour="red") facet_grid(Species~.)
ggplot(iris,aes(Sepal.Length,fill=Species)) geom_histogram(bins=30,position = "identity",alpha=0.5)
ggplot(faithful,aes(waiting,..density..)) geom_histogram(fill="cornsilk",colour="grey60",size=2) geom_density()
# 将x轴线限制在一个范围
ggplot(faithful,aes(waiting,..density..)) geom_histogram(fill="cornsilk",colour="grey60",size=2) geom_density() xlim(35,105)
ggplot(faithful,aes(waiting)) geom_freqpoly()
# 进一步更改
ggplot(faithful,aes(waiting)) geom_freqpoly(binwidth=4)
ggplot(iris,aes(x=factor(Species),y=Sepal.Length)) geom_boxplot(outlier.size = 5,outlier.shape = 21)
ggplot(iris,aes(x=factor(Species),y=Sepal.Length)) geom_boxplot(notch = TRUE)
ggplot(BOD,aes(Time,demand)) geom_bar(stat = "identity",fill="lightblue") geom_line(size=2,colour="red") geom_point(size=5,colour="blue")
ggplot(worldpop,aes(Year,Population)) geom_line() geom_point() scale_y_log10()
ggplot(climate,aes(Year,Anomaly10y)) geom_ribbon(aes(ymin=Anomaly10y-Unc10y, ymax=Anomaly10y Unc10y)) geom_line() xlim(1800,1850)
ggplot(climate,aes(Year,Anomaly10y)) geom_line(aes(y=Anomaly10y-Unc10y),colour="grey50",linetype="dotted") geom_line(aes(y=Anomaly10y Unc10y),colour="grey50",linetype="dotted") geom_line() xlim(1800,1850)
ggplot(mtcars,aes(mpg,cyl)) geom_point() geom_hline(yintercept = 5,size=2) geom_vline(xintercept = 25,size=2)
ggplot(mtcars,aes(mpg,cyl)) geom_point() geom_abline(intercept=1,slope =0.3)
ggplot(climate,aes(Year,Anomaly10y)) geom_line() annotate("segment",x=1850,xend = 1900, y=0,yend=-0.1,colour="blue", size=2,arrow=arrow())
ggplot(climate,aes(Year,Anomaly10y)) geom_line() annotate("rect",xmin = 1850,xmax = 1900, ymin=-0.75,ymax = 0,fill="blue",alpha=.3)
ggplot(climate,aes(Year,Anomaly10y)) geom_line() ggtitle("这里是标题")
# panel.grid.major = element_line()修改网格主线
# panel.grid.minor = element_line()修改网格次线
# panel.background = element_rect()修改网格背景颜色
#panel.border = element_rect()修改网格边线 # element_line()适合修改线
# element_rect()适合修改矩形
ggplot(climate,aes(Year,Anomaly10y)) geom_line() theme(panel.grid.major = element_line(colour="red"), panel.grid.minor = element_line(colour="red",linetype = "dashed",size=0.2), panel.background = element_rect(fill="lightblue"), panel.border = element_rect(colour = "blue",fill = NA,size=2))
37.Theme修改每个标题
# axis.title.x = element_text()修改x轴的标题
# axis.text.x = element_text()修改x轴
# plot.title = element_text()修改ggtitle的字体格式
ggplot(climate,aes(Year,Anomaly10y)) geom_line() ggtitle("这里是标题") theme( axis.title.x = element_text(colour = "red",size=24), axis.text.x = element_text(colour = "blue",size=22), axis.title.y=element_text(colour="red",size=24), axis.text.y=element_text(colour = "blue",size=22), plot.title = element_text(colour="red",size=20,face = "bold"))
p38 <- ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar)) geom_bar(stat = "identity",position ="dodge")
# legend.background = element_rect()修改legend的背景
# legend.title = element_text()修改legend的标题
# legend.text = element_text()修改legend字体颜色
# legend.key=element_rect()修改legend的外边框
p38 theme( legend.background = element_rect(fill="grey85",colour = "red",size=1), legend.title = element_text(colour="blue",face = "bold",size=14), legend.text = element_text(colour = "red",size=18), legend.key=element_rect(colour = "red",size=0.25))
# 去除legend的方法
p38 theme(legend.position = "none")
# 修改legend的位置,将其放在顶端
p38 theme(legend.position = "top")
#使用坐标固定legend的位置
p38 theme(legend.position = c(.85,.2)) theme(legend.background =element_rect(fill = "lightblue",colour="black",size=1))
# 修改legend的名称
p38 labs(fill="这是legend的名称")
# 修改legend的label的名字
p38 scale_fill_discrete(labels=c("第一","第二"))
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved