控制座標軸和刻度線外觀的函數svg
函數 | 選項 |
scale_x_continuous()和 scale_y_continuous() |
breaks=指定刻度標記,labels=指定刻度標記標籤, limits=控制要展現的值的範圍 |
scale_x_discrete()和 scale_y_discrete() |
breaks=對因子的水平進行放置和排序,labels=指定這些水平的標籤, limits=表示哪些水平應該展現 |
coord_flip() | 顛倒 x 軸和 y 軸 |
按學術等級和性別分組的薪資水平函數
> data(Salaries,package="car") > library(ggplot2) > ggplot(data=Salaries, aes(x=rank, y=salary, fill=sex)) + + geom_boxplot() + + scale_x_discrete(breaks=c("AsstProf", "AssocProf", "Prof"), #對x軸因子進行自定義 + labels=c("Assistant\nProfessor", + "Associate\nProfessor", + "Full\nProfessor")) + + scale_y_continuous(breaks=c(50000, 100000, 150000, 200000), #對Y軸刻度進行處理 + labels=c("$50K", "$100K", "$150K", "$200K")) + + labs(title="Faculty Salary by Rank and Sex", x="", y="") #將 x y 軸名稱都設置爲空
圖例是指如用顏色、形狀、尺寸等視覺特性表示數據特徵的指南spa
在labs中添加 fill=「myvar」code
由theme()函數中的 legend.position選項控制,可能的值包括「left」、「top」、「right(默認值)」,「bottom」 ,也能夠指定一個二元素向量對象
legend.position = "none"排序
> ggplot(data=Salaries, aes(x=rank, y=salary, fill=sex)) + + geom_boxplot() + + scale_x_discrete(breaks=c("AsstProf", "AssocProf", "Prof"), + labels=c("Assistant\nProfessor", + "Associate\nProfessor", + "Full\nProfessor")) + + scale_y_continuous(breaks=c(50000, 100000, 150000, 200000), + labels=c("$50K", "$100K", "$150K", "$200K")) + + labs(title="Faculty Salary by Rank and Sex",x="",y="",fill="Gender") + #fill=「」更改圖例的名稱 + theme(legend.position=c(.1,.8)) #圖例的左上角分別距離邊緣10%和底部邊緣80%
ggplot2包使用標尺把數據空間的觀察值映射可視化的空間中,標尺既能夠應用年到連續的變量,也能夠應用到離散的變量圖片
#一個連續性的標尺把 yrs.since.phd 變量的數值映射到 x 軸,同時將 salary的變量映射到 y 軸 > ggplot(mtcars, aes(x=wt, y=mpg, size=disp)) + #size = disp 生成連續變量disp(發動機排量)的標尺,並使用它控制點的尺寸 + geom_point(shape=21, color="black", fill="cornsilk") + + labs(x="Weight", y="Miles Per Gallon", + title="Bubble Chart", size="Engine\nDisplacement")
> data(Salaries, package="car") > ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary, color=rank)) + + scale_color_manual(values=c("orange", "olivedrab", "navy")) + #scale_color_manual() 函數設定三個學術等級的點的顏色 + geom_point(size=2)
薪水與助理教授、副教授、教授經驗對比散點圖,點的顏色是人爲指定的ip
經過scale_color_brewer() 和 scale_fill_brewer() 函數來預先指定分得清的顏色集ci
> ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary, color=rank)) + + scale_color_brewer(palette="Set1") + geom_point(size=2) #可將 Set1 改爲 Set二、Set三、Pastel一、Pastel二、Paired、Dark2或Accent
> library(RColorBrewer) > display.brewer.all() #得到顏色集
theme()函數element
> data(Salaries, package="car") > library(ggplot2) > mytheme <- theme(plot.title=element_text(face="bold.italic", #指定圖的標題應該爲粗斜體棕色14號 + size="14", color="brown"), + axis.title=element_text(face="bold.italic", #軸的標題爲粗斜體的棕色10號字 + size=10, color="brown"), + axis.text=element_text(face="bold", size=9, #軸標籤爲粗體的深藍色9號 + color="darkblue"), + panel.background=element_rect(fill="white", #圖片區域有白色的填充和深藍色的邊框 + color="darkblue"), + panel.grid.major.y=element_line(color="grey", #主水平網格應該是灰色的實線 + linetype=1), + panel.grid.minor.y=element_line(color="grey", #次水平網格應該是灰色的虛線 + linetype=2), + panel.grid.minor.x=element_blank(), #垂直網格不輸出 + legend.position="top") #圖例展現在頂部 #設置完成後使用 myeheme > ggplot(Salaries, aes(x=rank, y=salary, fill=sex)) + + geom_boxplot() + + labs(title="Salary by Rank and Sex", + x="Rank", y="Salary") + # +mytheme 使用以前 mytheme設置的內容 + mytheme
ggplot2中將多個圖放到單個圖形中最簡單的使用方式是 gridExtra包中的grid.arrange()函數
#先將圖都保存爲一個對象,而後用grid.arrange() > data(Salaries, package="car") > library(ggplot2) > p1 <- ggplot(data=Salaries, aes(x=rank)) + geom_bar() > p2 <- ggplot(data=Salaries, aes(x=sex)) + geom_bar() > p3 <- ggplot(data=Salaries, aes(x=yrs.since.phd, y=salary)) + geom_point() > library(gridExtra) > grid.arrange(p1, p2, p3, ncol=3)
ggplot(data=mtcars, aes(x=mpg)) + geom_histogram() ggsave(file="my.png",plot=myplot,width=5,height=4) #在當前路徑下將 my.png 的5英寸*4英寸(12.7釐米*10.2釐米)PNG格式的圖片,可設定擴展名爲ps\tex\jpeg\pdf\pg\bmp\svg或wmf,其中wmf文件僅限Windows系統 #若是忽略 plot=選項,最近建立的圖形會被保存 ggplot(data=mtcars, aes(x=mpg)) + geom_histogram() ggsave(file="mygraph.pdf") #保存mygraph.pdf到磁盤