R语言 三个变量的冲积图

ruarlubt  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(134)

从这样的 Dataframe :

data.frame(status = c("open", "close", "close", "open/close","close"), 
           stock = c("google", "amazon", "amazon", "yahoo", "amazon"), 
           newspaper = c("times", "newyork", "london", "times", "times"))

要获得冲积地块

,需要如何转换数据
其中两列是股票和报纸,链接是状态列的频率

vfwfrxfs

vfwfrxfs1#

根据所提供的数据,很难准确地知道需要绘制什么样的图。我建议使用受R库ggalluvial的冲积地块this blog启发的方法:

library(ggalluvial)
library(ggplot2)
library(dplyr)

df <- data.frame(status = c("open", "close", "close", "open/close", "close"), 
                 stock = c("google", "amazon", "amazon", "yahoo", "amazon"), 
                 newspaper = c("times", "newyork", "london", "times", "times"))

# Count the number of occurance for each alluvial
df <- df %>% 
  dplyr::group_by(stock, newspaper, status) %>% 
  summarise(n = n()) 

# Define the factors
df$status <- factor(df$status, levels = c("open", "open/close", "close"))
df$stock <- factor(df$stock, levels = c("google", "amazon", "yahoo"))
df$newspaper <- factor(df$newspaper, levels = c("times", "newyork", "london"))

# Plot the alluvial as in https://cran.r-project.org/web/packages/ggalluvial/vignettes/ggalluvial.html#alluvia-wide-format
ggplot2::ggplot(df, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  ggalluvial::geom_alluvium(aes(fill = status), width = 1/12) +
  ggalluvial::geom_stratum(width = 1/12, fill = "black", color = "grey") +
  ggplot2::geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), expand = c(.05, .05)) +
  ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") +
  ggplot2::ggtitle("Alluvial-Test")

相关问题