在 R 中的 onRender() htmlWidgets 上更改散点图中的线条粗细和不透明度 [英] Changing line thickness and opacity in scatterplot on onRender() htmlWidgets in R

查看:24
本文介绍了在 R 中的 onRender() htmlWidgets 上更改散点图中的线条粗细和不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 R 包 htmlwidgets 的 onRender() 函数制作一个图,其中用户可以单击一个点并绘制一条线.我现在的关键是它的工作原理是一条灰线以其默认粗细和可能的默认不透明度绘制.

I am hoping to make a plot using the R package htmlwidgets' onRender() function in which a user can click on a point and a line is drawn. I have the crux of it working right now where a gray line is drawn at its default thickness and probably its default opaqueness.

但是,我一直坚持更改线条的粗细(并且可能会更改线条的不透明度,尽管它可能正在工作并且我看不到它,因为线条太细了).我希望线条非常粗且相当透明.我尝试了几种线宽和不透明度的参数和方法(其中一些在下面注释掉了),但似乎它们并没有什么区别.有什么想法我可能会错过吗?谢谢你.

However, I have been stuck on changing the thickness of the line (and possibly changing the opaqueness of the line, although it may be working and I cannot see it since the line is so thin). I want the line to be very thick and rather transparent. I tried several parameters and approaches for line width and opaqueness (some of which are commented out below), but it seems they do not make a difference. Any ideas what I may be missing? Thank you.

library(plotly)
library(broom)

dat <- mtcars
dat$mpg <- dat$mpg * 10

p <- ggplot(data = dat, aes(x=disp,y=mpg)) + geom_point(size=0.5)

ggplotly(p) %>%
  onRender("
           function(el, x, data) {
            // reduce the opacity of every trace except for the hover one
            el.on('plotly_click', function(e) {

              var trace1 = {
                 x: [100, 400],
                 y: [100, 400],
                 mode: 'lines',
                 //line: dict(color: 'gray', width: 100)
                 marker: {
                   color: 'gray',
                   size: 200,
                   width: 1000,
                   opacity: 0.5
                 }
              }
              Plotly.addTraces(el.id, trace1);
           })
          }
          ", data=dat)

推荐答案

opacity 需要在跟踪对象中.您的 line 对象存在一些语法问题,导致 Javascript 无法读取它.

The opacity would need to be in the trace object. Your line object has some syntax issues which prevents Javascript from reading it.

gp %>% onRender("
function(el, x, data) {

  el.on('plotly_click', function(e) {

  var trace1 = {
    x: [100, 400],
    y: [100, 400],
    mode: 'lines',
    line: {
        color: 'gray', 
        width: 100
    },
    opacity: 0.8,
  }
  Plotly.addTraces(el.id, trace1);
})
}", data=dat)

这篇关于在 R 中的 onRender() htmlWidgets 上更改散点图中的线条粗细和不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆