ggplot2繪圖工具
阿新 • • 發佈:2018-12-23
qplot
qplot(Species,Sepal.Length,data = iris,geom = "boxplot",fill=Species,
main = "根據種類分組的花萼長度")
小提琴圖
qplot(Species,Sepal.Length,data=iris,geom=c("violin","jitter"),fill=Species,main = "小提琴圖")
分面板散點圖
qplot(Sepal.Length,Sepal.Width,data = iris,geom = c("point","smooth"), facets = ~Species,col=Species,main = "分面板散點圖")
ggplot
初始化一個ggplot物件
layer
建立一個新圖層
layer(geom = NULL, stat = NULL, data = NULL, mapping = NULL,
position = NULL, params = list(), inherit.aes = TRUE,
check.aes = TRUE, check.param = TRUE, show.legend = NA)
aes
描述資料中的變數如何對映到geom的視覺屬性
ggplot(iris,aes(x=Species,y=Sepal.Length,fill=Species))+geom_boxplot()+labs(title = "箱型圖")
ggplot(singer,aes(x=height,fill=voice.part))+geom_density()+facet_grid(voice.part~.)
ggplot(singer,aes(x=height,fill=voice.part))+geom_density()+facet_wrap(voice.part~.,ncol=4)
ggplot(singer,aes(x=height,fill=voice.part))+geom_density()+facet_wrap(voice.part~.,ncol=4)+theme(legend.position = "none")
scale_color_manual scale_color_brewer改變圖形顏色
g1 <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,colour=Species,
shape=Species))+scale_color_manual(values = c("orange","olivedrab","navy"))+
geom_point(size=3)
g2 <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,colour=Species,
shape=Species))+scale_color_brewer(palette="Set1")+
geom_point(size=3)
grid.arrange(g1,g2,ncol=2)
ggsave()
library(ggthemes)
p1 <- ggplot(mtcars,aes(x=wt,y=mpg))+geom_point(size=4)
p2 <- p1+ggtitle("經濟型圖表")+theme_economist()+scale_colour_economist()
p3 <- p1+ggtitle("過度曝光")+theme_solarized()+scale_colour_solarized("blue")
grid.arrange(p2,p3,ncol=2)
p4 <- p1+ggtitle("state")+theme_stata()+scale_colour_stata()
p5 <- p1+ggtitle("Excel 2003")+theme_excel()+scale_colour_excel_new()
grid.arrange(p4,p5,ncol=2)