ggplot里面含data,aes里面含横轴(纵轴)
install.packages("ggplot2")
install.packages("tidyverse")
library(ggplot2)
library(tidyverse)data(mpg)
ggplot(data=mpg,
mapping = aes(x=displ,y=hwy,colour = drv,size = cyl))+
geom_point()+
geom_smooth(method = "lm")+
facet_wrap(~drv)
theme_gray()
ggplot(data=mpg,
aes(hwy,col=drv))+
geom_boxplot()+
facet_wrap(~drv)+
coord_flip() ##颠倒横纵坐标
条形图 :geom_bar
data(msleep)
msleep %>%
drop_na(vore) %>%
ggplot(aes(vore))+
geom_bar(fill=1)+
coord_flip()+
theme_bw()+
labs(x="who",y="Freq",title = "Numbers")
帕累托图:fct_infreq (对条形图进行排序)
msleep %>%
drop_na(vore) %>%
ggplot(aes(fct_infreq(vore)))+
geom_bar(fill=2)+
coord_flip()+
theme_bw()+
labs(x="who",y="Freq",title = "Numbers")
直方图:geom_histogram
ggplot(data=mpg,
aes(hwy,fill = class))+ #根据class填充颜色
geom_histogram(breaks=seq(10,45,3),col=2)+ #边框颜色col
lims(x=c(5,50),y=c(0,60))+
scale_x_continuous(breaks = seq(10,50,3))+
scale_y_continuous(breaks = seq(0,60,10))+
labs(title ="Histogram of hwy")+
theme_bw()+
facet_wrap(~class,ncol = 3) #分组排成三列
点图 :geom_point
msleep %>%
filter(bodywt<2) %>%
ggplot(aes(bodywt,brainwt))+
geom_point(aes(colour = sleep_total,
size = awake))+
geom_smooth()+
facet_wrap(~vore)+
labs(title = "point")