前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R4DS-学习记录-ggplot2_2-CG

R4DS-学习记录-ggplot2_2-CG

原创
作者头像
Crazy_George
发布2024-04-01 22:12:29
820
发布2024-04-01 22:12:29
举报
文章被收录于专栏:R语言R语言

练习思路和数据完全来自于R4DS(R for data science)教材

加载R包

代码语言:r
复制
library(tidyverse)
library(palmerpenguins)
library(ggthemes)

1. categorical variable

变量(variable)的值(value)较少时。明确的结果。

代码语言:r
复制
ggplot(penguins, aes(x = species)) +
  geom_bar()#non-ordered levels
  • Doing so requires transforming the variable to a factor (how R handles categorical data) and then reordering the levels of that factor.
代码语言:r
复制
ggplot(penguins, aes(x = fct_infreq(species))) +
  geom_bar()#转换因子

2. A numerical variable

A variable is numerical (or quantitative) if it can take on a wide range of numerical values,visualization for distributions of continuous variables is a histogram.

代码语言:r
复制
> ggplot(penguins,aes(x=body_mass_g))+
+   geom_histogram()
#`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#Warning message:
#Removed 2 rows containing non-finite outside the scale range (`stat_bin()`). 
> ggplot(penguins,aes(x=body_mass_g))+
+   geom_histogram(binwidth = 200)
binwidth = 200
binwidth = 200
binwidth = 30
binwidth = 30
  • shows fewer details than a histogram but can make it easier to quickly glean the shape of the distribution, particularly with respect to modes and skewness.
代码语言:r
复制
ggplot(penguins,aes(x = body_mass_g))+
  geom_density()
density!更重趋势,少细节
density!更重趋势,少细节

练习

1.

代码语言:r
复制
ggplot(penguins,aes(y = species))+
  geom_bar()

2.

分析那种加颜色的方式比较有用

代码语言:r
复制
ggplot(penguins, aes(x = species)) +
  geom_bar(color = "red")#红色描边

ggplot(penguins, aes(x = species)) +
  geom_bar(fill = "red")#红色填充

3.

What does the bins argument in geom_histogram() do?

It determines the number of bins (bars) in a histogram.

4. Make a histogram of the carat variable in the diamonds dataset that is available when you load the tidyverse package. Experiment with different binwidths. What binwidth reveals the most interesting patterns?

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 1. categorical variable
  • 2. A numerical variable
  • 练习
    • 1.
      • 2.
        • 3.
          • 4. Make a histogram of the carat variable in the diamonds dataset that is available when you load the tidyverse package. Experiment with different binwidths. What binwidth reveals the most interesting patterns?
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档