0

consider the following simple example

time_idx <- c('2015-11-06 00:35', '2015-11-06 00:36' , '2015-11-06 00:42', '2015-11-06 00:43')
bid <- c(2,2,3,4)
ask <- c(3,3,4,4.5)
q_bid <- c(3,2,4,2)
q_ask <- c(1,3,3,2)

data <- data_frame(time_idx, bid, q_bid, ask, q_ask)
data <- data %>% mutate(time_idx = ymd_hm(time_idx))

# A tibble: 4 × 5
             time_idx   bid q_bid   ask q_ask
               <dttm> <dbl> <dbl> <dbl> <dbl>
1 2015-11-06 00:35:00     2     3   3.0     1
2 2015-11-06 00:36:00     2     2   3.0     3
3 2015-11-06 00:42:00     3     4   4.0     3
4 2015-11-06 00:43:00     4     2   4.5     2

Here I would like to plot the time series of bid and ask using dots instead of a line, and I want the size of the dot to be proportional to q_bid and q_ask respectively.

This is what I came up with:

ggplot(data, aes(x = time_idx)) + 
  geom_point(aes(y = bid, color = 'q_bid', size = q_bid, alpha = .4) ) +
  geom_point(aes(y = ask, color = 'q_ask', size = q_ask, alpha = .4)) +
  scale_x_datetime(breaks = scales::pretty_breaks(n = 10)) +
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
  theme(axis.text.x = element_text(angle=90,hjust=1)) +
  theme_igray() + xlab('time') + ylab('price')

myplot

As you can see the plot is correct, but the legend is completely crazy. Namely :

  1. that is this 0.4 legend?
  2. the name of the series are bid and ask, not q_ask and q_bid
  3. the weights are q_bid and q_ask, not q_bid only

What is wrong here?

Many thanks!

Axeman
  • 32,068
  • 8
  • 81
  • 94
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235

0 Answers0