Convert date to continuous scale / variable

Is there a way to convert or instruct ggplot to interpret a Dates column as a continuous variable?

My data ( df ) is as follows:

 Location Date Value 56.28,-36.57 2011-01-10 32 56.28,-36.57 2010-02-08 40 52.24,-36.58 2010-03-22 18 52.24,-36.58 2011-06-14 39 52.25,-36.59 2012-04-10 41 52.25,-36.59 2010-04-09 38 

I tried to build the data with the following command:

 g=ggplot(df) + geom_boxplot(aes(factor(Location),Value, col=Date))+ geom_jitter(aes(factor(Location),Value),size=1) + scale_colour_gradient(low='red',high='green') 

But received the following error message:

 Error: Discrete value supplied to continuous scale 

If I convert a Date to Date object (e.g. col=as.Date(Date) ), I get the following error:

 Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0 

The goal is for the Date column to define the color of the dots, with the earliest dates having color red and later dates having green in the color gradient.

+4
source share
2 answers

One option is to enclose a date column in as.numeric . However, as Hadley noted in the comments, the trans argument to scale_colour_gradient can take the value date . This has the added benefit (compared to what I previously posted) of a legend displaying date values ​​rather than numeric ones.

Here it is generally:
Also note that I moved the col argument to geom_jitter (not geom_boxplot )

 ggplot(df) + geom_boxplot(aes(factor(Location),Value)) + geom_jitter(aes(factor(Location),Value, col=Date),size=2) + # <~~~ col scale_colour_gradient(trans="date", low="red", high="green") + xlab("Location") 

enter image description here


previous answer using as.numeric for comparison

You can wrap a column in as.numeric . Also, I moved the col argument to geom_jitter (not geom_boxplot ).

 ggplot(df) + geom_boxplot(aes(factor(Location),Value))+ geom_jitter(aes(factor(Location),Value, col=as.numeric(Date)),size=2) + scale_colour_gradient(low='red',high='green') + theme(legend.position="none") + xlab("Location") 

enter image description here

+5
source

You can try to take the min and max of the Date column and display dates on a scale in the range from 0 to 1.

 df$Date=as.POSIXct(df$Date) min=min(df$Date) max=max(df$Date) as.numeric(difftime(df$Date,min,units='days'))/as.numeric(difftime(max,min,units='days')) [1] 0.42426474 0.00000000 0.05298048 0.61992950 1.00000000 0.07570895 

Add this to your data frame and you must be in business.

+1
source

Source: https://habr.com/ru/post/1495161/


All Articles