ggplot_bars 渐变填充

ggplot_bars with gradient fill

嗨,我想制作这种图表。我知道这是条形图图。并且可以与 bar_charts 一起使用。但我唯一能做的就是制作一个简单的条形图,其中最大值位于条形图上方。我想它更复杂,我不确定 ggplot 是否可以做到。有什么想法吗?

[

tab1<read.csv("/Users/vladalexandru/Documente/vlad_R/R_markdown/Rmrkd/Tab/Tx_tn.csv")
tab1$dat <- as.Date(tab1$dat)
tab2 <- aggregate(tab1, by = list(format(tab1$dat, "%m")), FUN = "mean")
ggplot(data=tab2, aes(x=Group.1, y= tx)) +
geom_bar(stat="identity", fill="yellow")+
geom_text(aes(label=round(tx,0)), vjust=-0.3, size=3.5)+
labs(x = "month")+
scale_y_continuous(name = "°C")+
theme_minimal()

实际上可以在 ggplot 中制作这样的 gradient-filled 箱线图。虽然很麻烦。您必须在堆叠条形图的底部添加一个透明条,然后通过将条切成细条并为其着色来构造渐变:

set.seed(123)

a <- sample(10:20, 12, TRUE)
b <- sample(1:10, 12, TRUE)

data.frame(vals = c(sapply(1:12, function(i) c(rep(a[i]/39, 39), 20 - b[i]))),
           month =factor( rep(month.abb, each = 40), levels = month.abb),
           fills = rep(c(1:39, "top"), 12)) %>%
  ggplot(aes(x = month, y = vals, fill = fills)) +
  geom_col(fill = "gray95", aes(y = Inf), width = 0.7) +
  geom_col(position = position_stack(), width = 0.5) +
  scale_fill_manual(values = c("#00000000", 
                    colorRampPalette(colors = c("forestgreen",
                                                "gold", "orange"))(38),
                               "#00000000"),
                    guide = guide_none()) +
  theme_classic()