You can customize the display of axes using the dyOptions
function (for global options) and dyAxis
function (for per-axis options). Here’s an example that uses both:
dygraph(nhtemp, main = "New Haven Temperatures") %>%
dyAxis("y", label = "Temp (F)", valueRange = c(40, 60)) %>%
dyOptions(axisLineWidth = 1.5, fillGraph = TRUE, drawGrid = FALSE)
The valueRange
is used to set a specific range for the y axis.
The axisLineWidth
option specifies a slightly wider pixel width for axis lines.
The fillGraph
option specifies that y values should be filled vertically
The drawGrid
option turns off the grid for both axes (we’ll demonstrate doing this on a per-axis basis below).
Here’s another example that customizes some other axes properties:
dygraph(AirPassengers, main = "Airline Passengers / Month") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyAxis("y", label = "Passengers (Thousands)") %>%
dyOptions(includeZero = TRUE,
axisLineColor = "navy",
gridLineColor = "lightblue")
Here we use a number of options to customize axis display:
The drawGrid
option turns off the grid for the x axis
The includeZero
option ensures that the y axis is scaled from zero rather than the low-end of it’s range of values.
The axisLineColor
and gridLineColor
options change the colors of axis and grid lines respectively.
There are many more options available that affect axis display. See the documentation on dyAxis
and dyOptions
for more details.
If you are plotting multiple series that have distinct value types you can add a second Y-axis to show a distinct value scale. This is done by setting the independentTicks
option for the y2 axis and then assigning one or more series to the y2 axis. For example:
# define mts with distinct y-axis scales
temperature <- ts(frequency = 12, start = c(1980, 1),
data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5,
25.2, 26.5, 23.3, 18.3, 13.9, 9.6))
rainfall <- ts(frequency = 12, start = c(1980, 1),
data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0,
135.6, 148.5, 216.4, 194.1, 95.6, 54.4))
weather <- cbind(rainfall, temperature)
# assign the "rainfall" series to the y2 axis
dygraph(weather) %>%
dySeries("rainfall", axis = 'y2')
The independentTicks
option can be used to determine which axis is primary (and therefore which axis grid lines are aligned with). In order to display the secondary axis scale at least one of the two axes must specify independentTicks = TRUE. Possible combinations include:
y=TRUE, y2=FALSE (default): y is the primary axis and the y2 ticks are aligned to the the ones of y. (only 1 grid)
y=FALSE, y2=TRUE: y2 is the primary axis and the y ticks are aligned to the the ones of y2. (only 1 grid)
y=TRUE, y2=TRUE: Both axis are independent and have their own ticks. (2 grids)
In this example we specify that the y2 axis has independent ticks (resulting in a more natural value scale for the axis labels). We also add a label to each Y axis.
dygraph(weather) %>%
dyAxis("y", label = "Temperature (C)") %>%
dyAxis("y2", label = "Rainfall", independentTicks = TRUE) %>%
dySeries("rainfall", axis = 'y2')