嘤
不想记笔记qaq
但是书只有纸质版的。
为了学shiny框架做毕设qaq
GITHUB地址:
github-rstudio/shiny-examples
SHINY官方教程:
shiny.rstudio-tutorial-lesson1
书的数据下载:
csv数据
画图不显示:控制台运行 dev.off() / dev.new()
#读取数据
table <- read.csv("/") #含有标题的
table <- read.csv("/", header=False) #不含有标题的
load("/") #R数据
#保存数据
write.csv(table, file="/") #保存至csv
save(table, file="/") #以R格式保存数据
vector <- as.vector()
as.matrix(frame)
as.data.frame(matrix)
library(reshape2)
table <- melt(table, id.vars, variable.name...)
rnorm(n, mean=0, sd=1)
sample(x, size, replace = FALSE, prob = NULL)
R中生成列联表的函数有base包中的table函数(一、二维),stats包中的ftable函数(多维),vcd包中的structable函数(多维)。
mytable <- table(data1_1$满意度)
prob.table(mytable)*100
mytable <- table(table1$性别, table1$满意度)
addmargins(mytable) #添加边际和
mytable <- ftable(data1, row.vars = c("", ""), col.vars = "")
structable(formula, data, direction)
nclass.Sturges(data1$销售额)
d <- table(cut(v, breaks=30*(17, 24), right = FALSE, dig.lab = 4))
grouped.data(v, breakers = 5, right = FALSE)
#breakers = c(500, 550, 600, 650, 700, 750)
layout(matrix(c(1,2,3,3),2,2,byrow=TRUE))
mai:以数值向量表示边界大小,顺序为"下、左、上、右",单位为英寸
functionname <- function(a1, a2, ...) {
}
1 高级绘图函数
函数 | 数据类型 | 图形 |
plot() | 数值 | 散点图 |
plot() | 因子 | 条形图 |
plot() | 一维列联表 | 条形图 |
plot() | 数值,数值 | 散点图 |
plot() | 因子,因子 | 脊形图 |
plot() | 二维列联表 | 马赛克图 |
plot() | 数值,因子 | 箱线图 |
plot() | 因子,数值 | 带状图 |
plot() | 数据框 | 散点图矩阵 |
par(mfrow = c(2, 2), mai = c(0.6, 0.6, 0.2, 0.1), cex = 0.6)
model <- lm(R~Python, data = data2_1)
plot(model)
2 其他高级绘图函数
graphics包中其他一些高级绘图函数
chull(x, y = NULL):计算位于指定点集凸包上的点的子集(即数据点集合中最外围,轮廓点集合)
#par(mai = c(0.7, 0.7, 0.4, 0.4), cex=0.8)
set.seed(1)
x <- rnorm(200)
y <- 1+2*x+rnorm(200)
d <- data.frame(x, y)
plot(x, y, xlab="x=自变量", ylab="y=因变量")
grid(col="grey60")
axis(side=4, col.ticks = "blue", lty=1)
polygon(d[chull(d), ], lty=6, lwd=1)
points(mean(x), meand(y), pch=19, cex=5, col=2)
abline(v=mean(x), h=mean(y), lty=2, col="gray30")
abline(lm(y~x), lwd=2, lty=6)
lines(lowess(y~x, f=1/6), col=4, lwd=2, lty=6)#拟合曲线
segments(-0.8, 0, -1.6, 3.3, lty=6, col="blue")#左示意曲线
arrows(0.45, -2.2, -0.8, -0.6, code = 2, angle = 25, length = 0.06, col=2)#带箭头的线段头
text(-2.2, 3.5, labels=expression("拟合的曲线"), adj=c(-0.1, 0.02), col=4)
rect(0.4, -1.6, 1.6, -3.5, col="pink")
mtext(expression(hat(y)==hat(beta)[0]+hat(beta)[1]*x), cex=1.2, side=1, line=-5.6, adj=0.72)
legend("topleft", legend=c("拟合的曲线", "拟合的曲线"), lty=c(1, 6), col=c(2, 4), cex=0.8, fill=c("red", "blue"), ncol=1, inset=0.02)
title("散点图以及拟合直线和曲线", cex.main=0.8, font.main=4)
box(col=4, lwd=2)
使用par设置图的排列方式,图形的边界大小,图中字体或符号的大小,主标题字体的大小等;如果希望所有的图形都使用相同的参数,可将参数放在par函数中统一设置,如果不同的图形有不同的参数设置,则需要将参数设置放在相应的绘图函数中。
使用colors()函数可以查看R全部567种颜色的名称列表;在graphics中
使用多种颜色:col=colors(),col=colors(256),col=rainbow(n, start=0.4, end=0.5) #渐变
相当于在每一个小图里面,继续布局。
par(mfrow=c(1,2))
layout(matrix(c(1, 2, 3, 3), nrow=2, ncol=2, byrow=TRUE), heights=c(2, 1))
layout.show(3)#预览
matrix(c(1, 2, 3, 4, 5, 5, 6, 7, 8), 3, 3, byrow=TRUE), widths=c(2:1), heights=c(1:1);宽是2:1
dev.new();打开多个绘图窗口,以比较不同的图像,最多63个;
data1_1 <- read.csv("D:/soft/other/R/data/mydata/chap01/data1_1.csv")
attach(data1_1)
table2 <- table(网购次数)
bar <- barplot(table2, col=grey.colors(3), ylim = c(0, 1.1*max(table2)), xlab="网购次数", ylab = "人数", cex.names = 0.8, main = "abc")
text(bar, table2, labels = table2, pos = 3) #频数标签
lines(bar, table2, type = "o", col = "red", pch=19) #频数折线
points(bar, table2, type = "h", col = "red", lwd = 1, lty=6) #垂线
或者使用sjPlot;会自动加上频数标签和各类别频数百分比等