Home > Enterprise >  Aesthetics in ggplot give unexpected results
Aesthetics in ggplot give unexpected results

Time:10-02

So what I'm trying to do is color different parts of the lineplot representing the dataset with different colors because the majority of the data is original but some of it has to be extrapolated.

For some reason I can't get line colors working:

pop <- ggplot()   
       geom_path(df[1:153, ], aes(x = period, y = population, size = 2, color = "blue"))  
       geom_path(df[153:158, ], aes(x = period, y = population, size = 2, color = "red"))

This simply creates a single red line and with what appears to be size 1 thickness.

But everything seems to work weirdly. This code, for example, gives me a thick red line:

pop <- ggplot(df)   
       geom_path(aes(x = period, y = population, size = 5, color = "blue")) 

And the output: enter image description here

What am I doing wrong here?

CodePudding user response:

Creating two lines manually like that is really not using the full power of ggplot; and also, move the hard coded colors and sizes out of aes().

Try this:

pop <- ggplot(df)   
       geom_path(aes(x = period, y = population, color = (1:length(period))>153), size=2) 

(It would be better to have an indicator column for data being interpolated, and not specifying row numbers)

For your single line:

pop <- ggplot(df)   
       geom_path(aes(x = period, y = population), color = "blue", size=5)
  • Related