标签: r-plotly

更改绘图悬停框 R 的位置

我想更改plotly条形图中悬停框的位置。我希望它在栏上方弹出,而不是在左侧或右侧弹出。这可能吗?我一直在谷歌上搜索这个并盯着 Plotly R 参考页面几个小时,但没有运气。

这是一个示例:

library(dplyr)
library(plotly)
data.frame(x = 1:10, y = 1:10*10) %>% 
  plotly::plot_ly(data = .) %>%
  plotly::add_trace(x = ~x, 
                    y = ~y, 
                    type = 'bar', 
                    hoverinfo = "text", 
                    text = ~y)
Run Code Online (Sandbox Code Playgroud)

r plotly r-plotly

5
推荐指数
0
解决办法
747
查看次数

R - 绘图 - 删除地图拉伸

我正在尝试使用plotly 制作交互式ggplot2 地图。

在下面的代码中,我创建了一张法国地图,每个部门都有不同的颜色:

library(tidyverse)
library(stringr)
library(stringi)
library(maps)
library(ggplot2)

map <- map_data("france")

map_theme <- theme(title=element_text(),
                   plot.title=element_text(margin=margin(20,20,20,20), size=18, hjust = 0.5),
                   axis.text.x=element_blank(),
                   axis.text.y=element_blank(),
                   axis.ticks=element_blank(),
                   axis.title.x=element_blank(),
                   axis.title.y=element_blank(),
                   panel.grid.major= element_blank(), 
                   panel.background= element_blank()) 

p = ggplot(map, aes(long,lat, group = group, fill = region)) +
  geom_polygon() +
  coord_map() +
  theme(legend.position = "none") +
  map_theme

plot(p)
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)

plot(p) 和 ggplotly(p) 会给出类似的图表,但用plotly提供的图表看起来被拉伸了。地图有问题。避免这种情况的最佳策略是什么?

r ggplot2 r-plotly

5
推荐指数
1
解决办法
559
查看次数

Plotly:如何在箱形图上添加中线

我想在箱形图上添加中线的痕迹。

像这样

在此输入图像描述

到目前为止,这是我的情节:

在此输入图像描述

library(plotly)
p <- plot_ly(y = ~rnorm(50), type = "box") %>%
  add_trace(y = ~rnorm(50, 1))

p
Run Code Online (Sandbox Code Playgroud)

r boxplot plotly r-plotly

5
推荐指数
1
解决办法
5080
查看次数

记录数据上的箱形图中的对数刻度

我想创建一个具有对数刻度和根据记录数据计算的统计数据的箱线图(以及可能的其他图)。以下示例显示了逻辑。

数据如下:

d1 <- data.frame(x = rchisq(1000, 2), mod = c(rep('a', 500), rep('b', 500)))
Run Code Online (Sandbox Code Playgroud)

如果我预先转换数据,我会获得 y 轴上对数值的图。

plot_ly(d1, y = ~log10(x), color = ~mod, type = 'box')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果我在创建箱线图后转换 y 轴,我会得到一个箱线图,其中包含原始数据的须线长度和中位数以及 y 轴上对数刻度的原始数据。

plot_ly(d1, y = ~x, color = ~mod, type = 'box') %>%
  layout(yaxis = list(type = "log", showgrid=T,  ticks="outside", autorange=TRUE))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我理想的结果是上面两张图的组合 - 第一张图片的箱线图和第二张图片的比例。它应该看起来像可以在 ggplot 中执行的操作:

d1 %>% ggplot(aes(y=x, alpha = 0.1, color = mod, fill = mod))+
  geom_boxplot()+
  scale_y_log10()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我尝试使用 ggploly 将 ggplot 修改为plotly,但它丢失了比例并将其更改为第一张图片的比例。

任何人都可以帮助用plotly制作这样的图,或者如何用ggplotly保留y轴上的对数比例?

scale ggplotly r-plotly

5
推荐指数
1
解决办法
2551
查看次数

r-plotly 筛选按钮显示所有数据

我正在尝试使用 r-plotly 制作交互式图表。我希望有一个按钮可以按数据中指定的组进行过滤。在下面的示例中,该按钮用于将数据过滤到组 1 和组 2,但“所有组”按钮不会显示所有数据。有人有建议吗?

df <- tibble(x = c(1, 4, 2, 7), y = c(3, 1, 8, 4), group = c(2, 1, 1, 2))

fig <- plot_ly(data = df, type = "scatter", mode = "markers") %>%
  add_trace(type = "scatter", mode = "markers", x=~x, y=~y,
            transforms = list(
              list(
                type = "filter",
                target = ~group,
                operation = 'in',
                value = unique(df$group)
              )
            )
  ) %>%
  layout(
    updatemenus = list(
      list(
        type = 'dropdown',
        active = 0,
        buttons = list( …
Run Code Online (Sandbox Code Playgroud)

r r-plotly

5
推荐指数
1
解决办法
1053
查看次数

使用按时间范围过滤的共享数据创建交互式条形图

我想创建一个交互式条形图,让用户可以根据一系列值过滤观察结果,然后动态呈现所选时间段内每个类的计数。由于过滤后的数据需要可用于许多这样的图表,我认为串扰plotly/ggplot的组合可能会证明是有价值的。

我在下面附加了一个 reprex,它使用共享数据和来自串扰的过滤功能来允许动态过滤部分。当我编织文档时,只要选择了全部范围的值(默认值),条形图就会很好地呈现。

满的

但是,绘图区域对于任何其他区域都为空,即。用户调整范围。

部分的

我到底错过了什么?我认为ggplotly()无法处理的完整共享数据集和过滤共享数据集之间一定存在差异。也许我可以遵循另一种方法来实现我的目标?

这是我的 .Rmd 文件的内容:

---
title: mpg class counts filtered by time period
output: html_document
---

```{r echo = FALSE, message = FALSE, warning = FALSE}
library(crosstalk)
library(plotly)

# Wrap data frame in SharedData
sd = SharedData$new(mpg)

# Create a filter input
filter_slider("Year", "Year", sd, column = ~ year, step = 1, width = 250)

# Render graph
bscols(
  ggplotly(
    ggplot(aes(x = class), data = sd) + 
      geom_bar() …
Run Code Online (Sandbox Code Playgroud)

visualization r ggplotly r-plotly crosstalk

5
推荐指数
1
解决办法
889
查看次数

如何将子图应用于具有辅助 y 轴的绘图列表

我想准备一个子图,其中每个方面都是一个变量相对于其他变量的单独的双 y 轴图。因此,我制作了一个基本图p并在循环中添加辅助 y 轴变量:

library(rlang)
library(plotly)
library(tibble)

dual_axis_lines <- function(data, x, y_left, ..., facets = FALSE, axes = NULL){
  x <- rlang::enquo(x)
  y_left <- rlang::enquo(y_left)
  y_right <- rlang::enquos(...)
  
  y_left_axparms <- list(
    title = FALSE,
    tickfont = list(color = "#1f77b4"),
    side = "left")
  y_right_axparms <- list(
    title = FALSE,
    overlaying = "y",
    side = "right",
    zeroline = FALSE)
  
  p <- plotly::plot_ly(data , x = x) %>%
    plotly::add_trace(y = y_left, name = quo_name(y_left),
                      yaxis = "y1", type = 'scatter', …
Run Code Online (Sandbox Code Playgroud)

r plotly r-plotly

5
推荐指数
1
解决办法
941
查看次数

如何更改 R 中 ggplotly 中的图例位置

下面的代码使用ggplot和生成两个图ggplotly。尽管使用了layout()ggplotly,图例仍然位于右侧。图例必须位于底部。任何人都可以帮助将图例移动到 ggplotly 的底部吗?我已经尝试了 R +shiny+plotly 的解决方案:ggplotly 将图例移至右侧,但在这里不起作用。如果我错过了显而易见的事情,有人可以帮忙吗?

measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score)


ui <- fluidPage(
  mainPanel(
    plotOutput("myplot" ),
    plotlyOutput("myplot2" )
  )
)

server <- function(input, output) {
  myplot <- reactive({
    gpl1 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity")+
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    gpl1
  })
  
  myplot2 <- reactive({
    gpl2 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity") +
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    ggplotly(gpl2) %>% 
      layout(legend = list(orientation = 'h', …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggplotly r-plotly

5
推荐指数
1
解决办法
3637
查看次数

如何根据串扰条件动态更改绘图轴

这个问题之前已经被问过,但由于没有reprex而没有得到答案,所以让我试一下。

假设我有两个跨越不同日期范围的数据集。我想使用滑块控制每个的可视化。以下 reprex 将直接在下面创建视觉效果。

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

#+ message = FALSE, warning = FALSE
library(plotly)
library(crosstalk)
library(dplyr)
#+
```

```{r}
df1 <- data.frame(d = seq.Date(from = as.Date("2020-01-01"), by = "months", length.out = 100), v = runif(100))
df2 <- data.frame(d = seq.Date(from = as.Date("2020-6-01"), by = "months", length.out = 20), other_v = runif(20))

both_df <- full_join(df1, df2, by = 'd')

both_df_sh <- both_df %>% SharedData$new(group = "boom")

selector <- filter_slider(id …
Run Code Online (Sandbox Code Playgroud)

r plotly r-plotly crosstalk

5
推荐指数
1
解决办法
1941
查看次数

Rplotly:自定义悬停(信息和文本)

我想自定义当我将鼠标悬停在栏上时我在情节中看到的内容。

请查看帖子末尾的 reprex。我看了一下

如何设置不同的文本和hoverinfo文本

https://community.rstudio.com/t/changing-hovertext-in-plotly/71736

但我一定犯了一些错误。我希望当我将鼠标悬停在栏上时只看到变量“macro_sector”和“amount”,而栏上根本没有静态文本。我怎样才能做到这一点?非常感谢

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> …
Run Code Online (Sandbox Code Playgroud)

r plotly r-plotly

5
推荐指数
1
解决办法
3597
查看次数

标签 统计

r-plotly ×10

r ×9

plotly ×5

ggplotly ×3

crosstalk ×2

ggplot2 ×2

boxplot ×1

scale ×1

visualization ×1