Color scales for geom_point


I try to create a ternary graph ggtern(data = Libro1,x= Arcilla, y= Arena, z= MO) and use geom_point to represent with points and scales of colors the different Largos de planta obtained on the ternary graph. The problem is that being points does not allow me to use the scale_colour_gradient2 function that would be ideal for the type of representation I am looking for, with a red, yellow and green scale representing the Largo de planta obtained to visualize where the largest ones are productions depending on the type of soil.

I copy below the function with which I am working and results obtained:

> ggtern(data = Libro1, aes(x= Arcilla, y= Arena, z= MO))+
  Tlab('Arena')+Llab('Arcilla')+Rlab(expression(paste('MOx10'^-1))) +
  geom_point(aes(colour = factor(Largo)))

As you can see in the previous graphic, all the data are presented one by one with a different color. The result that I hope to obtain is a color scale with representation of the value of the data with maximum in green, medium in yellow and minimum in red.

I have tried other functions such as alpha or geom_point(aes(colour = (Largo)) , but I have not achieved the expected result.

Any recommendation or comment will be very helpful, thank you very much!

asked by Ignacio 19.09.2017 в 16:45

1 answer


First of all, in the second graph you are only drawing the points that fall on the scale of absolute black, that's why you see only a few. What you can do is use one of the color palettes already defined as terrain.colors(n) , in this case you define the number of colors in the sample:

df = data.frame(Arena = runif(100),
                Arcilla = runif(100),
                MO = runif(100),
                Largo = runif(100))

ggtern(data = df, aes(x=Arena, y=Arcilla, z=MO)) +
    Tlab('Arena') +
    Rlab(expression(paste('MOx10'^-1))) +
    geom_point(aes(colour = Largo)) +
    scale_colour_gradientn(colours = rev(terrain.colors(100)))

This would generate a graph closer to what you are looking for:

Take note, that we reverse the order of the palette so that the high values tend to the green and the low ones to the orange. Now if you want a more "exact" palette to what you ask, you can create it using colorRampPalette()

colfunc <- colorRampPalette(c("red", "yellow", "green"))

And then to generate it, you set the amount of colors:

paleta <- colfunc(100)

Now simply by setting this palette on the graphic: scale_colour_gradientn(colours = paleta) we will get something like this:

answered by 19.09.2017 / 19:06