R语言心情笔记

Data Visualization with ggplot2

【BB有话说】:仅仅是作为学习笔记使用的,如有错误请批评指正,会不定期持续更新

  1. Creating a ggplot
    ggplot(data = mpg) +
    geom_point(mapping = aes(x = displ, y = hwy, color = …, size = …, alpha = …, shape = …))

  2. ggplot(): creates a coordinate system that you can add layers to
    Parameters:
     First one: the dataset

  3. geom_point(): add a layer of points to your plot
    Parameters:
     mapping: how variables in your dataset are mapped to visual properties; 总是和aes一起使用,aes中的x和y表明哪些变量map to the x and y
     aes中的其他变量:
    color: 改变points的颜色,可以通过不同的类别改变
    size: 改变大小
    shape:改变形状;值得注意:ggplot2 will only use six shapes at a time
    alpha: 改变点的透明度
     也可以不用传递给mapping来设置color等: it goes outside of aes()
    ggplot(data = mpg) +
    geom_point(mapping = aes(x = displ, y = hwy), color = …)
    【Graphing Template】:
    ggplot(data = )+
    <GEOM_FUNCTION>(mapping = aes()

  4. Facets
    To facet your plot by a single variable use facet_wrap()
    Parameters:
    (1) Formula: begin with ~ and followed by a variable name
    ggplot(data = mpg) +
    geom_point(mapping = aes(x = displ, y = hwy, color = …, size = …, alpha = …, shape = …))
    facet_wrap(~class, nrow = 2)
    如果是plot the combination of two variablesfacet_grid(), 第一个参数还是formula,用~分隔开两个变量
    facet_grid(var1~var2)
    if you prefer to not facet in the rows or columns dimension, use . instead of 变量名facet_grid(.~var2)

  5. Geometric Objects
    People often describe plots by the type of geom that the plot uses
    每一个geom function 都有mapping arguments
    【Tips】:
    如果把mapping参数直接传递给ggplot()则其认为为全局变量
    如果是传递给geom function则认为是局部变量for this layer. It will use these mappings to extend or overerite the gl
    两次geom function有叠加的效果
    ggplot(data = mpg,mapping = aes(x = displ, y = hwy, color = …, size = …, alpha = …, shape = …)) +
    geom_point()
    gemo_smooth()

  6. Statistical Transformation
    Bar Chart
    ggplot(data = diamonds)+
    geom_bar(mapping = aes(x=cut)) / stat_count(mapping = aes(x=cut))/geom_bar(mapping = aes(x=cut, y=…prop…, group = 1) # 画占比instead of count
    Bar charts, histograms, and frequency polygons bin your data and then plot bin counts, the number of points that fall in each bin
    简而言之,这种图具有统计作用,以上提到的一些plots是来统计各分类的频数之类的
    ggplot(data = diamonds)+
    stat_count(mapping = aes(x=cut)) # 和geom_bar()效果一样对于其默认的count统计方法

  7. Position Adjustments

    ggplot(data = diamonds) +
    geom_bar(mapping = aes(x=cut, color = cut)) / geom_bar(mapping = aes(x = cut, fill = cut)
    如果fill参数后面接除了cut之外的变量,自动变成stacked

    【未完结1/20/2022】

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值