Search code examples
rplotggplot2scale

Use factors and variable with same kind of scale - possible?


I want to plot data from different data frames with ggplot. However, I'm having problems with discrete and continuous scales.

Let's say we want to use this data:

x <- rnorm(9,5)
y <- rnorm(9,5)
f1 <- rep(c("a","b","c"),3) 
df1 <- data.frame(x,y,f1)
x <- rnorm(9,5)
y <- rnorm(9,5)
f2 <- rep(c("d","e","f"),3)
df2 <- data.frame(x,y,f2)

I'd like to show both data frames on one plot. df1$f1 and df1$f2 shall both be responsible for colours of the points:

p <- ggplot(df1,aes(x,y))
p <- p + geom_point(aes(colour=f1))
p <- p + geom_point(data=df2,aes(x,y,colour=f2))
p

This works.

However, when I have this data:

x <- rnorm(9,5)
y <- rnorm(9,5)
f1 <- rep(c("a","b","c"),3) 
df1 <- data.frame(x,y,f1)
x <- rnorm(9,5)
y <- rnorm(9,5)
quan <- rnorm(9,1)
df2 <- data.frame(x,y,quan)

this plot doesn't work:

p <- ggplot(df1,aes(x,y))
p <- p + geom_point(aes(colour=f1))
p <- p + geom_point(data=df2,aes(x,y,colour=quan))
p

Error: Continuous variable () supplied to discrete scale_hue.

f1 as a factor, quan is a numeric value, and it seems logical that they can't be used with the same scale. But how can I define a separate scale for each data frame? Or is this maybe not possible?

I tried using scale_colour_discrete and scale_colour_continuous, but it seems as if only one of the two scales can be used for one plot.


Solution

  • You can only have one type of scale per plot. In the first example, the scale in both cases is a factor so you do not receive an error.

    For the second example, one is for a continuous variable (f) and the other is for a factor (quan). When you try to pass 'quan' ggplot expects a continuous variable, rather than a factor and provides you with the error message.

    You should try to simplify by using rbind() first, and then plotting. But make sure your data is formatted the same way. If rbind fails it's because you're not passing similarly structured data frames to it:

    df3 <- rbind(df1,df2) 
    p <- ggplot(df3, aes(x,y,colour=f) + geom_point()