Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何创造,;使用拆分参数时,在绘图中设置图例格式_R_Data Visualization_Plotly - Fatal编程技术网

R 如何创造,;使用拆分参数时,在绘图中设置图例格式

R 如何创造,;使用拆分参数时,在绘图中设置图例格式,r,data-visualization,plotly,R,Data Visualization,Plotly,我正在尝试使用split参数在plot_!中创建散点图,但是在图例和格式方面遇到了一些困难。特别是,一旦我将标签添加到图表中,图例中会添加更多的项目,颜色不匹配,并且我无法通过单击关闭整个系列。下面是一个例子: library(plotly) 地块(mtcars、, type=“散布”, x=~hp, y=~qsec, 分裂=共青团, text=行名(mtcars))%>% 添加文本(textposition=“右中”) 运行此代码时,您可以看到标签确实为每个点显示,但它们以随机颜色显示。此外

我正在尝试使用split参数在plot_!中创建散点图,但是在图例和格式方面遇到了一些困难。特别是,一旦我将标签添加到图表中,图例中会添加更多的项目,颜色不匹配,并且我无法通过单击关闭整个系列。下面是一个例子:

library(plotly)
地块(mtcars、,
type=“散布”,
x=~hp,
y=~qsec,
分裂=共青团,
text=行名(mtcars))%>%
添加文本(textposition=“右中”)
运行此代码时,您可以看到标签确实为每个点显示,但它们以随机颜色显示。此外,图例有6项。我想要的是:

  • 标签应与其系列/拆分(编号)的颜色相匹配 (钢瓶数量)
  • 图例包含3项(4、6和8)
  • 当你点击 在图例中的一个项目上,应同时关闭/打开 该系列/拆分的标记和文本标签
  • 非常感谢你的帮助

    如果有帮助:

    • R:3.3.1
    • 详细内容:4.5.6

    使用类型
    分散时,需要设置
    模式
    。使用
    标记+文本
    选项,我相信您没有能力单独为文本着色。如果您不介意文本为灰色,解决方案是:

    plot_ly(mtcars,
            type = "scatter",
            x = ~hp,
            y = ~qsec,
            split = ~cyl,
            mode = "markers+text",
            text = rownames(mtcars),
            textposition = "middle right") 
    
    如果您想让它100%匹配您的请求,它会变得更复杂,并且您不能使用
    split
    参数

    您必须为每个柱面级别创建单独的记录道,首先使用
    mutate
    明确包含行名称。然后使用
    filter
    对每个气缸液位的
    mtcars
    进行子集设置。为每个标高创建2个迹线、一个标记和一个文字。然后,
    颜色
    将等于
    cyl
    的因子级别。最后,您必须对图例中每个圆柱体的轨迹进行分组,隐藏不需要的条目

    library(plotly)
    library(dplyr)    
    
    plot_ly(filter(mutate(mtcars, names = rownames(mtcars)), cyl == 4),
            type = "scatter",
            x = ~hp,
            y = ~qsec,
            mode = "markers",
            color = ~factor(cyl, c(4,6,8)),
            legendgroup = '4',
            name = '4',
            textposition = "middle right") %>% 
      add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 4),
                type = "scatter",
                x = ~hp+2,
                y = ~qsec,
                color = ~factor(cyl, c(4,6,8)),
                mode = "text",
                text = ~names, 
                legendgroup = '4',
                name = '4',
                showlegend = FALSE,
                textposition = "middle right") %>% 
      add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 6),
              type = "scatter",
              x = ~hp,
              y = ~qsec,
              mode = "markers",
              color = ~factor(cyl, c(4,6,8)),
              legendgroup = '6',
              name = '6',
              textposition = "middle right") %>% 
      add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 6),
                type = "scatter",
                x = ~hp+2,
                y = ~qsec,
                mode = "text",
                color = ~factor(cyl, c(4,6,8)),
                text = ~names, 
                legendgroup = '6',
                name = '6',
                showlegend = FALSE,
                textposition = "middle right") %>% 
      add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 8),
                type = "scatter",
                x = ~hp,
                y = ~qsec,
                mode = "markers",
                color = ~factor(cyl, c(4,6,8)),
                legendgroup = '8',
                name = '8',
                textposition = "middle right") %>% 
      add_trace(data = filter(mutate(mtcars, names = rownames(mtcars)), cyl == 8),
                type = "scatter",
                x = ~hp+2,
                y = ~qsec,
                mode = "text",
                color = ~factor(cyl, c(4,6,8)),
                text = ~names, 
                legendgroup = '8', 
                name = '8',
                showlegend = FALSE,
                textposition = "middle right")
    

    目前,plot_y无法做到这一点,为了实现这一功能,我编辑了我的原始答案,以展示如何实现这一点。干得好!您可以为标记跟踪保留
    text=“~names”
    ,为文本跟踪添加
    hoverinfo=“none”
    ,以保持默认外观