小编Luc*_*uca的帖子

如何在ggplot2中获得水平颜色渐变?

我想创建一个水平温度计图表,颜色渐变从绿色(左)到红色(右)。

我能够添加颜色渐变,但它是垂直的而不是水平的。

其次,是否可以在图表上方显示“我”文本?当它位于图表顶部时很难阅读

library(ggplot2)
library(grid)

g <- rasterGrob(c("lightgreen", "yellow", "orange", "red"), 
                width=unit(1,"npc"), height = unit(1,"npc"), 
                interpolate = TRUE) 

myVariable1 <- 17
dataset <- data.frame(myVariable1)

maxVariable1 = max(myVariable1, 25)

ggplot(dataset, aes(myVariable1)) +
  scale_x_continuous(expand = c(0, 0), limits = c(0, maxVariable1)) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 10)) +

  annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +   

  theme(
    axis.title.y=element_blank(),
    axis.ticks.y=element_blank(),
    axis.text.y=element_blank()
  ) + 
  geom_vline(aes(xintercept=myVariable1), color="red", size=1) +  
  annotate("text", x=myVariable1-1, y=10-0.4, label="Me", colour="red")
Run Code Online (Sandbox Code Playgroud)

gradient r colors linear-gradients ggplot2

2
推荐指数
1
解决办法
951
查看次数

标签 统计

colors ×1

ggplot2 ×1

gradient ×1

linear-gradients ×1

r ×1