R语言练习代码

Wesley13
• 阅读 469
x <- c(1:10)

x <- c(v1 = 1:4,v2 = 10:14)

install.packages()

install.packages("xlsx")

install.packages("rJava")

install.packages("xlsxjars")

install.packages("vcd")

.libPaths()

install.packages(c("AER","ca"))

library()

library(xlsx)

library(rJava)

print()

require(vcd)

require(xlsx)

installed.packages()

installed.packages()[,1]

Rpack <- installed.packages()[,1]
save(Rpack,file = "Rpack.Rdata")
for (i in Rpack) install.packages(i)

help.xlsx()
help.start()

x <- read.csv("碧彩测试数据1.07",header = TRUE)

y <- readClipboard()

read.table("clipboard",sep = ",",header = TRUE)

read.table("clipboard",sep = "\t",header = TRUE)

install.packages("arules")

data(Groceries)

Groceries

a <- c(100,rep(10,4))
mean(a)
b <- c(1000,rep(50,9),rep(5,5))
sum(b)

A <- c(1,2,5,8,9)
jicha <- max(A) - min(A)
jicha

fangcha <- function(x){return(sum((x-mean(x))^2)/length(x))}
fangcha
fangcha(A)

install.packages("moments")
library("moments")

data1

x <- read.csv("C:\\Users\\QDM\\Desktop\\销售业绩.csv",header = T)
x

y <- read.table("C:\\Users\\QDM\\Desktop\\销售业绩.txt",header = T)
y

z <- read.table("clipboard",header = T,sep = "\t")
z

1+3

x <- c(1,3,8,4,7,5,6,1)
png(file = "barchart.png")
barplot(x)
dev.off()

x <- read.table("clipboard",header = T,sep = "\t")
x

x <- read.table("clipboard",header = T)
x

barplot(apply(x,1,mean))

barplot(apply(x,2,median))

pie(apply(x,2,mean))

barplot(height = 1:10)

data <- cbind(a = 1:4, b = 1:4)
barplot(data)

data <- cbind(a = 1:4, b = 1:4)
barplot(data, beside = T)

barplot(1:5, width = 5:1)

barplot(1:3, space = c(0.1, 0.2, 0.3))

par(mfrow = c(1, 2))
height <- c(1:3)
names(height) <- c("A", "B", "C")
barplot(height)

data <- cbind("A" = 1:4, "B" = 1:4)
barplot(data)

par(mfrow = c(1, 2))
height <- c(1:3)
names(height) <- c("A", "B", "C")
barplot(height, names.arg = c("A1", "A2", "A3"))

data <- cbind("A" = 1:4, "B" = 1:4)
barplot(data, names.arg = c("D", "E"))

data <- cbind(a = 1:4, b = 1:4)
rownames(data) <- c("A", "B", "C", "D")
barplot(data, legend.text = TRUE)

data <- cbind(a = 1:4, b = 1:4)
rownames(data) <- c("A", "B", "C", "D")
barplot(data, legend.text = c("E", "F", "G", "H"))

par(mfrow = c(1, 2))
data <- cbind(a = 1:4, b = 1:4)
barplot(data, main = "beside = F")
barplot(data, beside = T, main = "beside = T")

par(mfrow = c(1, 2))
barplot(1:4, main = "horiz = F")
barplot(1:4, main = "horiz = T", horiz =T)

barplot(1:4, col = "green")

barplot(1:4, col = rainbow(4))

barplot(1:4, col = rainbow(4), border = NA, main = "border = NA")

par(mfrow = c(1, 3))
barplot(rep(1,4), density = 1, main = "density = 1")
barplot(rep(1,4), density = 2, main = "density = 2")
barplot(rep(1,4), density = 3, main = "density = 3")

par(mfrow = c(1, 3))
barplot(rep(1,4), density = 2, angle = 45,  main  = "angle = 45")
barplot(rep(1,4), density = 2, angle = 90,  main  = "angle = 90")
barplot(rep(1,4), density = 2, angle = 180, main  = "angle = 180") 

par(mfrow = c(1, 2))
data <- cbind(a = 1:4, b = 1:4)
barplot(data, axes = F, main = "axes = F")
barplot(data, axes = T, main = "axes = T")

par(mfrow = c(1, 2))
data <- cbind(a = 1:4, b = 1:4)
barplot(data, axisnames = F, main = "axisnames = F")
barplot(data, axisnames = T, main = "axisnames = T")

par(mfrow = c(1, 3))
data <- cbind(a = 1:4, b = 1:4)
barplot(data, offset = 0, main = "offset = 0")
barplot(data, offset = 1, main = "offset = 1")
barplot(data, offset = 2, main = "offset = 2")

barplot(1:4, col = rainbow(4), border = NA, main = "border = NA")

barplot(height = 1:7)
barplot(height = 1:7,beside = T, main = "2020年5月份业绩")
barplot(height = 1:7,beside = T, main = "2020年5月份业绩",col = rainbow(7))
barplot(height = 1:7,beside = T, main = "2020年5月份业绩",col = rainbow(7),border = NA)
barplot(height = 1:7,beside = T, main = "2020年5月份业绩",col = rainbow(7),border = NA)

QDM <- read.table("clipboard",header = T)
QDM

QDM <- c(100,200,300,400,500,600)
depatment <- c("电商","水产","水果","蔬菜","肉类","综合")
barplot(QDM,names.arg = depatment)
barplot(QDM,names.arg = depatment,col = "green")
barplot(QDM,names.arg = depatment,col = "red")
barplot(QDM,names.arg = depatment,col = "blue")
barplot(QDM,names.arg = depatment,col = "black")
barplot(QDM,names.arg = depatment,col = "pink")
barplot(QDM,names.arg = depatment,beside = T, main = "2020年5月份各部门业绩",col = rainbow(7))
barplot(QDM,names.arg = depatment,beside = T, main = "2020年5月份各部门业绩",col = rainbow(7),border = NA)

data <- c(60,70,80,90,100)
barplot(data)


data1 <- c(60,70,80,90,100)
bumen <- c("水产","水果","蔬菜","猪肉","综合")
barplot(data1,names.arg = bumen)

barplot(y,names.arg = x,border = NA,xlab = "部门",ylab = "销售额",main = "2020年5月份各部门总销售额",col = rainbow(4))
barplot(y,names.arg = x,border = "blue",xlab = "部门",ylab = "销售额",main = "2020年5月份各部门总销售额",col = rainbow(4))

x <- c(1020,1640,1340,1610,1942)
x

y <- c("水产","水果","蔬菜","猪肉","综合")
y

plot(x,names.arg = y)
barplot(x,names.arg = y,border = NA)
barplot(x,names.arg = y,border = NA,col = rainbow(5))
plot(x,names.arg = y,type = "b")
plot(x,names.arg = y,type = "b",col = "red")
plot(x,names.arg = y,type = "b",col = "green")
plot(x,names.arg = y,type = "b",col = "DarkTurquoise")


n <- 6
pie(x,n)
pie(x,y,border = NA)
pie(x,y,border = NA,col = rainbow(7))
pie(x,y,col = rainbow(7))
piecolors <- rainbow(n)
piecolors
pie(x,y,border = "gray",col = rainbow(7))
a <- c(100,150,130,168,111)
pie(a,y)


x <- c(1,2,3,4,5,6,7)
y <- c("周一","周二","周三","周四","周五","周六","周日")
barplot(x,names.arg = y)
barplot(x,names.arg = y,col = rainbow(7))
barplot(x,names.arg = y,col = rainbow(7),border = NA)


pie(x,y)
pie(x,y,col = brewer.pal(7,"Set1"))


write.table(x,file = "result.txt", sep = "\t", quote = FALSE)

x <- read.table("clipboard",header = T,sep = "\t")
x

y <- read.csv("出入库.csv",header = TRUE)
y


ls("package:graphics")

x <- c(1:4)
y <- c(4:6)
arrows(x,y)


stars(x)
box(x)
demo(graphics)

help(package = "graphics")
example("heatmap")
example("barplot")
example("pie")


pie(rep(1, n), labels = "", col = rainbow(n), border = NA,
    pie+     main = "pie(*, labels=\"\", col=rainbow(n), border=NA,..")

pie(c(Sky = 78, "Sunny side of pyramid" = 17, "Shady side of pyramid" = 5),
         pie+     init.angle = 315, col = c("deepskyblue", "yellow", "yellow3"), border = FALSE)

pie> pie(c(Sky = 78, "Sunny side of pyramid" = 17, "Shady side of pyramid" = 5),
         pie+     init.angle = 315, col = c("deepskyblue", "yellow", "yellow3"), border = FALSE)

example("boxplot")

women
plot(women)
barplot(women$height)
barplot(women$height,women$weight)
plot("fit")
methods(plot)
methods(print)
par()
barplot(height = 1:3,col = c("red","green","blue"))
barplot(height = 1:3,col = c("red","green","blue"),border = NA)



plot(women$height,women$weight)

fit <- lm(height ~ weight,data = women)
fit
summary(fit)

install.packages("ggplot2")
?ggplot2
?barplot
install.packages("caret")
library("caret")
library(caret)
data()
cars
women
GermanCredit
GermanCredit
GermanCredit

?pie
x <- c("蔬菜","水果","水产","猪肉","综合")
x
y <- c(5,4,3,2,1)
y
pie(y,labels = names(x))
pie(y,labels = names(x),col = rainbow(5))
pie(y,labels = names(x),col = rainbow(5),border = NA)
pie(y,labels = names(x),col = rainbow(5),border = NA,edges = 200)
pie(y,labels = names(x),col = rainbow(5),border = NA,edges = 10)
pie(y,labels = names(x),col = rainbow(5),border = NA,radius = 1)
pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1)
pie(y,labels = names(x),col = rainbow(5),border = "white",radius = 1)
pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1,main = "滕玉龙")
pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1, angle = 45)
pie(y,labels = names(x),col = rainbow(5),border = NULL,radius = 1, clockwise = TRUE)
pie(y,labels = names(x),col = rainbow(5),border = NA,radius = 1, clockwise = TRUE)
pie(y,labels = names(x),col = rainbow(5),border = NA,radius = 1, clockwise = TRUE,main = "滕玉龙")
example(pie)
pie(y,names(x) <- c("蔬菜","水果","水产","猪肉","综合") ,col = rainbow(5),border = NA,radius = 1, clockwise = TRUE,main = "7月份各部门销售占比")

library("ggplot2")
qplot(Wind,Temp,data = airquality)
qplot(Wind,Temp,data = airquality,color = Month)
qplot(Wind,Temp,data = airquality,color = I("blue"))
qplot(Wind,Temp,data = airquality,shape = Month)
qplot(Wind,Temp,data = airquality,size = Month)
qplot(Wind,Temp,data = airquality,size = I(15))
qplot(Wind,Temp,data = airquality,size = I(10),color = I("red"),xlab = "风速",ylab = "气温",main = "气温与风速的关系")
qplot(Wind,Temp,data = airquality,size = I(10),color = I("red"),xlab = "风速",ylab = "气温",main = "气温与风速的关系",fill = Month)
qplot(Wind,data = airquality,fill = Month)
qplot(Wind,data = airquality,geom = "density")
qplot(Wind,data = airquality,geom = "density",color = Month)
qplot(Wind,data = airquality,geom = "dotplot",color = I("green"))


df = data.frame(x=1:10, 
                y=seq(1, 20, 2), 
                z=seq(1000, 100, -100), 
                c=paste("color", seq(1, 10, 1)))
df


install.packages("ggplot2") # 安装包
install.packages("RColorBrewer")
library(ggplot2) # 加载包
library(RColorBrewer)

ggplot(df, aes(x=x, y=y)) +
  geom_bar(stat="identity")


ggplot(df, aes(x=x, y=y, fill=c)) + 
  geom_bar(stat="identity") +
  labs(x="x axis", y="y axis", fill="legend")

ggplot(df, aes(x=x, y=y, fill=x)) +
  geom_bar(stat="identity") +
  labs(x="x axis", y="y axis", fill="legend")


ggplot(df, aes(x=x, y=y, fill=z)) +
  geom_bar(stat="identity") +
  labs(x="x axis", y="y axis", fill="legend")


colors <- colorRampPalette(c("red", "orange"))(10)
colors
# [1] "#FF0000" "#FF1200" "#FF2400" "#FF3700" "#FF4900" "#FF5B00" "#FF6E00" "#FF8000" "#FF9200" "#FFA500"

ggplot(df, aes(x=x, y=y)) +
  geom_bar(stat="identity", fill=colors[rank(10:1)]) +
  labs(x="x axis", y="y axis", fill="legend")


ggplot(df, aes(x=x, y=y)) +
  geom_bar(stat="identity", fill=brewer.pal(10, "Set3")) +
  labs(x="x axis", y="y axis", fill="legend")

?data.frame

?barplot

goods <- c("豆芽","茄子","淮山","番茄","芋头","土豆","萝卜","马蹄","莴笋","红薯")
goods

amount

barplot(height = 1:10,col = rainbow(10),border = NA,names.arg = goods,main = "各种农产品的产量(单位:万吨)\n\nMade By 滕玉龙",sub = "左手Python,右手R语言",ylab = "产量(万吨)",xlab = "农产品名称")

amount <- c(100, 130, 169, 220, 286, 372, 484, 629, 818, 1063)
amount

barplot(height = amount,col = rainbow(10),border = NA,names.arg = goods,main = "各种农产品的产量(单位:万吨)\n\nMade By 滕玉龙",sub = "左手Python,右手R语言",ylab = "产量(万吨)",xlab = "农产品名称")



grade <- c("大一", "大二", "大三", "大四")

amount <- c(15, 38, 123, 35)

barplot(height = amount,col = rainbow(4),border = NA,names.arg = grade,main = "各年级发生的频次",sub = "哈佛在等我呢~",ylab = "频次",xlab = "年级")
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这