ggplot2

R语言学习1:ggplot2(第一次学习)

一:可视化

问题1:大引擎的汽车比小引擎的汽车耗油多吗?(画在一张图上)

1,简单画图:

// An highlighted block
library(tidyverse)
library(ggplot2)
ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy));

在这里插入图片描述
mpg美国环境保护署收集的38种车型的观察结果:
displ 汽车的引擎
hwy,高速公路,汽车在高速公路上的燃油效率,单位为英里每加仑(英里每加仑)。低燃油效率的汽车在行驶相同距离时比高燃油效率的汽车消耗更多的燃油。

geom_point()将点图层添加到绘图中
ggplot2中的每个geom函数都有一个映射参数。

#ggplot(data = ) +<GEOM_FUNCTION>(mapping = aes()),通用格式

2用不同颜色标记

ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy,color=class))

在这里插入图片描述

3 用大小标记

ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy,size=class))

在这里插入图片描述

4,用透明度

ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy,alpha=class))

在这里插入图片描述

5,用形状

ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy,shape=class)) #一次只能使用六个形状

在这里插入图片描述

6自定义

ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy,color=blue))

在这里插入图片描述
为什么不是蓝色?

ggplot(data=mpg)+geom_point(mapping=aes(x=displ,y=hwy),color=blue)

在这里插入图片描述
+不能出现在行的开头

2.分割

ggplot(data=mpg)+
  geom_point(mapping=aes(x=displ,y=hwy))+
               facet_wrap(~class,nrow=2)

在这里插入图片描述

ggplot(data=mpg)+
  geom_point(mapping=aes(x=displ,y=hwy))+
               facet_grid(drv~cyl)

在这里插入图片描述

1.ggplot(data=mpg)+
      geom_point(mapping=aes(x=displ,y=hwy))+
           facet_grid(drv~.)
2.ggplot(data = mpg) +
     geom_point(mapping = aes(x = displ, y = hwy)) +
          facet_grid(. ~ cyl)
3.ggplot(data = mpg) +
     geom_point(mapping = aes(x = displ, y = hwy)) +
         facet_wrap(~ class, nrow = 2)               

3使用不同的图形

上面的是散点图

 ggplot(data=mpg)+
      geom_smooth(mapping=aes(x=displ,y=hwy))线性图

在这里插入图片描述

 ggplot(data=mpg)+
      geom_smooth(mapping=aes(x=displ,y=hwy,linetype=driv))

根据driv值将汽车分为三条线
在这里插入图片描述

1.ggplot(data=mpg)+
    geom_smooth(mapping=aes(x=displ,y=hwy,group=driv))按组
2.ggplot(data = mpg) +
     geom_smooth(
    mapping = aes(x = displ, y = hwy, color = drv),
    show.legend = FALSE )不显示注释
3.ggplot(data = mpg) +
   geom_point(mapping = aes(x = displ, y = hwy)) +
   geom_smooth(mapping = aes(x = displ, y = hwy))散点图+线性图4.ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
     geom_point(mapping = aes(color = class)) +
        geom_smooth()
5.ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
      geom_point() +
         geom_smooth()
 ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut)) #柱形图
  geom_bar 相当于stat_count 计数

在这里插入图片描述

 demo <- tribble(
  ~a,      ~b,
  "bar_1", 20,
  "bar_2", 30,
  "bar_3", 40
)
ggplot(data = demo) +
  geom_bar(
    mapping = aes(x = a, y = b), stat = "identity"
  )

在这里插入图片描述

1.ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, color = cut))
2.ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = cut))
3.ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, fill = clarity))
4.ggplot(data = diamonds) +
  geom_bar(
    mapping = aes(x = cut, fill = color, y = ..prop..)
  )
5.ggplot(data = diamonds) +
  stat_summary(
    mapping = aes(x = cut, y = depth),
    fun.ymin = min,
    fun.ymax = max,
    fun.y = median
  )

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

ggplot(
  data = diamonds,
  mapping = aes(x = cut, fill = clarity)
)+
  geom_bar(alpha = 1/5, position = "identity")

在这里插入图片描述

ggplot(
  data = diamonds,
  mapping = aes(x = cut, color = clarity)
)+
  geom_bar(fill = NA, position = "identity")

在这里插入图片描述

ggplot(data = diamonds) +
  geom_bar(
    mapping = aes(x = cut, fill = clarity),
    position = "dodge"
  )

在这里插入图片描述

ggplot(data = diamonds) +
  geom_bar(
    mapping = aes(x = cut, fill = clarity),
    position = "fill"
  )

在这里插入图片描述
箱线图

1.ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot()
2.ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot() +
  coord_flip()#转换x轴和y轴

在这里插入图片描述
极坐标

bar <- ggplot(data = diamonds) +
 geom_bar(
   mapping = aes(x = cut, fill = cut), show.legend = FALSE,
   width = 1
 )+
 theme(aspect.ratio = 1) + labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()

在这里插入图片描述
模块
ggplot(data = ) +
<geom_function>(
mapping = aes(),
stat = ,
position =
)+ <coordinate_function> + <facet_funcrion>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值