标签: ggplotly

R: facet_wrap does not render correctly with ggplotly in Shiny app

When I do a facet_grid in ggplotly() for a Shiny App, with a large number of faceting groups, the plot is messed up. However it works correctly outside Shiny.

How can I fix this?
I suspect it is linked to the Y scale but I couldn't find the solution.


Here's a reproducible example based on diamonds example from plotly.

Comparison of Shiny vs non Shiny outputs : Comparison of facet_grid outside and within Shiny

Code

Outside Shiny:

library(ggplot2)

data(diamonds, …
Run Code Online (Sandbox Code Playgroud)

r shiny facet-grid ggplotly

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

工具提示删除回归线 ggplotly

我在 server.R 中有以下代码:

library(shiny)
library(plotly)

art.data <- read.csv("data1.csv", stringsAsFactors = FALSE)

shinyServer(function(input, output) {

output$distPlot <- renderPlotly({
    col.str <- paste0(input$colspa, ".", input$rgbchoice, ".median")
    p <- ggplot(art.data, aes(x = year, y = art.data[[col.str]], text = paste0(artist, "<br>", art))) + geom_point(size = 1) + xlab("Year") + stat_smooth(method = loess, se = FALSE)
    ggplotly(p , tooltip = "text")
})
})
Run Code Online (Sandbox Code Playgroud)

如果我删除工具提示,则回归线出现在输出图中,但包含工具提示后,回归线不会出现在图中。有什么解决方案可以将两者结合在一起吗?

带工具提示

ggplotly() 中没有工具提示,aes() 中没有文本

r ggplot2 shiny plotly ggplotly

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

在绘图中设置长文本标签的工具提示格式

考虑下面的简单示例。有没有一种方法可以格式化绘图工具提示,以便长文本标签在框中可见,而不是这个截断值的荒谬矩形?

library(ggplot2); library(plotly)

df <- data.frame(x = 1:10, y = 1:10, z = rep("the longggggggggggggggggggggggggggggestttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt labelllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll you can imagineeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", 10))

p <- ggplot(df, aes(x,y,label=z)) + geom_point()
ggplotly(p, tooltip = "label")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r ggplot2 plotly ggplotly

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

格式化闪亮的 Plotly 子图 - 单独的标题和图形大小

我试图为plotly'ssubplot函数中的每个图表提供单独的标题,我发现了一篇文章,您可以使用它来扩展子图,%>% layout(title = "Main Title)但我想为每个图表提供单独的标题(我正在使用,ggtitle但只绘制最后一个标题提供(图 4)。我发现了类似的帖子为每个子图提供标题 - R Shiny但我不认为我可以facet_wrap在我的场景中。

此外 - 我想知道如何增加子图中图表之间的边距,因为它们似乎真的被压在一起了。

任何帮助表示赞赏!

在此输入图像描述

ui <- fluidPage(
  sidebarPanel("This is a sidebar"),
  mainPanel(plotlyOutput("myplot"))
    )

server <- function(input, output){

  output$myplot <- renderPlotly({

    gg1 <- ggplotly(
      ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
        geom_point() +
        theme_minimal() +
        ggtitle("Plot 1")
    )

    gg2 <- ggplotly(
      ggplot(iris, aes(x=Species, y=Sepal.Length)) +
        geom_boxplot() +
        theme_minimal() +
        ggtitle("Plot 2")
    )

    gg3 <- ggplotly(
      ggplot(iris, aes(x=Petal.Width)) +
        geom_histogram() +
        ggtitle("Plot 3")
    ) …
Run Code Online (Sandbox Code Playgroud)

r shiny plotly ggplotly

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

ggplot 到 ggplotly 不适用于自定义 geom_boxplot 宽度

当我尝试在 ggplot 中为箱线图设置自定义宽度时,它工作正常:

p=ggplot(iris, aes(x = Species,y=Sepal.Length )) + geom_boxplot(width=0.1)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用 ggplotly 时,宽度(和 hjust)是默认的:

p %>% ggplotly()
Run Code Online (Sandbox Code Playgroud)

我做错了什么或者这是 ggplotly 中的错误?

r ggplot2 boxplot plotly ggplotly

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

第二个 y 轴在 ggplotly 命令上消失,dynamick 刻度 =true,ggplot2

我在 R 中创建了以下数据框和关联的 ggplot 图表 首先我们使用 R 导入库

 library(plotly)
 library(ggplot2)
Run Code Online (Sandbox Code Playgroud)

接下来我们创建数据框如下

    dataframe_1<-data.frame("Month"=c(1:12))
    dataframe_1$Sales<-25*dataframe_1$Month
    dataframe_1$Fac1=dataframe_1$Sales/100
    dataframe_1$Month<-as.character(dataframe_1$Month)
Run Code Online (Sandbox Code Playgroud)

接下来我们创建一个基于 ggplot 的条形图和折线图,如下所示

   p<-ggplot(data = dataframe_1, mapping = aes(x = Month, y = Fac1))+geom_bar(data = dataframe_1, mapping = aes(x = Month, y = Sales/100, fill = "#82e600", text=paste0("Sales:", Sales)),stat="identity")+geom_line(mapping = aes(x = Month, y = Fac1, group = 1))+geom_point(mapping = aes(x = Month, y = Fac1, group = 1, text=paste0( "Factoid:", Fac1)),    inherit.aes = FALSE)+scale_y_continuous(sec.axis = sec_axis(~.*100, name = "Sales"))+labs(fill =  "Sales")
Run Code Online (Sandbox Code Playgroud)

当我们渲染图 p 时,我们得到一个带有两个 …

r ggplot2 ggplotly

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

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

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

数据如下:

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
查看次数

使用 ggplotly() 时删除面之间的空白

我希望使用plotly 使ggplot 对象具有交互性。当我使用 时ggplotly(),我的面之间会引入巨大的空白,使绘图变得很小且看起来很奇怪:

情节方面

与仅使用时的大小和整洁相比ggplot

ggplot 对象

我的可重现代码是:

library(plotly)
library(ggplot2)
library(reshape2)

p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  facet_wrap( ~ day, ncol=2)

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

不知道是我的电脑设置有问题还是我的代码有问题。我使用 R 版本 4.0.2、ggplot2 版本 3.3.2 和plotly 版本 4.9.2.1。有人帮助我保留大小并为我的情节添加交互性。

我已经尝试过Cam McMains在这里给出的答案,但没有任何改变。

更新

library(plotly)
library(ggplot2)
library(reshape2)

p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  theme(panel.spacing=unit(0,'npc'))+
  facet_wrap( ~ day, ncol=2)
  
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)

给予 更新

r facet ggplot2 ggplotly

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

如何更改 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
查看次数

Plotly 无法正确渲染绘图

我试图使用 ggplotly 函数来交互地使用 buraR 包中的trace_explorer 函数创建一个绘图,但生成的绘图不是预期的。

\n

这是代码:

\n
library(ggplot2)\nlibrary(bupaR)\n\npatients <- eventdataR::patients # dataset from bupaR\n\ndf <- eventlog(patients,\n               case_id = "patient",\n               activity_id = "handling",\n               activity_instance_id = "handling_id",\n               lifecycle_id = "registration_type",\n               timestamp = "time",\n               resource_id = "employee")\n\n\n\n\ntr <- df %>% processmapR::trace_explorer(type = "frequent", coverage = 1.0)\n\n# tr # print the ggplot to see the expected output!\nggplotly(tr)\n
Run Code Online (Sandbox Code Playgroud)\n

和结果图

\n

在此输入图像描述

\n

我尝试使用 ggplot2 中的主题选项,然后使用布局函数,但结果仍然相同,没有图例。

\n
ggtrace <- trace_explorer(df,\n                          type = "frequent", \n                          coverage = 1.0)\n\nggtrace <- ggtrace + \n  theme (legend.position="none") +\n …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 plotly ggplotly process-mining

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

标签 统计

ggplotly ×10

r ×9

ggplot2 ×7

plotly ×5

shiny ×3

r-plotly ×2

boxplot ×1

facet ×1

facet-grid ×1

process-mining ×1

scale ×1