forked from FanJingithub/MyCode_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temp.Rmd
84 lines (67 loc) · 2.27 KB
/
Temp.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
---
title: "Temp"
author: "Fan"
date: "2020年4月20日"
output:
html_document:
theme: cerulean
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,cache = T)
# knitr::opts_chunk$set(engine.path = list(
# python = 'C:/ProgramData/Anaconda3/python'
# ))
setwd("D:\\data_work\\R_sources\\my_code")
my_colors = c("royalblue","firebrick1","#FF9966","#66CC99","white","66CCCC","#3399CC")
library(reticulate)
# use_python("C:/ProgramData/Anaconda3/python")
# use_condaenv("base")
# use_condaenv(condaenv = "base", conda = "/opt/anaconda3/bin/conda")
```
## 图汇总{.tabset}
* AA
* BB
### 图1
```{r D3}
library(networkD3)
library(dplyr)
Sankey<-tibble(Source=c("A","A","B","C","C","C","u","u","u","u","u","u","v","v","v","v","w","w"),
Target=c("u","v","u","w","u","v","1","2","3","4","6","7","1","2","4","5","3","7"),
Value =c( 1, 3, 5, 2, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1))
Sankeylinks<-Sankey
Sankeynodes<-data.frame(name=unique(c(Sankeylinks$Source,Sankeylinks$Target)),stringsAsFactors=FALSE)
Sankeynodes$index<-0:(nrow(Sankeynodes) - 1)
Sankeylinks<-merge(Sankeylinks,Sankeynodes,by.x="Source",by.y="name")
Sankeylinks<-merge(Sankeylinks,Sankeynodes,by.x="Target",by.y="name")
Sankeydata<-Sankeylinks[,c(4,5,3)];names(Sankeydata)<-c("Source","Target","Value")
Sankeyname<-Sankeynodes[,1,drop=FALSE]
sankeyNetwork(Links=Sankeydata,Nodes=Sankeyname, Source ="Source",
Target = "Target", Value = "Value", NodeID = "name",
units = "TWh", fontSize = 12, nodeWidth = 30)
```
### 图2
```{r Sankey_ggalluvial}
#install.packages("ggalluvial")
library(ggalluvial)
library(reshape2)
titanic_wide <- data.frame(Titanic)
head(titanic_wide)
ggplot(data = titanic_wide,
aes(axis1 = Class, axis2 = Sex, axis3 = Age,
y = Freq)) +
scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.1, .05)) +
xlab("Demographic") +
# geom_alluvium(aes(fill = Survived)) +
geom_alluvium(aes(fill = Class)) +
geom_stratum() + geom_text(stat = "stratum", infer.label = TRUE) +
theme_minimal() +
ggtitle("passengers on the maiden voyage of the Titanic",
"stratified by demographics and survival")
```
## 第二
```{r}
library(tibble)
m<-tibble(x1=c(1,2,5,NA,6,8))
m
```