关于r:如何为ggplot2添加手动颜色(geom_smooth / geom_line)

How to add manual colors for a ggplot2 (geom_smooth/geom_line)

我想用ggplot2建立一个情节。因此,我使用geom_line可视化线,并使用geom_smooth来显示特定索引的最小-最大范围。
使用了两个数据框,第一行包含日期(例如:2013-02-04),下一行是测量值(例如2.532283)。

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

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

1
2
3
4
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")

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

1
2
3
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更改颜色:

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

我阅读了很多有关特定软件包的答案和手册,但是当我使用不同的colors ="和fill ="时我不明白:

  • geom_line(ads(color =",fill ="))
  • geom_line(ads(),color =",fill =")
  • scale_color_manual(values = c("))或scale_fill_manual =(values = c("))
  • 如果我没有定义1.没有图例出现。但是,如果我像代码中那样定义颜色,则颜色与图不匹配。这是我第一次使用ggplot2,我读了很多有用的程序包,但我不明白如何定义颜色。以及剧情和图例中的颜色如何匹配。如果有人可以帮助我,那将是很好的。


    首先,将示例数据包含在任何绘图代码中总是很不错的,否则我们将无法运行它来查看您看到的内容。 在发表其他文章之前,请阅读如何制作出色的R可重现示例。 这将使人们更轻松地为您提供帮助。 无论如何,这是一些示例数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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
    )

    这应该使您想要的情节

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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 \
    of specific Croptypes",
            values=c(Vegetable="#008B00", Tomato="#FFFFFF")) +
        scale_color_manual(name="Min-Max-Range and Mean \
    of specific Croptypes",
            values=c(Vegetable="#008B00",Tomato="#CD4F39"))
    EVI2_veg

    请注意在aes()调用中添加了color=fill=。 您确实应该将所需的内容放在aes()中的图例中。 在这里,我指定"假"颜色,然后在scale_*_manual命令中对其进行定义。

    sample output