-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-ggplot.Rmd
109 lines (86 loc) · 2.8 KB
/
12-ggplot.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# ggplot2
```{r setup}
require(tidyverse)
require(readxl)
require(ggthemr)
require(TeachingDemos)
ggthemr('light', spacing = 0.5, type = 'inner')
# 读入excel文件
exams03 <- read_excel("data/exams_results.xlsx")
```
```{r}
plot_area <- function(min,max){
function(x){
y <- dnorm(x)
y[x<min | x>max] <-NA
return(y)
}
}
krup.dnorm_area <- function(min = -Inf,max = Inf,mean = 0, sd = 1){
function(x){
y <- dnorm(x , mean, sd)
y[x<min | x>max] <- NA
return(y)
}
}
ggplot(NULL,aes(samp)) + # geom_histogram() +
stat_function(fun=plot_area(-Inf, -1.96), geom="area", fill="red", alpha=0.2) +
stat_function(fun=plot_area(1.96, Inf), geom="area", fill="red", alpha=0.2) +
stat_function(fun=plot_area(-1, 1), geom="area", fill="#ffccdd", alpha=0.2) +
stat_function(fun=dnorm) +
geom_text(aes(y=0.2, x=0), label="70",parse = TRUE) +
geom_text(aes(y=0.025, x=-2),label="2.5%") +
geom_text(aes(y=0.025, x=2),label="2.5%")
myfun <- function(min = -3, max = 3){
function(x){
y <- dnorm(x)
y[x<min | x>max] <- NA
return(y)
}
}
dnorm2 <- function(min,max,mean = 0, sd = 1){
function(x){
y <- dnorm(x,mean,sd)
y[x< min | x>max] <- NA
return(y)
}
}
samp.left <- z.test(samp,5,1)$conf.int[1]
samp.right <- z.test(samp,5,1)$conf.int[2]
ggplot(NULL, aes(x = samp)) +
stat_function(fun=dnorm2(-Inf, samp.left, 5, 1), geom="area", fill="red", alpha=0.2) +
stat_function(fun=dnorm2(samp.right, Inf, 5, 1), geom="area", fill="red", alpha=0.2) +
stat_function(fun = dnorm,args = list(mean = 5, sd = 1),aes(x = -4:8, color = "标准")) +
geom_vline(xintercept = z.test(samp,5, 1)$statistic) +
scale_colour_manual("Lgend title", values = c("red", "blue"))
```
```{r test}
ztplot <- function(sample, mu, sd = 1, alter = "both"){
sample <- samp
mu <- 5
sd <- 1
mean = sample
dnorm2 <- function(min,max,mean = 0, sd = 1){
function(x){
y <- dnorm(x,mean,sd)
y[x< min | x>max] <- NA
return(y)
}
}
samp.left <- z.test(sample,mean,sd)$conf.int[1]
samp.right <- z.test(sample,mean,sd)$conf.int[2]
p <- ggplot(NULL, aes(x = sample))
p <- p + stat_function(fun = dnorm2(-Inf, samp.left, mean, sd), geom="area", fill="red", alpha=0.2)
p <- p + stat_function(fun = dnorm2(samp.right, Inf, mean, sd), geom="area", fill="red", alpha=0.2)
p <- p + stat_function(fun = dnorm,args = list(mean = mean, sd = sd),aes(x = c(samp.left:samp.right), color = "标准"))
#p <- p + geom_vline(xintercept = z.test(sample,mean, sd)$statistic)
p <- p + geom_vline(xintercept = mu)
p <- p + scale_colour_manual("Lgend title", values = c("red", "blue"))
print(p)
}
x<-c(175,176,173,175,174,173,173,176,173,179)
#z.test(x,10,1.5,0.05)
test <- rnorm(n = 100,sd = 1,mean = 0)
ztplot(samp, 5, sd = 1)
z.test(test,0,1)
```