R语言12-绘图总结

  1. 组距
  2. 数据间隔
  3. 填充/边框
  4. 轴标签
  5. 坐标轴/背景色
  • 组距(bindwidth)/数据间隔
    组距:根据最大值,最小值确定每组组距(直方图的组宽,一个里面表示多少个x轴间隔)
    数据间隔:将breaks参数传给scale_x_continuous图层,包括起始点,终点和间隔(坐标轴上显示间隔)

qplot示例:

qplot(x=friend_count,data = pf,binwidth=25)+
  scale_x_continuous(limits = c(0,1000),
                     breaks = seq(0,1000,50))

ggplot示例:

ggplot(aes(x = friend_count), data = pf) +
  geom_histogram(binwidth = 25) +
  scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50))
  • 填充(fill)/边框(color)

qplot示例:

qplot(x=tenure,data = pf,binwidth=30,
      color=I('black'),fill=I('#F79420'))  

ggplot示例:

ggplot(aes(x = tenure), data = pf) +
   geom_histogram(binwidth = 30, color = 'black', fill = '#099DD9')
  • 轴标签:使用xlab和ylab 改变坐标轴名称

qplot示例:

qplot(x=tenure/365,data = pf,binwidth=0.25,
      xlab = 'number of years using facebook',
      ylab = 'number of users in sample',
      color=I('black'),fill=I('pink'))+
  scale_x_continuous(breaks = seq(1,7,1),lim = c(0,7))

ggplot示例:

ggplot(aes(x = friend_count, y = ..count../sum(..count..)), data = subset(pf, !is.na(gender))) +
  geom_freqpoly(aes(color = gender), binwidth=10) + 
  scale_x_continuous(limits = c(0, 1000), breaks = seq(0, 1000, 50)) + 
  xlab('好友数量') + 
  ylab('Percentage of users with that friend count')
  • 坐标轴/背景色(theme)

改变背景色+去掉网格线

ggplot(aes(x = age), data = pf) +
  geom_histogram(binwidth = 1, fill = '#5760AB')+
  theme(panel.background = element_rect(fill="#DCDCDC"),
        panel.grid = element_blank())

x轴字体旋转90°

+theme(axis.text.x=element_text(angle = 90,hjust = 1,vjust=0,size=8))  #hjust 控制水平对齐和 vjust 控制垂直对齐	

调整坐标轴字号

+theme(axis.text = element_text(size = 5) )

查看更多作图相关变换:https://ggplot2.tidyverse.org/reference/theme.html
https://ggplot2.tidyverse.org/reference/element.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值