【问题标题】:Color coding weather data using ggplot2使用 ggplot2 对天气数据进行颜色编码
【发布时间】:2020-01-27 06:01:21
【问题描述】:

我有一个包含降水量和温度的天气文件,我将其编码为将整个数据集绘制成四个类别:凉爽和潮湿、凉爽和干燥、温暖和潮湿以及温暖和干燥。我想知道是否可以为每个类别引入特定的颜色。我做不到

年雨温度 87 91.4 10.4 88 133.3 10.9 89 106 10.64 90 59.12 12.6 91 99 9.9 92 145.36 10.51 93 133.3 10.89 94 133 10.8 95 122 10.09 96 162 8.7 97 250 11.06 98 133 10.5 99 27 10.4 0 138 11 1 182 12.8 2 138 10 3 129 10.3 4 142 12.5 5 114 13.3 6 102 10.3 7 193 11.8 8 242 10.6 9 139 10.1 10 80.41 4.05 11 56.64 11.1 12 58.24 11.3 13 141.26 11.56 14 76.7 10.3 15 87.28 10.4 16 147.7 13.2 17 154.8 11.8 126.3390323 10.76774194

我把它编码为:

library(ggplot2)
setwd('C:/Users/nchatterjee2/Desktop')
data <- read.csv("Fall.csv", header=TRUE, sep=",",row.names=NULL, fill = T)
limit_humid <- 128
limit_warm <- 11
head(data)

    ggplot(data, aes(x = temp, y = rain)) +
      geom_text(aes(label = Year))+
      geom_vline(xintercept = limit_warm) +
      geom_hline(yintercept = limit_humid) +
      annotate('text', label = 'bold("Cool and Wet")', size = 3, parse = T,
               x = min(data$temp), y = max(data$rain)) +
      annotate('text', label = 'bold("Warm and Wet")', size = 3, parse = T,
               x = max(data$temp), y = max(data$rain)) +
      annotate('text', label = 'bold("Cool and Dry")', size = 3, parse = T,
               x = min(data$temp), y = min(data$rain)) +
      annotate('text', label = 'bold("Warm and Dry")', size = 3, parse = T,
               x = max(data$temp), y = min(data$rain)) +
      theme_bw() + 
      theme(
        plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
      ) 

      labs(x = 'Average Temperature (°C)',
           y = 'Cumulative precipitation (mm)')+ theme_light()







    library(ggplot2)
    setwd('C:/Users/nchatterjee2/Desktop')
    data <- read.csv("Fall.csv", header=TRUE, sep=",",row.names=NULL, fill = T)
    limit_humid <- 128
    limit_warm <- 11
    head(data)

    ggplot(data, aes(x = temp, y = rain)) +
      geom_text(aes(label = Year))+
      geom_vline(xintercept = limit_warm) +
      geom_hline(yintercept = limit_humid) +
      annotate('text', label = 'bold("Cool and Wet")', size = 3, parse = T,
               x = min(data$temp), y = max(data$rain)) +
      annotate('text', label = 'bold("Warm and Wet")', size = 3, parse = T,
               x = max(data$temp), y = max(data$rain)) +
      annotate('text', label = 'bold("Cool and Dry")', size = 3, parse = T,
               x = min(data$temp), y = min(data$rain)) +
      annotate('text', label = 'bold("Warm and Dry")', size = 3, parse = T,
               x = max(data$temp), y = min(data$rain)) +
      theme_bw() + 
      theme(
        plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
      ) 

      labs(x = 'Average Temperature (°C)',
           y = 'Cumulative precipitation (mm)')+ theme_light()

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是一种方法,我在数据中添加一个字段并将颜色映射到该数据:

    library(dplyr)
    data %>%
      mutate(category = case_when(temp >= limit_warm & rain >= limit_humid ~ "Warm and Wet",
                                  temp >= limit_warm  ~ "Warm and Dry",
                                  rain >= limit_humid ~ "Cool and Wet",
                                  TRUE ~ "Cool & Dry")) %>%
      ggplot(aes(x = temp, y = rain, color = category)) +
        geom_text(aes(label = Year))+
        geom_vline(xintercept = limit_warm) +
        geom_hline(yintercept = limit_humid) +
        annotate('text', label = 'bold("Cool and Wet")', size = 3, parse = T,
                 x = min(data $temp), y = max(data $rain), hjust = 0) +
        annotate('text', label = 'bold("Warm and Wet")', size = 3, parse = T,
                 x = max(data $temp), y = max(data $rain), hjust = 1) +
        annotate('text', label = 'bold("Cool and Dry")', size = 3, parse = T,
                 x = min(data $temp), y = min(data $rain), hjust = 0) +
        annotate('text', label = 'bold("Warm and Dry")', size = 3, parse = T,
                 x = max(data $temp), y = min(data $rain), hjust = 1) +
        theme_bw() + 
        theme(
        plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
      ) 
    

    如果要将图例中的“a”替换为点,可以通过隐藏文本图例,添加虚拟透明geom_point,并覆盖点图例大小来完成:

    ggplot(aes(x = temp, y = rain, color = category)) +
        geom_point(alpha = 0) +
        geom_text(aes(label = Year), show.legend = F)+
        ...
        guides(colour = guide_legend(override.aes=list(size=4, alpha = 1))) +
    

    【讨论】:

    • 嗨!非常感谢。当我尝试此代码时,出现错误: eval_tidy(pair$lhs, env = default_env) 中的错误:找不到对象'temp'
    • 我将数据命名为df,而不是现在答案中的data like your example. I've renamed as data`,希望能修复它。
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多