美文网首页
[datacamp笔记]ggplot2

[datacamp笔记]ggplot2

作者: 琼脂糖 | 来源:发表于2017-12-21 16:39 被阅读91次
  1. factor
    Notice that ggplot2 treats cyl as a factor. This time the x-axis does not contain variables like 5 or 7, only the values that are present in the dataset.
    当变量是factor的时候,轴上只出现dataset中的数值。

  2. base

# A scatter plot has been made for you
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()

# Replace ___ with the correct column
ggplot(mtcars, aes(x = wt, y = mpg, color = disp)+
  geom_point()

# Replace ___ with the correct column
ggplot(mtcars, aes(x = wt, y = mpg, size = disp)) +
  geom_point()
  1. two plot
    geom-point + geom-smooth
    一层一层往上叠加
  1. color = clarity
    将clarity作为factor分组,分别画图。

function

# Inspect the arguments of the mean() function args(mean)

  1. 关于函数mean
    用法一:mean(x, ...)
    用法二:The 'Default S3 method
    mean(x, trim = 0, na.rm = FALSE, ...)
    省略号 is a way for R to pass arguments along without the function having to name them explicitly.

'''# The linkedin and facebook vectors have already been created for you
linkedin <- c(16, 9, 13, 5, 2, 17, 14)
facebook <- c(17, 7, 5, 16, 8, 13, 14)

Calculate the mean of the sum

avg_sum <- mean(linkedin+facebook)

Calculate the trimmed mean of the sum

avg_sum_trimmed <- mean(linkedin+facebook,trim=0.2)

Inspect both new variables

avg_sum_trimmed
avg_sum
'''

avg_sum_trimmed
[1] 22.6
avg_sum
[1] 22.28571

lapply

1.lapply(X, FUN, ...)
x为向量或list
返回结果为list,长度等于x长度

# The vector pioneers has already been created for you
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")

# Split names from birth year
split_math <- strsplit(pioneers, split = ":")
# Convert to lowercase strings: split_low
split_low <- lapply(split_math,tolower)
# Transform: use anonymous function inside lapply

names <- lapply(split_low, function(x){x[1]})

# Transform: use anonymous function inside lapply
years <- lapply(split_low, function(x){x[2]})
# Generic select function
select_el <- function(x, index) {
  x[index]
}
# Use lapply() twice on split_low: names and years
names <- lapply(split_low,select_el,index=1)
years <- lapply(split_low,select_el,index=2)

注意参数index的写法。第一个参数也就是x被直接传入。

lapply(split_low, function(x) {
  if (nchar(x[1]) > 5) {
    return(NULL)
  } else {
    return(x[2])
  }
})

根据条件来选择返回值

sapply

sapply(X,FUN,...)
X为向量或list
返回结果是向量

sapply相比lappy可以更清晰的显示结果,
但如果每列的结果长度不一致,就会选择和lapply一样的方式
identical(freezing_1,freezing_0)

or如果返回NULL,NULL也是list。和lapply相同。因为不是相同长度的vector。which is no longer a vector with the same length as the input

cat("The average temperature is", mean(x), "\n")
identical(x,y)

shell

less file1 file2
:n --remove to next file

相关文章

网友评论

      本文标题:[datacamp笔记]ggplot2

      本文链接:https://www.haomeiwen.com/subject/ygzzixtx.html