使用 ggplotly 添加徽标?
Add logo with ggplotly?
我正在与 ggplotly
合作,而不是直接与 plot_ly
合作。我正在尝试按照发现的说明在图表上添加徽标 here,但似乎没有任何反应。
注意:我使用ggplotly
是因为在我使用的实际数据中更容易操作长轴标签。
这是我的代码:
ggplotly(ggplot(mtcars) +
geom_point(aes(mpg, disp)),
images = list(
source = "https://www.rstudio.com/wp-content/uploads/2018/10/
RStudio-Logo-Flat.png"))
同样的效果我也试过
ggplotly(ggplot(mtcars) +
geom_point(aes(mpg, disp))) %>%
layout(
images = list(
source = "https://www.rstudio.com/wp-content/uploads/2018/10/
RStudio-Logo-Flat.png",
x = 0, y = 1,
sizex = 0.2, sizey = 0.1,
xref = "paper", yref = "paper",
xanchor = "left", yanchor = "bottom"
),
margin = list(t = 50)
)
最后,我还尝试根据 here 中的示例使用 onRender
:
ggplotly(ggplot(mtcars) +
geom_point(aes(mpg, disp))) %>%
htmlwidgets::onRender("
function(el, x) {
// when hovering over an element, do something
el.on('plotly_hover', function(d) {
// extract tooltip text
txt = d.points[0].data.text;
// image is stored locally
image_location = 'https://www.rstudio.com/wp-content/uploads/2018/10/
RStudio-Logo-Flat.png';
// define image to be shown
var img = {
// location of image
source: image_location,
// top-left corner
x: 1,
y: 1,
sizex: 0.2,
sizey: 0.2,
xref: 'paper',
yref: 'paper'
};
// show image and annotation
Plotly.relayout(el.id, {
images: [img]
});
})
}
")
在右上角添加徽标有点必要,所以我希望有一个简单的修复方法。
您可以在 www 文件夹中保留图像文件的副本,例如 YBS.png,然后您可以将其指向
layout(
images = list(
source = base64enc::dataURI(file = "YBS.png"),
x = 0.9, y = 1,
sizex = 0.2, sizey = 0.1,
xref = "paper", yref = "paper",
xanchor = "left", yanchor = "bottom"
))
这给出了下图:
您可以在 0 和 1 之间更改 x 的值,以将图像移动到所需位置。您将需要安装 base64enc 包。
我正在与 ggplotly
合作,而不是直接与 plot_ly
合作。我正在尝试按照发现的说明在图表上添加徽标 here,但似乎没有任何反应。
注意:我使用ggplotly
是因为在我使用的实际数据中更容易操作长轴标签。
这是我的代码:
ggplotly(ggplot(mtcars) +
geom_point(aes(mpg, disp)),
images = list(
source = "https://www.rstudio.com/wp-content/uploads/2018/10/
RStudio-Logo-Flat.png"))
同样的效果我也试过
ggplotly(ggplot(mtcars) +
geom_point(aes(mpg, disp))) %>%
layout(
images = list(
source = "https://www.rstudio.com/wp-content/uploads/2018/10/
RStudio-Logo-Flat.png",
x = 0, y = 1,
sizex = 0.2, sizey = 0.1,
xref = "paper", yref = "paper",
xanchor = "left", yanchor = "bottom"
),
margin = list(t = 50)
)
最后,我还尝试根据 here 中的示例使用 onRender
:
ggplotly(ggplot(mtcars) +
geom_point(aes(mpg, disp))) %>%
htmlwidgets::onRender("
function(el, x) {
// when hovering over an element, do something
el.on('plotly_hover', function(d) {
// extract tooltip text
txt = d.points[0].data.text;
// image is stored locally
image_location = 'https://www.rstudio.com/wp-content/uploads/2018/10/
RStudio-Logo-Flat.png';
// define image to be shown
var img = {
// location of image
source: image_location,
// top-left corner
x: 1,
y: 1,
sizex: 0.2,
sizey: 0.2,
xref: 'paper',
yref: 'paper'
};
// show image and annotation
Plotly.relayout(el.id, {
images: [img]
});
})
}
")
在右上角添加徽标有点必要,所以我希望有一个简单的修复方法。
您可以在 www 文件夹中保留图像文件的副本,例如 YBS.png,然后您可以将其指向
layout(
images = list(
source = base64enc::dataURI(file = "YBS.png"),
x = 0.9, y = 1,
sizex = 0.2, sizey = 0.1,
xref = "paper", yref = "paper",
xanchor = "left", yanchor = "bottom"
))
这给出了下图:
您可以在 0 和 1 之间更改 x 的值,以将图像移动到所需位置。您将需要安装 base64enc 包。