使用ggplot2包绘图

散点图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
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值