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

I want to build a plot with ggplot2. Therefore, I use geom_line to render strings and geom_smooth to show the Min-Max-Range of a specific index. Two data frames were used, the first line consists of a date (for example, 2013-02-04), and the next is the measured values ​​(for example, 2.532283).

First I create an empty ggplot with all the styles:

yrange_EVI2 = is the index range (minimum - maximum) xrange = is the date range for the x axis (earliest is the last date)

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

The second step is to build ranges (Min-Max-Range) and lines with an average value for certain values:

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)

In the last step, I tried to change the color using scale_fill_manual and 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 ( ( = ", =" "))
  • geom_line (ads(), color = ", fill =" ")
  • scale_color_manual (values ​​= c ( ")) scale_fill_manual = (values ​​= c (" "))

1. . , . ggplot2 , , . . , - .

+4
1

-, , , , . , R, . . ,

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

color= fill= aes(). , aes(). "" , scale_*_manual.

sample output

+10

Source: https://habr.com/ru/post/1543424/


All Articles