随笔分类 - R语言
摘要:ctrl + shift + 3
阅读全文
摘要:R语言中数据框除以向量,规则是数据框中的元素按照列依次递增,除数向量循环递增。 001、 a <- c(20, 10, 6, 8) b <- c(2, 4, 12, 8) c <- c(6, 8, 14, 14) dat <- data.frame(a, b, c) dat idx <- c(2,
阅读全文
摘要:001、 c1 <- c("a", "b", "a", "a", "b", "c") c2 <- c(3, 1, 4, 7, 8, 2) dat <- data.frame(c1, c2) ## 测试数据框 dat aggregate(dat$c2, by=list(dat$c1), sum) ##
阅读全文
摘要:001、 library(tidyverse) ## 加载包 a <- c(3, 5, 2, 1) b <- letters[1:4] c <- LETTERS[1:4] dat <- data.frame(a, b, c) dat column_to_rownames(dat, "a") ## 将
阅读全文
摘要:001、测试数据框 studentID <- seq(1, 20) gender <- rep(c("M", "M", "F", "F", "F"), 4) math <- rep(c(92, 86, 85, 74, 82), 4) english <- rep(c(76, 69, 82, 71,
阅读全文
摘要:001、 正常绘图 par(mar=c(6, 6, 6, 6), xpd = TRUE) ## 绘图区域外留白 plot(1:10) legend('right',inset= 0.3, pch=19,legend='xxx') ## 绘图,图例在图形内部 002、图例放在图形正右方 par(mar
阅读全文
摘要:001、 par(mfrow = c(1,2)) plot(1:10,tck = -0.01, main = "tck = -0.01") ## 保持不变,设置弹tck参数设置刻度线长度 plot(1:10,tck = -0.1, main = "tck = -0.1")
阅读全文
摘要:001、生成测试子图 library(ggplot2) library(dplyr) ## 依次生成测试子图p1、p2、p3、p4 p1 <- ggplot(mpg) + geom_point(aes(x = displ, y = hwy)) + ggtitle("P1") p2 <- ggplot
阅读全文
摘要:001、生成几个测试数据 library(ggplot2) library(dplyr) p1 <- ggplot(mpg) + geom_point(aes(x = displ, y = hwy)) + ggtitle("P1") ## 测试图p1 p2 <- ggplot(mpg) + geom
阅读全文
摘要:解决方法: 001、在安装目录中手动删除:rlang目录,例如此处windows默认的安装目录:C:\Users\ljx\AppData\Local\R\win-library\4.2;安装目录的获取方法,在终端执行: .libPaths() 002、然后再R中(不是Rstudio)重新安装: in
阅读全文
摘要:001、默认绘图 bp <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() ## 绘图 bp ## 输出图片 002、上部 bp + theme(legend.position="top") ## 放
阅读全文
摘要:001、正常绘图 library(ggplot2) bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() bp ## 显示绘图结果 002、修改图例标题的名称 bp + scale_fi
阅读全文
摘要:001、正常绘图,显示图例 library(ggplot2) bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() ## 绘图 bp ## 显示绘图 绘图结果如下: 002、隐藏图例的标
阅读全文
摘要:001、直接绘图效果: library(ggplot2) bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() ## 绘图 bp ## 显示绘图结果 绘图结果如下: 002、修改图例显示
阅读全文
摘要:001、 a、利用测试数据绘制箱线图 library(ggplot2) bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() ## 绘图 bp ## 显示绘图结果 绘图结果如下: b、移
阅读全文
摘要:001、使用绘制散点图进行测试。 a、直接绘制散点图 x <- 1:10 y <- seq(1, 1000, 100) dat <- data.frame(x, y) ## 生成测试数据 ggplot(dat, aes(x, y)) + geom_point() ## 直接绘制散点图 绘图结果如下:
阅读全文
摘要:001、 利用绘制散点图进行测试 a、直接绘制散点图 x <- 1:10 y <- seq(1, 1000, 100) dat <- data.frame(x, y) ## 生成测试数据 ggplot(dat, aes(x, y)) + geom_point() ## 直接绘制散点图 绘图结果如下:
阅读全文
摘要:001、theme_classic() 主题用来去除背景 a、使用默认的背景 type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums
阅读全文
摘要:001、生成测试数据以及测试 library(ggplot2) x <- paste0("s", 1:16) y <- 4:19 dat <- data.frame(x, y) ## 生成测试数据框 dim(dat) head(dat, 3) ggplot(dat,aes(x,y))+ geom_p
阅读全文