Reputation: 167
Is it possible to format the number labels on a guide_colourbar to a date format e.g. %b %Y
? So instead of having 17400 17700 18000 18300
on the Closing Date
legend, there would be the %b %Y
equivalents.
Example plot
Code used to produce the plot
Area = c(1705, 902, 1496, 1257, 763)
ClosePrice = c(2292500, 977520, 1990680, 1720840, 855330)
HasTerrace = c(FALSE, FALSE, TRUE, FALSE, TRUE)
CloseDate = as.Date(c('2017-03-17', '2017-02-28', '2018-05-09', '2020-03-01',
'2017-01-09'))
data <- data.frame(Area, ClosePrice, HasTerrace, CloseDate)
data %>%
ggplot(aes(x=Area, y=ClosePrice, shape=HasTerrace)) +
geom_point(aes(colour=CloseDate), size=3, alpha=0.8) +
scale_color_viridis() +
theme_minimal() +
scale_x_continuous(expression(Indoor~Area~(ft^2)),
labels=label_comma()) +
scale_y_continuous('Closing Price ($m)',
labels=label_number(accuracy=0.01, scale=10^-6)) +
theme(legend.position='bottom', legend.box='vertical') +
guides(shape = guide_legend(title='Has a Terrace'),
colour = guide_colourbar(title='Closing Date',
barwidth=10,
labels=label_date(format='%b %Y')))
Upvotes: 2
Views: 582
Reputation: 5747
Here is a solution. The guide_colorbar()
function does not accept the labels
argument, so it ignores it. You need to add your labels to scale_color_viridis
. However, that will create an error unless you also include trans = "date"
.
data %>%
ggplot(aes(x=Area, y=ClosePrice, shape=HasTerrace)) +
geom_point(aes(colour=CloseDate), size=3, alpha=0.8) +
scale_color_viridis(name='Closing Date',
trans = "date",
labels = label_date(format = "%b %Y") )+
theme_minimal() +
scale_x_continuous(expression(Indoor~Area~(ft^2)),
labels=label_comma()) +
scale_y_continuous('Closing Price ($m)',
labels=label_number(accuracy=0.01, scale=10^-6)) +
theme(legend.position='bottom', legend.box='vertical') +
guides(shape = guide_legend(title='Has a Terrace'),
colour = guide_colourbar(barwidth=10))
Upvotes: 3