R作图 | datacamp-ggplot2笔记

  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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,349评论 0 33
  • 灰灰平时对自己的东西看的很紧,不太喜欢分享给其他小朋友。可是上次他送一盒棒棒糖给妞妞的时候说,妹妹,记得要分享哦。...
    漫步云端的T姐阅读 1,280评论 0 0
  • 总想写些东西来记录一下自己军恋的日常,害怕日子久了,自己会变得很懒,懒得去动,懒得去回忆过往。虽然往事不必再...
    胖团阅读 2,656评论 0 0
  • 手握一只玫瑰去荒岛旅游,我看见夕阳下的天空一无所有,只有晚霞在颤抖,仿佛在将流星守候。 可是就算海浪烘托了海鸥,天...
    李一十八阅读 2,890评论 0 3