简体   繁体   中英

Manual scale ggplot legend

I'd like to make a 'geom tile()' plot with x axis time and y axis date. I want to a.) scale the plot with scale_fill_gradient(low="lightyellow", high="red") , but I get an error. Discrete value supplied to continuous scale and b.) how to set breaks on the x-axis at 1-hour intervals?

在此处输入图像描述

Sample code:

library(dplyr)
library(ggplot2)
library(readr)


    df<-as.data.frame(df)
    
    ggplot(df, aes(x=time, y=date) )+ geom_tile(aes(fill=temperature))+
    theme_bw()+
      labs(x="Time", y ="", fill="Temperature")  +
      theme(plot.title = element_text(hjust = 0.5))+ 
      theme(axis.text.x = element_text(hjust = 1,family="Times", face="bold", size=14), 
            axis.text.y = element_text(family="Times", face="bold", size=14),
            strip.text = element_text(size=12, face="bold"))

Sample data:

df<-structure(list(time = structure(c(63285, 63885, 64485, 65085, 
65685, 66285, 66885, 67485, 68085, 68685, 69285, 69885, 70485, 
71085, 71685, 72285, 72885, 73485, 74085, 74685, 75285, 75885, 
76485, 77085, 77685, 78285, 78885, 79485, 80085, 80685, 81285, 
81885, 82485, 83085, 83685, 84285, 84885, 85485, 86085, 285, 
885, 1485, 2085, 2685, 3285, 3885, 4485, 5085, 5685, 6285), class = c("hms", 
"difftime"), units = "secs"), temperature = c("6.31<f8>", "14.81<f8>", 
"17.13<f8>", "16.56<f8>", "16.44<f8>", "16.44<f8>", "16.56<f8>", 
"16.69<f8>", "16.88<f8>", "17.00<f8>", "17.13<f8>", "17.19<f8>", 
"17.31<f8>", "17.50<f8>", "17.81<f8>", "18.06<f8>", "18.19<f8>", 
"18.25<f8>", "18.25<f8>", "18.19<f8>", "18.06<f8>", "17.94<f8>", 
"17.81<f8>", "17.75<f8>", "17.63<f8>", "17.50<f8>", "17.44<f8>", 
"17.38<f8>", "17.25<f8>", "17.19<f8>", "17.13<f8>", "17.06<f8>", 
"17.00<f8>", "17.00<f8>", "16.88<f8>", "16.88<f8>", "16.88<f8>", 
"16.81<f8>", "16.81<f8>", "16.81<f8>", "16.81<f8>", "16.88<f8>", 
"16.88<f8>", "16.88<f8>", "16.88<f8>", "16.88<f8>", "16.81<f8>", 
"16.81<f8>", "16.81<f8>", "16.75<f8>"), humidity = c(0.674, 0.643, 
0.472, 0.503, 0.515, 0.523, 0.53, 0.539, 0.541, 0.546, 0.548, 
0.549, 0.555, 0.561, 0.578, 0.577, 0.575, 0.579, 0.596, 0.597, 
0.599, 0.604, 0.612, 0.616, 0.619, 0.623, 0.637, 0.638, 0.639, 
0.639, 0.643, 0.646, 0.649, 0.653, 0.654, 0.66, 0.664, 0.668, 
0.672, 0.676, 0.691, 0.693, 0.693, 0.694, 0.693, 0.694, 0.698, 
0.691, 0.694, 0.697), date = c("04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", "04/01/2022", 
"04/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", 
"05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", "05/01/2022", 
"05/01/2022", "05/01/2022")), row.names = c(NA, 50L), class = "data.frame")

Original plot:

在此处输入图像描述

The issue is that your temperature column is a character containing a symbol <f8> (perhaps a degree symbol?). To make scale_fill_gradient work you have to convert it to a numeric. For the second part of your question, as your time column is of type hms you could set the breaks of scale_x_time to 1 hour intervals using hms::hms(hours = seq(0, 24, 1)) (To avoid clutter I use 4 hours in the example code):

library(ggplot2)

df$temperature <- as.numeric(gsub("<f8>$", "", df$temperature))

ggplot(df, aes(x=time, y=date) )+ 
  geom_tile(aes(fill=temperature))+
  scale_x_time(breaks = hms::hms(hours = seq(0, 24, 4))) +
  scale_fill_gradient(low="lightyellow", high="red") +
  theme_bw()+
  labs(x="Time", y ="", fill="", title="Temperature")  +
  theme(plot.title = element_text(hjust = 0.5))+ 
  theme(axis.text.x = element_text(hjust = 1,family="Times", face="bold", size=14), 
        axis.text.y = element_text(family="Times", face="bold", size=14),
        strip.text = element_text(size=12, face="bold"))

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