Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

colors

ggplot2

I want to built a plot with ggplot2. Therefore i use geom_line to visualize lines and geom_smooth to show the Min-Max-Range of a specific index. Two data frames were used, the first row consists of the date (e.g.: 2013-02-04) and the next are measured values (e.g. 2.532283).

First i generate an empty ggplot with all styles:

yrange_EVI2 = is the Range of the Index (Minimum - Maximum) xrange = is the date range for the x-Axis (earliest - latest 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")

Second step is to plot the Ranges (Min-Max-Range) and lines with the mean for specific 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 with 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"))

I read a lot of answers and the manuals for the specific packages but i don't understand when i use the different colors="" and fill="":

  1. geom_line(ads(color="",fill=""))
  2. geom_line(ads(),color="", fill="")
  3. scale_color_manual(values=c("")) or scale_fill_manual=(values=c(""))

If i don't define the 1. no legend appears. But if i define it like in the Code the color don't match to the plot. Its my first time with ggplot2 and i read a lot of this useful package but i don't understand how i can define the colors. And how the colors from the plot and legend matching. It would be nice if somebody could help me.

like image 712
MaTo Avatar asked Jun 05 '14 14:06

MaTo


People also ask

How to manually set colors in ggplot?

To specify colors of the bar in Barplot in ggplot2, we use the scale_fill_manual function of the ggplot2 package. Within this function, we need to specify a color for each of the bars as a vector. We can use colors using names as well as hex codes.

How to change the colour of points in ggplot?

To color the points in a scatterplot using ggplot2, we can use colour argument inside geom_point with aes. The color can be passed in multiple ways, one such way is to name the particular color and the other way is to giving a range or using a variable.

What does Geom_smooth mean in R?

The curve geom_smooth produces is indeed an estimate of the conditional mean function, i.e. it's an estimate of the mean distance in miles conditional on the number of trips per week (it's a particular kind of estimator called LOESS).


1 Answers

First, it's always nice to include sample data with any plotting code otherwise we can't run it to see what you see. Please read how to make a great R reproducible example before making other posts. It will make it much easier for people to help you. Anyway, here's some sample data

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
)

And this should make the plot you want

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

Note the addition of color= and fill= in the aes() calls. You really should put stuff you want in legends inside aes(). Here i specify "fake" colors that i then define them in the scale_*_manual commands.

sample output

like image 83
MrFlick Avatar answered Oct 02 '22 20:10

MrFlick