R语言 更改图表的流程颜色[副本]

0lvr5msh  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(149)
    • 此问题在此处已有答案**:

Custom colors for groups using ggplot2(1个答案)
7天前关闭。
是否有设置自己托盘的命令?
在这样的情节中:

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")

怎么可能设定我们自己的颜色呢?

z9smfwbn

z9smfwbn1#

您已经在设置自己的调色板(您已经通过scale_fill_brewer从RColorBrewer包中选择了“Set1”调色板)。如果您想手动设置颜色,请使用scale_fill_manual

ggplot(df, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  geom_alluvium(aes(fill = status), width = 1/12) +
  geom_stratum(width = 1/12, fill = "gray30", color = "white", size = 2) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum), angle = 90),
            color = "white", size = 6) +
  scale_x_discrete(limits = c("stock", "newspaper"), expand = c(.05, .05)) +
  scale_fill_manual(values = c(open = 'red4', 
                               `open/close` = 'orange3', 
                               close ='navy')) +
  ggtitle("Alluvial-Test") +
  theme_void(base_size = 16) +
  theme(plot.margin = margin(20, 20, 20, 20))

相关问题