KarthikRIyer / swiftplot

Swift library for Data Visualization :bar_chart:
Apache License 2.0
401 stars 39 forks source link

Add multiple independent y axes feature to LineGraph #12

Closed KarthikRIyer closed 5 years ago

KarthikRIyer commented 5 years ago

For this I was thinking we can have a primary and secondary axis in LinePlot. While adding the series we can assign which axis we wan to use, which would be the primary one by default. For differentiating which axis a series belongs to we can maybe make the lines dashed... Any ideas?

In matplotlib, the new axis is twinned with an existing axis, so the new axis is plotted opposite to the existing axis. But here each axis has a series of data. Something like this:

import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
ax1.plot(t, s1, 'b-')
ax1.set_xlabel('time (s)')
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
ax1.tick_params('y', colors='b')

ax2 = ax1.twinx()
s2 = np.sin(2 * np.pi * t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
ax2.tick_params('y', colors='r')

fig.tight_layout()
plt.show()

In ggplot, from what I could make out, the secondary axis is plotted opposite to the primary axis but it's scale has to be set manually, by multiplying or dividing the primary scale values by another value. Something like this:

p <- ggplot(obs, aes(x = Timestamp))
  p <- p + geom_line(aes(y = air_temp, colour = "Temperature"))

  # adding the relative humidity data, transformed to match roughly the range of the temperature
  p <- p + geom_line(aes(y = rel_hum/5, colour = "Humidity"))

  # now adding the secondary axis, following the example in the help file ?scale_y_continuous
  # and, very important, reverting the above transformation
  p <- p + scale_y_continuous(sec.axis = sec_axis(~.*5, name = "Relative humidity [%]"))

  # modifying colours and theme options
  p <- p + scale_colour_manual(values = c("blue", "red"))
  p <- p + labs(y = "Air temperature [°C]",
                x = "Date and time",
                colour = "Parameter")
  p <- p + theme(legend.position = c(0.8, 0.9))
p
KarthikRIyer commented 5 years ago

Any ideas?