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) 我在 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)
如果我删除工具提示,则回归线出现在输出图中,但包含工具提示后,回归线不会出现在图中。有什么解决方案可以将两者结合在一起吗?
考虑下面的简单示例。有没有一种方法可以格式化绘图工具提示,以便长文本标签在框中可见,而不是这个截断值的荒谬矩形?
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)
我试图为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) 当我尝试在 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 中创建了以下数据框和关联的 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 时,我们得到一个带有两个 …
我想创建一个具有对数刻度和根据记录数据计算的统计数据的箱线图(以及可能的其他图)。以下示例显示了逻辑。
数据如下:
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轴上的对数比例?
我希望使用plotly 使ggplot 对象具有交互性。当我使用 时ggplotly(),我的面之间会引入巨大的空白,使绘图变得很小且看起来很奇怪:
与仅使用时的大小和整洁相比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)
下面的代码使用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) 我试图使用 ggplotly 函数来交互地使用 buraR 包中的trace_explorer 函数创建一个绘图,但生成的绘图不是预期的。
\n这是代码:
\nlibrary(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)\nRun Code Online (Sandbox Code Playgroud)\n和结果图
\n\n我尝试使用 ggplot2 中的主题选项,然后使用布局函数,但结果仍然相同,没有图例。
\nggtrace <- 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)