ggplot 收支桑基图 (ggsankey)
ggplot sankey diagram of income to expenses (ggsankey)
我正在尝试制作收支桑基图,最好使用 ggsankey
或其他 ggplot 扩展,因为我需要最终图作为图像 (png)。这是我的数据:
data <- tibble::tribble(
~Name, ~Annual.Amount, ~Category,
"Moira's Earnings", 50000L, "Income",
"Johnny's Earnings", 300000L, "Income",
"Living Expenses", 140000L, "Expenses",
"Spent Savings", 25238L, "Expenses",
"Liabilities", 44280L, "Expenses",
"Planned Savings", 23000L, "Expenses",
"Taxes", 98482L, "Expenses",
"Insurance", 13000L, "Expenses"
)
我正在尝试创建类似于此图表的内容,但只有前 2 个流量,所有收入都从支出流向一列:
library(tidyverse)
library(networkD3)
data <- tibble::tribble(
~Name, ~Annual.Amount, ~Category,
"Moira's Earnings", 50000L, "Income",
"Johnny's Earnings", 300000L, "Income",
"Living Expenses", 140000L, "Expenses",
"Spent Savings", 25238L, "Expenses",
"Liabilities", 44280L, "Expenses",
"Planned Savings", 23000L, "Expenses",
"Taxes", 98482L, "Expenses",
"Insurance", 13000L, "Expenses"
)
Nodes = tibble(
Name = c(data$Name, "Budget") %>% unique()
) %>% as.data.frame()
df = data %>% filter(Category=="Income") %>%
select(-Category) %>%
rename(Source = Name) %>%
mutate(Target = "Budget") %>%
bind_rows(
data %>%
filter(Category=="Expenses") %>%
select(-Category) %>%
rename(Target = Name) %>%
mutate(Source = "Budget")
) %>% mutate(
IDSource = match(Source, Nodes$Name)-1,
IDTarget = match(Target, Nodes$Name)-1
) %>% as.data.frame()
sankeyNetwork(Links = df, Nodes = Nodes,
Source = "IDSource", Target = "IDTarget",
Value = "Annual.Amount", NodeID = "Name",
sinksRight=FALSE, fontSize = 16)
小更新
library(webshot)
webshot::install_phantomjs()
sankeyNetwork(Links = df, Nodes = Nodes,
Source = "IDSource", Target = "IDTarget",
Value = "Annual.Amount", NodeID = "Name",
sinksRight=FALSE, fontSize = 16) %>%
saveNetwork("sn.html")
webshot("sn.html", "sankeyNetwork.png")
我正在尝试制作收支桑基图,最好使用 ggsankey
或其他 ggplot 扩展,因为我需要最终图作为图像 (png)。这是我的数据:
data <- tibble::tribble(
~Name, ~Annual.Amount, ~Category,
"Moira's Earnings", 50000L, "Income",
"Johnny's Earnings", 300000L, "Income",
"Living Expenses", 140000L, "Expenses",
"Spent Savings", 25238L, "Expenses",
"Liabilities", 44280L, "Expenses",
"Planned Savings", 23000L, "Expenses",
"Taxes", 98482L, "Expenses",
"Insurance", 13000L, "Expenses"
)
我正在尝试创建类似于此图表的内容,但只有前 2 个流量,所有收入都从支出流向一列:
library(tidyverse)
library(networkD3)
data <- tibble::tribble(
~Name, ~Annual.Amount, ~Category,
"Moira's Earnings", 50000L, "Income",
"Johnny's Earnings", 300000L, "Income",
"Living Expenses", 140000L, "Expenses",
"Spent Savings", 25238L, "Expenses",
"Liabilities", 44280L, "Expenses",
"Planned Savings", 23000L, "Expenses",
"Taxes", 98482L, "Expenses",
"Insurance", 13000L, "Expenses"
)
Nodes = tibble(
Name = c(data$Name, "Budget") %>% unique()
) %>% as.data.frame()
df = data %>% filter(Category=="Income") %>%
select(-Category) %>%
rename(Source = Name) %>%
mutate(Target = "Budget") %>%
bind_rows(
data %>%
filter(Category=="Expenses") %>%
select(-Category) %>%
rename(Target = Name) %>%
mutate(Source = "Budget")
) %>% mutate(
IDSource = match(Source, Nodes$Name)-1,
IDTarget = match(Target, Nodes$Name)-1
) %>% as.data.frame()
sankeyNetwork(Links = df, Nodes = Nodes,
Source = "IDSource", Target = "IDTarget",
Value = "Annual.Amount", NodeID = "Name",
sinksRight=FALSE, fontSize = 16)
小更新
library(webshot)
webshot::install_phantomjs()
sankeyNetwork(Links = df, Nodes = Nodes,
Source = "IDSource", Target = "IDTarget",
Value = "Annual.Amount", NodeID = "Name",
sinksRight=FALSE, fontSize = 16) %>%
saveNetwork("sn.html")
webshot("sn.html", "sankeyNetwork.png")