R语言练习代码

1x <- c(1:10) 2 3x <- c(v1 = 1:4,v2 = 10:14) 4 5install.packages() 6 7install.packages("xlsx") 8 9install.packages("rJava") 10 11install.packages("xlsxjars") 12 13install.packages("vcd") 14 15.libPaths() 16 17install.packages(c("AER","ca")) 18 19library() 20 21library(xlsx) 22 23library(rJava) 24 25print() 26 27require(vcd) 28 29require(xlsx) 30 31installed.packages() 32 33installed.packages()[,1] 34 35Rpack <- installed.packages()[,1] 36save(Rpack,file = "Rpack.Rdata") 37for (i in Rpack) install.packages(i) 38 39help.xlsx() 40help.start() 41 42x <- read.csv("碧彩测试数据1.07",header = TRUE) 43 44y <- readClipboard() 45 46read.table("clipboard",sep = ",",header = TRUE) 47 48read.table("clipboard",sep = "\t",header = TRUE) 49 50install.packages("arules") 51 52data(Groceries) 53 54Groceries 55 56a <- c(100,rep(10,4)) 57mean(a) 58b <- c(1000,rep(50,9),rep(5,5)) 59sum(b) 60 61A <- c(1,2,5,8,9) 62jicha <- max(A) - min(A) 63jicha 64 65fangcha <- function(x){return(sum((x-mean(x))^2)/length(x))} 66fangcha 67fangcha(A) 68 69install.packages("moments") 70library("moments") 71 72data1 73 74x <- read.csv("C:\\Users\\QDM\\Desktop\\销售业绩.csv",header = T) 75x 76 77y <- read.table("C:\\Users\\QDM\\Desktop\\销售业绩.txt",header = T) 78y 79 80z <- read.table("clipboard",header = T,sep = "\t") 81z 82 831+3 84 85x <- c(1,3,8,4,7,5,6,1) 86png(file = "barchart.png") 87barplot(x) 88dev.off() 89 90x <- read.table("clipboard",header = T,sep = "\t") 91x 92 93x <- read.table("clipboard",header = T) 94x 95 96barplot(apply(x,1,mean)) 97 98barplot(apply(x,2,median)) 99 100pie(apply(x,2,mean)) 101 102barplot(height = 1:10) 103 104data <- cbind(a = 1:4, b = 1:4) 105barplot(data) 106 107data <- cbind(a = 1:4, b = 1:4) 108barplot(data, beside = T) 109 110barplot(1:5, width = 5:1) 111 112barplot(1:3, space = c(0.1, 0.2, 0.3)) 113 114par(mfrow = c(1, 2)) 115height <- c(1:3) 116names(height) <- c("A", "B", "C") 117barplot(height) 118 119data <- cbind("A" = 1:4, "B" = 1:4) 120barplot(data) 121 122par(mfrow = c(1, 2)) 123height <- c(1:3) 124names(height) <- c("A", "B", "C") 125barplot(height, names.arg = c("A1", "A2", "A3")) 126 127data <- cbind("A" = 1:4, "B" = 1:4) 128barplot(data, names.arg = c("D", "E")) 129 130data <- cbind(a = 1:4, b = 1:4) 131rownames(data) <- c("A", "B", "C", "D") 132barplot(data, legend.text = TRUE) 133 134data <- cbind(a = 1:4, b = 1:4) 135rownames(data) <- c("A", "B", "C", "D") 136barplot(data, legend.text = c("E", "F", "G", "H")) 137 138par(mfrow = c(1, 2)) 139data <- cbind(a = 1:4, b = 1:4) 140barplot(data, main = "beside = F") 141barplot(data, beside = T, main = "beside = T") 142 143par(mfrow = c(1, 2)) 144barplot(1:4, main = "horiz = F") 145barplot(1:4, main = "horiz = T", horiz =T) 146 147barplot(1:4, col = "green") 148 149barplot(1:4, col = rainbow(4)) 150 151barplot(1:4, col = rainbow(4), border = NA, main = "border = NA") 152 153par(mfrow = c(1, 3)) 154barplot(rep(1,4), density = 1, main = "density = 1") 155barplot(rep(1,4), density = 2, main = "density = 2") 156barplot(rep(1,4), density = 3, main = "density = 3") 157 158par(mfrow = c(1, 3)) 159barplot(rep(1,4), density = 2, angle = 45, main = "angle = 45") 160barplot(rep(1,4), density = 2, angle = 90, main = "angle = 90") 161barplot(rep(1,4), density = 2, angle = 180, main = "angle = 180") 162 163par(mfrow = c(1, 2)) 164data <- cbind(a = 1:4, b = 1:4) 165barplot(data, axes = F, main = "axes = F") 166barplot(data, axes = T, main = "axes = T") 167 168par(mfrow = c(1, 2)) 169data <- cbind(a = 1:4, b = 1:4) 170barplot(data, axisnames = F, main = "axisnames = F") 171barplot(data, axisnames = T, main = "axisnames = T") 172 173par(mfrow = c(1, 3)) 174data <- cbind(a = 1:4, b = 1:4) 175barplot(data, offset = 0, main = "offset = 0") 176barplot(data, offset = 1, main = "offset = 1") 177barplot(data, offset = 2, main = "offset = 2") 178 179barplot(1:4, col = rainbow(4), border = NA, main = "border = NA") 180 181barplot(height = 1:7) 182barplot(height = 1:7,beside = T, main = "2020年5月份业绩") 183barplot(height = 1:7,beside = T, main = "2020年5月份业绩",col = rainbow(7)) 184barplot(height = 1:7,beside = T, main = "2020年5月份业绩",col = rainbow(7),border = NA) 185barplot(height = 1:7,beside = T, main = "2020年5月份业绩",col = rainbow(7),border = NA) 186 187QDM <- read.table("clipboard",header = T) 188QDM 189 190QDM <- c(100,200,300,400,500,600) 191depatment <- c("电商","水产","水果","蔬菜","肉类","综合") 192barplot(QDM,names.arg = depatment) 193barplot(QDM,names.arg = depatment,col = "green") 194barplot(QDM,names.arg = depatment,col = "red") 195barplot(QDM,names.arg = depatment,col = "blue") 196barplot(QDM,names.arg = depatment,col = "black") 197barplot(QDM,names.arg = depatment,col = "pink") 198barplot(QDM,names.arg = depatment,beside = T, main = "2020年5月份各部门业绩",col = rainbow(7)) 199barplot(QDM,names.arg = depatment,beside = T, main = "2020年5月份各部门业绩",col = rainbow(7),border = NA) 200 201data <- c(60,70,80,90,100) 202barplot(data) 203 204 205data1 <- c(60,70,80,90,100) 206bumen <- c("水产","水果","蔬菜","猪肉","综合") 207barplot(data1,names.arg = bumen) 208 209barplot(y,names.arg = x,border = NA,xlab = "部门",ylab = "销售额",main = "2020年5月份各部门总销售额",col = rainbow(4)) 210barplot(y,names.arg = x,border = "blue",xlab = "部门",ylab = "销售额",main = "2020年5月份各部门总销售额",col = rainbow(4)) 211 212x <- c(1020,1640,1340,1610,1942) 213x 214 215y <- c("水产","水果","蔬菜","猪肉","综合") 216y 217 218plot(x,names.arg = y) 219barplot(x,names.arg = y,border = NA) 220barplot(x,names.arg = y,border = NA,col = rainbow(5)) 221plot(x,names.arg = y,type = "b") 222plot(x,names.arg = y,type = "b",col = "red") 223plot(x,names.arg = y,type = "b",col = "green") 224plot(x,names.arg = y,type = "b",col = "DarkTurquoise") 225 226 227n <- 6 228pie(x,n) 229pie(x,y,border = NA) 230pie(x,y,border = NA,col = rainbow(7)) 231pie(x,y,col = rainbow(7)) 232piecolors <- rainbow(n) 233piecolors 234pie(x,y,border = "gray",col = rainbow(7)) 235a <- c(100,150,130,168,111) 236pie(a,y) 237 238 239x <- c(1,2,3,4,5,6,7) 240y <- c("周一","周二","周三","周四","周五","周六","周日") 241barplot(x,names.arg = y) 242barplot(x,names.arg = y,col = rainbow(7)) 243barplot(x,names.arg = y,col = rainbow(7),border = NA) 244 245 246pie(x,y) 247pie(x,y,col = brewer.pal(7,"Set1")) 248 249 250write.table(x,file = "result.txt", sep = "\t", quote = FALSE) 251 252x <- read.table("clipboard",header = T,sep = "\t") 253x 254 255y <- read.csv("出入库.csv",header = TRUE) 256y 257 258 259ls("package:graphics") 260 261x <- c(1:4) 262y <- c(4:6) 263arrows(x,y) 264 265 266stars(x) 267box(x) 268demo(graphics) 269 270help(package = "graphics") 271example("heatmap") 272example("barplot") 273example("pie") 274 275 276pie(rep(1, n), labels = "", col = rainbow(n), border = NA, 277 pie+ main = "pie(*, labels=\"\", col=rainbow(n), border=NA,..") 278 279pie(c(Sky = 78, "Sunny side of pyramid" = 17, "Shady side of pyramid" = 5), 280 pie+ init.angle = 315, col = c("deepskyblue", "yellow", "yellow3"), border = FALSE) 281 282pie> pie(c(Sky = 78, "Sunny side of pyramid" = 17, "Shady side of pyramid" = 5), 283 pie+ init.angle = 315, col = c("deepskyblue", "yellow", "yellow3"), border = FALSE) 284 285example("boxplot") 286 287women 288plot(women) 289barplot(women$height) 290barplot(women$height,women$weight) 291plot("fit") 292methods(plot) 293methods(print) 294par() 295barplot(height = 1:3,col = c("red","green","blue")) 296barplot(height = 1:3,col = c("red","green","blue"),border = NA) 297 298 299 300plot(women$height,women$weight) 301 302fit <- lm(height ~ weight,data = women) 303fit 304summary(fit) 305 306install.packages("ggplot2") 307?ggplot2 308?barplot 309install.packages("caret") 310library("caret") 311library(caret) 312data() 313cars 314women 315GermanCredit 316GermanCredit 317GermanCredit 318 319?pie 320x <- c("蔬菜","水果","水产","猪肉","综合") 321x 322y <- c(5,4,3,2,1) 323y 324pie(y,labels = names(x)) 325pie(y,labels = names(x),col = rainbow(5)) 326pie(y,labels = names(x),col = rainbow(5),border = NA) 327pie(y,labels = names(x),col = rainbow(5),border = NA,edges = 200) 328pie(y,labels = names(x),col = rainbow(5),border = NA,edges = 10) 329pie(y,labels = names(x),col = rainbow(5),border = NA,radius = 1) 330pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1) 331pie(y,labels = names(x),col = rainbow(5),border = "white",radius = 1) 332pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1,main = "滕玉龙") 333pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1, angle = 45) 334pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1, clockwise = TRUE) 335pie(y,labels = names(x),col = rainbow(5),border = NA,radius = 1, clockwise = TRUE) 336pie(y,labels = names(x),col = rainbow(5),border = NA,radius = 1, clockwise = TRUE,main = "滕玉龙") 337example(pie) 338pie(y,names(x) <- c("蔬菜","水果","水产","猪肉","综合") ,col = rainbow(5),border = NA,radius = 1, clockwise = TRUE,main = "7月份各部门销售占比") 339 340library("ggplot2") 341qplot(Wind,Temp,data = airquality) 342qplot(Wind,Temp,data = airquality,color = Month) 343qplot(Wind,Temp,data = airquality,color = I("blue")) 344qplot(Wind,Temp,data = airquality,shape = Month) 345qplot(Wind,Temp,data = airquality,size = Month) 346qplot(Wind,Temp,data = airquality,size = I(15)) 347qplot(Wind,Temp,data = airquality,size = I(10),color = I("red"),xlab = "风速",ylab = "气温",main = "气温与风速的关系") 348qplot(Wind,Temp,data = airquality,size = I(10),color = I("red"),xlab = "风速",ylab = "气温",main = "气温与风速的关系",fill = Month) 349qplot(Wind,data = airquality,fill = Month) 350qplot(Wind,data = airquality,geom = "density") 351qplot(Wind,data = airquality,geom = "density",color = Month) 352qplot(Wind,data = airquality,geom = "dotplot",color = I("green")) 353 354 355df = data.frame(x=1:10, 356 y=seq(1, 20, 2), 357 z=seq(1000, 100, -100), 358 c=paste("color", seq(1, 10, 1))) 359df 360 361 362install.packages("ggplot2") # 安装包 363install.packages("RColorBrewer") 364library(ggplot2) # 加载包 365library(RColorBrewer) 366 367ggplot(df, aes(x=x, y=y)) + 368 geom_bar(stat="identity") 369 370 371ggplot(df, aes(x=x, y=y, fill=c)) + 372 geom_bar(stat="identity") + 373 labs(x="x axis", y="y axis", fill="legend") 374 375ggplot(df, aes(x=x, y=y, fill=x)) + 376 geom_bar(stat="identity") + 377 labs(x="x axis", y="y axis", fill="legend") 378 379 380ggplot(df, aes(x=x, y=y, fill=z)) + 381 geom_bar(stat="identity") + 382 labs(x="x axis", y="y axis", fill="legend") 383 384 385colors <- colorRampPalette(c("red", "orange"))(10) 386colors 387# [1] "#FF0000" "#FF1200" "#FF2400" "#FF3700" "#FF4900" "#FF5B00" "#FF6E00" "#FF8000" "#FF9200" "#FFA500" 388 389ggplot(df, aes(x=x, y=y)) + 390 geom_bar(stat="identity", fill=colors[rank(10:1)]) + 391 labs(x="x axis", y="y axis", fill="legend") 392 393 394ggplot(df, aes(x=x, y=y)) + 395 geom_bar(stat="identity", fill=brewer.pal(10, "Set3")) + 396 labs(x="x axis", y="y axis", fill="legend") 397 398?data.frame 399 400?barplot 401 402goods <- c("豆芽","茄子","淮山","番茄","芋头","土豆","萝卜","马蹄","莴笋","红薯") 403goods 404 405amount 406 407barplot(height = 1:10,col = rainbow(10),border = NA,names.arg = goods,main = "各种农产品的产量(单位:万吨)\n\nMade By 滕玉龙",sub = "左手Python,右手R语言",ylab = "产量(万吨)",xlab = "农产品名称") 408 409amount <- c(100, 130, 169, 220, 286, 372, 484, 629, 818, 1063) 410amount 411 412barplot(height = amount,col = rainbow(10),border = NA,names.arg = goods,main = "各种农产品的产量(单位:万吨)\n\nMade By 滕玉龙",sub = "左手Python,右手R语言",ylab = "产量(万吨)",xlab = "农产品名称") 413 414 415 416grade <- c("大一", "大二", "大三", "大四") 417 418amount <- c(15, 38, 123, 35) 419 420barplot(height = amount,col = rainbow(4),border = NA,names.arg = grade,main = "各年级发生的频次",sub = "哈佛在等我呢~",ylab = "频次",xlab = "年级")
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

R语言练习代码 - HelloWorld