我有一个带有简单散点图(例如地图)的 Rmarkdown,我希望用户能够通过输入提供一些任意x坐标y,并将这些坐标绘制在图表上(在下面的示例中以红色显示)。问题是,我没有闪亮的服务器,所以我不能依赖这个选项。是否有一个实现,例如,通过 javascript 或其他东西?
这就是我所拥有的:
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
library(ggplot2)
library(plotly)
```
```{r fig.height=4, fig.width=4}
X <- data.frame(x = 1:10, y = 1:10)
gg <- ggplot(X, aes(x, y)) + geom_point()
ggplotly(gg)
```
Run Code Online (Sandbox Code Playgroud)
这就是我正在寻找的:
编辑
上面的例子是一个简化。实际上,网格是360x240,坐标只能是整数。
编辑2 @JohanRosa 已经通过完全在plotly.js 上重建绘图提供了一个很好的答案。然而,我的 ggplot 实际上相当复杂,而且我有很多。因此,将它们重建到plotly.js 对我来说是相当复杂的。这就是我正在寻找一种可以直接在我拥有的 ggplot(ly) 上工作的解决方案的原因。
我试图在绘图散点图中更改颜色条的标题.
看一下文档,看起来这应该是微不足道的.但是,我已经尝试插入colorbar = list(title = "Typing Rate")主plotly()代码,并将其输入layout(),如下所示.
我该如何自定义此标题?
library(dplyr)
library(shiny)
library(plotly)
df <- as.data.frame(list("UserID"=c(1,1,1,1,2,2,2,2),
"Category"=c('A','A','B','B','A','A','B','B'),
"Rate"=c(2,3,5,6,8,6,7,1),
"x"=c(1,3,5,7,2,4,6,8),
"y"=c(1,3,5,7,2,4,6,8)
))
ui <- (fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("userInput","Select User", sort(unique(df$UserID)),
selected=1),
checkboxGroupInput("CategoryInput", "Select Category", sort(unique(df$Category)),
selected=c('A','B'))
),
mainPanel(
plotlyOutput("mainPlot")#,
)
)
))
server <- function(input, output, session) {
# filter for both user and category
filteredFull <- reactive({
df %>%
filter(
UserID == input$userInput,
Category %in% input$CategoryInput
)
})
output$mainPlot <- renderPlotly({
plot_ly(data=filteredFull(), x=x, y=y,
color=Rate, size=Rate, …Run Code Online (Sandbox Code Playgroud) 我不确定这是否可行,但这是我想做的.我想plotly通过从下拉菜单中选择来更新绘图中的数据.
举个简单的例子,假设我有一个数据框
df <- data.frame(x = runif(200), y = runif(200), z = runif(200))
Run Code Online (Sandbox Code Playgroud)
从中我使用df$x并df$y在散点图中.我希望使用下拉列表实现两种数据操作方案:
df$y为df$zn值df$xdf$y我查看了以下两个示例,我可以轻松地重现这些示例:https: //plot.ly/r/dropdowns/
但是,我不知道如何根据下拉选择传递有关要绘制的数据的信息.对于方案2,例如我尝试过它args = list("data", df[1:n,])并不起作用.
对于场景1,(仅?)方式(根据示例)似乎分别隐藏/显示跟踪.这是方案2的唯一方法吗?
还有其他想法吗?
更新1:添加可重现的示例
所以这是一个实现方案1中我想要的例子.
require(plotly)
df <- data.frame(x = runif(200), y = runif(200), z = runif(200))
Sys.setenv("plotly_username"="xxx") #actual credentials replaced
Sys.setenv("plotly_api_key"="xxx") #actual credentials replaced
p <- plot_ly(df, x = df$x, y = df$y, mode = "markers", name = "A", visible …Run Code Online (Sandbox Code Playgroud) 我对日期格式有疑问plotly.我制作了一个时间序列图ggplot2,我试图用图表进行可视化,但是悬停时出现了日期时间的格式问题(见图).我想要日期格式YYMMD-hh:mm.我怎么能得到这种格式?
我脚本上的相关R代码:
library(lubridate)
datosO3.melt <- melt(datosO3.plot, id.vars="fecha", value.name="value")
ozono.plot <- ggplot() + geom_line(data=datosO3.melt, aes(x=fecha, y=value, colour=variable))
ggplotly(ozono.plot)
Run Code Online (Sandbox Code Playgroud)
关键是数据框中的列fecha是在先前数据帧中创建的日期时间对象.
datosO3<-within(datosO3, fecha.hora <- ymd_hm(paste(datosO3$AAMMDD,datosO3$hhmm,sep="")))
Run Code Online (Sandbox Code Playgroud)
并由datosO3.melt继承
str(datosO3.melt)
'data.frame': 23328 obs. of 3 variables:
$ fecha : POSIXct, format: "2017-06-13 00:00:00" "2017-06-13 00:10:00" ...
$ variable: Factor w/ 54 levels "Alcoi.Verge_dels_Lliris",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 75 76 73 72 71 72 73 74 74 73 ...
Run Code Online (Sandbox Code Playgroud)
但是当 …
我有一个我用 plotly 创建的情节。当我将它部署到我闪亮的应用程序时,X 和 Y 标签被切断,如下所示:

我怎样才能防止这种情况发生?如果我使用普通图,标签不会被切断,但我需要图是交互式的。
这是我创建情节的代码:
ui.r:
#creating app with shiny
library(shiny)
library(shinydashboard)
shinyUI(
dashboardPage(
dashboardHeader(title = "Dashboard"),
dashboardSidebar(
menuItem("Dashboard")
),
dashboardBody(
fluidPage(
box(plotlyOutput("bakePlot")),
box(plotOutput("bakeMonthly"))
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
服务器.r:
shinyServer(function(input, output){
output$bakePlot <- renderPlotly({
ggplot(sales_bakery, aes(ProductName, ProductSales))+
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(7000, 9500)) + ggtitle("January Sales in Bakery") +
xlab("Category") + ylab("Quantity Sold")+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(angle = 60, hjust = 1),
axis.text.y = element_text(colour = "black", size = …Run Code Online (Sandbox Code Playgroud) 我使用 ggplot2 创建了一个情节散点图ggplotly。
我想获得一个选择/放大的数据点的表格,但我找不到办法做到这一点。
library(ggplot2)
library(plotly)
p <- ggplotly(plot.rel.kinship)
htmlwidgets::saveWidget(as_widget(p), "scatterplot.html")
Run Code Online (Sandbox Code Playgroud)
在 plotly 论坛上似乎有一个类似的未回答的问题:
https://community.plot.ly/t/get-datapoints-in-currently-zoomed-view/259
这个问题似乎也相关:
通过 Shiny 中的串扰将 Plotly 与 DT 一起使用
感谢您的帮助。
更新:
library(crosstalk)
a <- datatable(king.kin.subset)
kinship.plotly.table <- bscols(widths = c(6, 4), p, a)
htmltools::save_html(kinship.plotly.table, "scatterplot_table.html")
Run Code Online (Sandbox Code Playgroud)
仍然无法根据散点图上的点选择来更新数据表。
我想让我的散点图点可点击,并在点击时打开每个点各自的超链接。我正在尝试使用 ggplotly 来做到这一点。使用 plotly() 构建绘图时,有一种简单的方法可以做到这一点(参见下面的第一个示例)。但是,在 ggplotly 上运行它时,单击时会打开一个空网页。
任何人都知道我如何使用下面的代码进行这项工作?代码的 OnRender 部分是否有解决方案(不熟悉 javascript)?使用 plotly 的第一个代码块有效。第二个代码块是我使用 ggplotly 的基本尝试,但它不起作用。
任何关于这方面的指导都会很棒!提前致谢
library(plotly)
library(htmlwidgets)
library(dplyr)
#1. Using plotly
mtcars$url <- paste0(
"http://google.com/#q=",
rownames(mtcars))
p <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>%
add_markers(customdata = ~url)
onRender(p, "
function(el, x) {
el.on('plotly_click', function(d) {
var url = d.points[0].customdata;
window.open(url);
});
}
")
#2. Using ggplotly`
p <- ggplot(data = mtcars, aes(x = wt, y = mpg))+
geom_point()
pp <- ggplotly(p)
pp <- add_markers(pp, customdata = ~url) …Run Code Online (Sandbox Code Playgroud) 我一直在绞尽脑汁想如何用 plotly 去掉跟踪名称,但似乎找不到任何东西。似乎添加跟踪名称是 plotly boxplots 的一个独特功能。我可以将它命名为“”,但我需要原始跟踪名称,以便在覆盖标记时可以引用它。我已经尽可能地将代码简化为根本问题。有没有办法隐藏跟踪名称?
housing = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")
colnames(housing) = c("CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS","RAD","TAX","PTRATIO","B","LSTAT","MEDV")
housing %>%
plot_ly( x = ~RM,
type="box",
name = "RM",
showlegend = FALSE
) %>%
add_markers(x=6, y="RM",
marker = list(color = "blue", size = 15)
)
Run Code Online (Sandbox Code Playgroud) 在A R降价文件,没有人知道为什么out.width,out.height,figure.width并figure.height产生一个PDF文件时参数不plotly改变图表的大小?(我确切地说这些参数可以完美地使用plot函数)
请在下面找到带有 Rmarkdown 文件的可复制示例
在这个例子中,我希望 plotly 图表像 plot 图表一样占据整个工作表。
---
title: "Change chart size chart on pdf file using plotly"
output:
pdf_document: default
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo=FALSE,message=FALSE)
```
## Parameters doesn't work with plotly
```{r, out.width='100%',out.height='100%',fig.height=20, fig.width=15, fig.align="left"}
library(plotly)
plot_ly(x = cars[1:10,]$speed,y = cars[1:10,]$dist)
```
## Parameters works using plot function
```{r,out.width='130%',out.height='100%', fig.height=20, fig.width=15, fig.align="left"}
plot(cars[1:10,])
```
Run Code Online (Sandbox Code Playgroud)
我想在地块之间添加空间,但是当我玩边距游戏时,它会重叠或割伤。这是代码:
library(plotly)
plotList <- function(nplots) {
lapply(seq_len(nplots), function(x) plot_ly())
}
s1 <- subplot(plotList(6), nrows = 2, shareX = TRUE, shareY = TRUE)
s2 <- subplot(plotList(2), shareY = TRUE)
p <- subplot(s1, s2, plot_ly(), nrows = 3, margin = 0.04, heights = c(0.6, 0.3, 0.1))
print(p)
Run Code Online (Sandbox Code Playgroud)
我得到这个:
而我希望这样的东西(用油漆完成的图像)在不同子图之间有更大的间距:
我应该怎么做 ?
r ×10
r-plotly ×10
plotly ×7
ggplot2 ×3
shiny ×3
ggplotly ×2
r-markdown ×2
datatables ×1
date ×1
htmlwidgets ×1
javascript ×1
knitr ×1
spacing ×1
subplot ×1