简体   繁体   中英

How to obtain a horizontal color gradient in ggplot2?

I want to create a horizontal thermometer chart, with a color gradient from green (left) to red (right).

I was able to add a color gradient but it's vertical instead of horizontal.

Secondly, is it possible to show the 'Me' text above the chart? It's hard to read when it's on top of the chart

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

To get a horizontal gradient in a rasterGrob object, you should define your image like this:

> matrix(c("lightgreen","yellow","orange","red"), nrow = 1)
     [,1]         [,2]     [,3]     [,4] 
[1,] "lightgreen" "yellow" "orange" "red"

Instead of this:

> c("lightgreen","yellow","orange","red")
[1] "lightgreen" "yellow"     "orange"     "red"  

Or this:

> matrix(c("lightgreen","yellow","orange","red"), ncol = 1)
     [,1]        
[1,] "lightgreen"
[2,] "yellow"    
[3,] "orange"    
[4,] "red"   

As for labelling beyond the chart area, you can do that if you turn clipping off in Cartesian coordinates:

# define raster grob with horizontal gradient
g <- rasterGrob(matrix(c("lightgreen","yellow","orange","red"), nrow = 1), 
                width=unit(1,"npc"), height = unit(1,"npc"), 
                interpolate = TRUE) 

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) +   
  geom_vline(aes(xintercept=myVariable1), color="red", size=1) +

  # no need to adjust y value; we can use the maximum limit set above,
  # & assign a negative number to vjust instead;
  # also, since it's now sitting atop the chart, it'll make more sense
  # to align the label with myVariable, rather than dodge it to one side. 
  annotate("text", x=myVariable1, y=10, label="Me", colour="red",
           vjust = -0.5) +

  # turn off clipping here
  coord_cartesian(clip = "off") +

  # add plot.margin specification to theme, with large value for top margin
  # (5.5 is the default for all four sides)
  theme(
    axis.title.y=element_blank(),
    axis.ticks.y=element_blank(),
    axis.text.y=element_blank(),
    plot.margin = unit(c(20, 5.5, 5.5, 5.5), "pt")
  )

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM