首页 文章

如何为ggplot2添加手动颜色(geom_smooth / geom_line)

提问于
浏览
4

我想用ggplot2 Build 一个情节 . 因此,我使用geom_line来显示线条和geom_smooth以显示特定索引的Min-Max-Range . 使用两个数据帧,第一行包括日期(例如:2013-02-04),下一个是测量值(例如2.532283) .

首先,我生成一个包含所有样式的空ggplot:

yrange_EVI2 =是索引的范围(最小 - 最大) xrange =是x轴的日期范围(最早 - 最新日期)

EVI2_veg <- ggplot() + geom_blank() + 
            ylim(yrange_EVI2) + xlim(xrange) +
            ggtitle("EVI2 for reference-data in Azraq (Jordan)") + ylab("EVI2") + xlab("month") +
            theme_bw(base_size = 12, base_family = "Times New Roman")

第二步是绘制范围(最小 - 最大范围)和具有特定值的平均值的线:

EVI2_veg <- EVI2_veg +
            geom_smooth(aes(x=Date, y=Vegetable_mean, ymin=Vegetable_min, ymax=Vegetable_max), data=Grouped_Croptypes_EVI2, stat="identity") +
            geom_line(aes(x=Date, y=Tomato), data=Sample_EVI2_A_SPOT)

在最后一步中,我尝试使用scale_fill_manual和scale_color_manual更改颜色:

EVI2_veg <- EVI2_veg + 
             scale_fill_manual("Min-Max-Range and Mean \nof specific Croptypes",labels=c("Vegetable","Tomato"),values=c("#008B00","#FFFFFF")) +
             scale_color_manual("Min-Max-Range and Mean \nof specific Croptypes",labels=c("Vegetable","Tomato"),values=c("#008B00","#CD4F39"))

我阅读了很多答案和特定包的手册,但我不明白我何时使用不同的颜色=“”和fill =“”:

  • geom_line(ads(color =“",fill="”))

  • geom_line(ads(),color =“", fill="”)

  • scale_color_manual(values = c(“")) or scale_fill_manual=(values=c("”))

如果我没有定义1.没有图例出现 . 但如果我在代码中定义它,颜色与情节不匹配 . 这是我第一次使用ggplot2,我读了很多这个有用的包,但我不明白我如何定义颜色 . 以及如何从情节和传说中匹配颜色 . 如果有人可以帮助我会很好 .

1 回答

  • 13

    首先,它运行它看看你看到了什么 . 在发表其他帖子之前请先阅读how to make a great R reproducible example . 这将使人们更容易帮助您 . 无论如何,这里有一些样本数据

    Sample_EVI2_A_SPOT<-data.frame(
        Date=seq(as.Date("2014-01-01"), as.Date("2014-02-01"), by="1 day"),
        Tomato = cumsum(rnorm(32))
    )
    Grouped_Croptypes_EVI2<-data.frame(
        Date=seq(as.Date("2014-01-01"), as.Date("2014-02-01"), by="1 day"),
        Vegetable_mean=cumsum(rnorm(32))
    )
    Grouped_Croptypes_EVI2<-transform(Grouped_Croptypes_EVI2,
        Vegetable_max=Vegetable_mean+runif(32)*5,
        Vegetable_min=Vegetable_mean-runif(32)*5
    )
    

    这应该是你想要的情节

    EVI2_veg <- ggplot() + geom_blank() + 
        ggtitle("EVI2 for reference-data in Azraq (Jordan)") +
        ylab("EVI2") + xlab("month") +
        theme_bw(base_size = 12, base_family = "Times New Roman") + 
        geom_smooth(aes(x=Date, y=Vegetable_mean, ymin=Vegetable_min, 
            ymax=Vegetable_max, color="Vegetable", fill="Vegetable"),
            data=Grouped_Croptypes_EVI2, stat="identity") +
        geom_line(aes(x=Date, y=Tomato, color="Tomato"), data=Sample_EVI2_A_SPOT) +
        scale_fill_manual(name="Min-Max-Range and Mean \nof specific Croptypes",
            values=c(Vegetable="#008B00", Tomato="#FFFFFF")) +
        scale_color_manual(name="Min-Max-Range and Mean \nof specific Croptypes",
            values=c(Vegetable="#008B00",Tomato="#CD4F39"))
    EVI2_veg
    

    请注意 aes() 调用中添加了 color=fill= . 你真的应该把你想要的东西放在 aes() 中的传说中 . 在这里我指定"fake"颜色然后我在 scale_*_manual 命令中定义它们 .

    sample output

相关问题