目录
散点图geom_point()
标题、标签、注释、legend,主题等设置
library(ggplot2);library(dplyr)
data(mpg)
label <- mpg %>%
summarize(displ=max(displ),hwy=max(hwy),
label='text write here')
ggplot(data=mpg,aes(displ,hwy))+
geom_point(aes(color=class,size=hwy,alpha=0.5,shape=as.factor(year),
fill=as.factor(drv)),
position='jitter' # 一定扰动
)+
scale_y_continuous(breaks=seq(15,40,by=5), # 更改坐标轴刻度
limits=range(mpg$hwy))+
scale_color_brewer(palette = 'Set1') + # 更改配色方式
scale_alpha_continuous(guide='none') + # 不显示alpha对应的legend
geom_smooth(data=filter(mpg, class=='subcompact'), # 增加曲线估计
se=F,method = 'loess',formula = 'y~x') +
geom_text(aes(label=label),data=label,vjust='top', # 添加注释
hjust='right',size=5)+
labs(title='Title',subtitle='subtitle',caption='caption',
x='x label',y='y label',
color='color label',fill='fill label',shape='year') +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.position = 'bottom') + # 默认在right
# 设定legend的显示方式,大小等
guides(color=guide_legend(nrow=3,override.aes = list(size