2

I'm trying to generalize a set of plots I regularly need through a function - I have trouble in getting some aspects right in there.

myCustomScatter <- function(df, col_x, col_y, col_z){
  p1 <- ggplot(df, aes(x=df[,col_x]))
  p1 <- p1 + geom_point(aes(y=df[,col_y], color=df[,col_z]))
  p1 <- p1 + scale_x_continuous(name=colnames(df)[col_x])
  p1 <- p1 + scale_y_continuous(name=colnames(df)[col_y])
  return(p1)
}

df1 <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1)))

myCustomScatter(df1, 1, 2, 3)

This gives the following plot as expected.

custom plot

  1. I need the color ranges to be discrete based on df[,3] values- I need blue for value > 90, green for 90 >= value > 70, yellow for 70 >= value > 55, orange for 55 >= value > 25 & red for value <= 25 - how do I specify this ?

  2. I need the title of the legend instead of df[,col_z] to be c which I can get through colnames(df1)[3] - how do I specify this ?

user3206440
  • 4,749
  • 15
  • 75
  • 132

2 Answers2

3

You can try this:

myCustomScatter <- function(df, col_x, col_y, col_z){
  p1 <- ggplot(df, aes_string(x=df[,col_x], color = cut(df[,col_z], c(-Inf, 25, 55, 70, 90, Inf))), size = 6)
  p1 <- p1 + geom_point(aes_string(y=df[,col_y]))
  p1 <- p1 + scale_x_continuous(name=colnames(df)[col_x])
  p1 <- p1 + scale_y_continuous(name=colnames(df)[col_y]) # + guides(color=guide_legend('c'))
  p1 <- p1 + scale_color_manual(name = names(df)[col_z],
                                values = c("red",
                                           "orange",
                                           "yellow",
                                           "green",
                                           "blue"))
  return(p1)
}

df1 <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1)))

myCustomScatter(df1, 1, 2, 3)

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
  • -1 because that's not how aes_string is supposed to work (and the mapping would fail in more complicated plots, possibly without warning) – baptiste Dec 05 '16 at 07:03
1

use aes_string if you don't know the variable names in advance,

myCustomScatter <- function(df, col_x, col_y, col_z){

  ggplot(df, aes_string(x=names(df)[col_x], y = names(df)[col_y])) + 
    geom_point(aes_string(colour=names(df)[col_z])) + 
    scale_x_continuous(names(df)[col_x]) + 
    scale_y_continuous(names(df)[col_y]) +
    scale_color_gradientn(colours = terrain.colors(5))
}

df1 <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1)))

myCustomScatter(df1, 1, 2, 3)
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • `scale_color_gradientn(colours = terrain.colors(5))` - this creates a gradient with 5 colors dividing the range for `df[,col_z]` into equal ranges - right? - however, the range which is required is `25, 55, 70, 90` - how to specify this ? – user3206440 Dec 05 '16 at 08:20
  • try values in ?scale_color_gradientn – baptiste Dec 05 '16 at 08:23
  • I looked at this - but didn't understood how to use it ? What I need to do is to specify discrete ranges of `df[col_z,]` and show them in discrete colors. – user3206440 Dec 08 '16 at 04:45