我想创建一个水平温度计图表,颜色渐变从绿色(左)到红色(右)。
我能够添加颜色渐变,但它是垂直的而不是水平的。
其次,是否可以在图表上方显示“我”文本?当它位于图表顶部时很难阅读
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)