【问题标题】:Change labels length in a plot更改绘图中的标签长度
【发布时间】:2022-12-11 19:46:41
【问题描述】:

我尝试运行这个情节选项:

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.2, 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")

有什么命令可以减少股票和报纸这两栏的标签吗?

【问题讨论】:

    标签: r ggplot2 ggalluvial


    【解决方案1】:

    您可以像这样更改geom_label中的size

    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()) 
    #> `summarise()` has grouped output by 'stock', 'newspaper'. You can override
    #> using the `.groups` argument.
    
    # 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)), size = 2) +
      ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), expand = c(.05, .05)) +
      ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") +
      ggplot2::ggtitle("Alluvial-Test")
    #> Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
    #> ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
    

    创建于 2022-12-11 reprex v2.0.2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-30
      • 2015-08-16
      • 2019-01-01
      • 1970-01-01
      • 2023-04-10
      • 2019-08-06
      • 2011-06-13
      相关资源
      最近更新 更多