我有一個非常簡單的繪圖圖,我想將公司徽標代替(或兩者)放在 Y 軸上
library(plotly)
plot_ly(
y = c("company_1", "company_2", "company_3", "company_4", "company_5"),
x = c(20, 14, 23, 6, 28),
name = "companies",
type = "bar"
)
例如,我們可以想象這些公司是 Facebook、Twitter、Apple、Microsoft 和 Tesla,我有 png 格式的影像(徽標)。
有沒有辦法做到這一點?
uj5u.com熱心網友回復:
這不是您在 R 中可以嚴格執行的操作。但是,如果您使用函式onRenderfrom htmlwidgets,則可以執行此操作。
我添加了margin這樣的影像將適合。但是,情節與您提供的完全一樣。確保如果/當您進行更改時,您尊重此處顯示的空格。如果屬性被砸在一起,HTML 和 Javascript 將無法理解。
第一個代碼和繪圖使用網路上的影像。下一個代碼和繪圖使用本地檔案。這樣您就可以使用適用于您的情況的任何方法。
圖片來自超鏈接
plot_ly(
y = c("company_1", "company_2", "company_3", "company_4", "company_5"),
x = c(20, 14, 23, 6, 28),
name = "companies",
type = "bar") %>%
layout(margin = list(l = 120, r = 20, t = 20, b = 50)) %>%
htmlwidgets::onRender("function(){
gimme = document.querySelectorAll('g.ytick'); /* get the text positions on y-axis */
imTag1 = '<image width=\"100\" height=\"100\" ' /* pre-build the HTML */
imTag2 = ' href=\"https://www.rstudio.com/wp-content/uploads/2018/10/RStudio-Logo-Flat.png\" />'
for(i = 0; i < gimme.length; i ) {
zz = gimme[i].firstChild.getAttribute('transform'); /* get position on page*/
yy = gimme[i].firstChild.getAttribute('y');
yy = yy * -10;
img = imTag1 'x=\"10\" y=\"' yy '\" transform=\"' zz '\"' imTag2;
gimme[i].innerHTML = img; /* replace the text tag */
}
}")

圖片來自本地檔案
我在這里使用的圖片來自
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523548.html
標籤:r图片情节地y轴
