Skip to content

Commit c08a781

Browse files
Fix font size and line width in plotly theme
1 parent 59fda9a commit c08a781

File tree

1 file changed

+86
-32
lines changed

1 file changed

+86
-32
lines changed

R/plotly_theme.R

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
iris <- iris |> dplyr::mutate(Sepal.Length = Sepal.Length * 100)
2+
13
fig <- plotly::plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
24

35
fig
46

5-
fig |>
7+
fig |>
68
theme_af_plotly()
79

10+
811
theme_af_plotly <- function(chart,
912
base_size = 14,
10-
base_line_size = base_size / 24,
1113
grid = c("y", "x", "xy", "none"),
1214
axis = c("x", "y", "xy", "none"),
1315
ticks = c("xy", "x", "y", "none"),
1416
legend = c("right", "left", "top", "bottom", "none")) {
1517

18+
1619
grid <- match.arg(grid)
1720
axis <- match.arg(axis)
1821
ticks <- match.arg(ticks)
@@ -25,6 +28,7 @@ theme_af_plotly <- function(chart,
2528
afcharts_font <- "Arial"
2629

2730
half_line <- base_size / 2
31+
base_line_size = base_size / 24
2832

2933
# Set grid lines dependent on grid arg
3034
grid_x <- if (grid %in% c("x", "xy")) TRUE else FALSE
@@ -38,78 +42,115 @@ theme_af_plotly <- function(chart,
3842
ticks_x <- if (ticks %in% c("x", "xy")) "outside" else ""
3943
ticks_y <- if (ticks %in% c("y", "xy")) "outside" else ""
4044

45+
46+
y_title <- plotly::plotly_build(chart)$x$layout$yaxis$title |> unclass()
47+
x_title <- plotly::plotly_build(chart)$x$layout$xaxis$title |> unclass()
48+
4149
chart |>
4250
plotly::layout(
4351
font = list(
44-
colour = "black",
45-
size = base_size,
52+
color = "black",
53+
size = base_size * ( 96 / 72),
4654
family = afcharts_font
4755
),
4856

49-
margin = list(
50-
l = 80,
51-
b = 80,
52-
r = 80,
53-
t = 0
54-
),
57+
# margin = list(
58+
# l = 80,
59+
# b = 80,
60+
# r = 80,
61+
# t = 0
62+
# ),
63+
64+
# # title
65+
# title = list(
66+
# text = "This is the title",
67+
# font = list(
68+
# size = 1.6 * base_size
69+
# ),
70+
# xref = "paper",
71+
# yref = "container",
72+
# xanchor = "left",
73+
# yanchor = "top",
74+
# x = 0,
75+
# y = 1,
76+
# automargin = TRUE,
77+
# pad = list(b = ((half_line * 2) + base_size) * (72 / 96))
78+
# ),
5579

56-
# title
57-
title = list(
58-
text = "This is the title",
59-
font = list(
60-
size = 1.6 * base_size
61-
),
62-
xref = "paper",
63-
xanchor = "left",
64-
yanchor = "top",
65-
x = 0,
66-
y = 1,
67-
pad = list(
68-
t = half_line * 2 * (96 / 72)
69-
)
70-
),
7180

7281
# x axis
82+
7383
xaxis = list(
84+
7485
fixedrange = TRUE,
7586

87+
# title
88+
title = list(
89+
text = x_title,
90+
font = list(
91+
color = "black",
92+
size = base_size * (96 / 72),
93+
family = afcharts_font
94+
)
95+
),
96+
97+
7698
# axis
7799
showline = axis_x,
78100
linecolor = light_grey,
79-
linewidth = base_line_size * (96 / 72),
101+
linewidth = base_line_size * 3.7795,
80102

81103
# ticks
82104
ticks = ticks_x,
83105
tickcolor = light_grey,
84106
ticklen = half_line / 2 * (96 / 72),
85-
tickwidth = base_line_size * (96 / 72),
107+
tickwidth = base_line_size * 3.7795,
86108
tickangle = 0,
87109

88110
# grid
89111
showgrid = grid_x,
90-
gridwidth = base_line_size * (96 / 72),
112+
gridwidth = base_line_size * 3.7795,
91113
gridcolor = light_grey
92114
),
93115

94116
# y axis
117+
annotations = list(
118+
xref = "paper",
119+
xanchor = "left",
120+
x = 0,
121+
yref = "paper",
122+
yanchor = "bottom",
123+
y = 1,
124+
text = y_title,
125+
showarrow = FALSE,
126+
font = list(
127+
size = base_size * ( 96 / 72)
128+
)
129+
),
130+
95131
yaxis = list(
96132
fixedrange = TRUE,
97133

134+
# title
135+
title = list(
136+
text = NA
137+
),
138+
98139
# axis
99140
showline = axis_y,
100141
linecolor = light_grey,
101-
linewidth = base_line_size * (96 / 72),
142+
linewidth = base_line_size * 3.7795,
102143

103144
# ticks
104145
ticks = ticks_y,
105146
tickcolor = light_grey,
106147
ticklen = half_line / 2 * (96 / 72),
107-
tickwidth = base_line_size * (96 / 72),
148+
tickwidth = base_line_size * 3.7795,
108149
tickangle = 0,
109150

110151
# grid
111152
showgrid = grid_y,
112-
gridwidth = base_line_size * (96 / 72),
153+
gridwidth = base_line_size * 3.7795,
113154
gridcolor = light_grey
114155
),
115156

@@ -118,7 +159,8 @@ theme_af_plotly <- function(chart,
118159
bgcolor = "white",
119160
font = list(
120161
color = "black",
121-
size = base_size
162+
size = base_size * (96 / 72),
163+
family = afcharts_font
122164
)
123165
)
124166
) |>
@@ -129,3 +171,15 @@ theme_af_plotly <- function(chart,
129171
)
130172

131173
}
174+
175+
176+
177+
fig |>
178+
theme_af_plotly()
179+
180+
library(ggplot2)
181+
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) +
182+
geom_point() +
183+
theme_af() +
184+
labs(title = "This is the title")
185+

0 commit comments

Comments
 (0)