我正在尝试生成使用 ggplot2 制作的绘图的交互式版本,并且希望保留图例标签。该图是特定社区 6 年来每年收入分布的条形图。
我的 ggplot scale_fill_manual() labels 参数是:
scale_fill_manual(values = values,
labels = c("< 10,000",
"10,000 to 14,999",
"15,000 to 24,999",
"25,000 to 34,999",
"35,000 to 49,999",
"50,000 to 74,999",
"75,000 to 99,999",
"100,000 to 149,999"))
Run Code Online (Sandbox Code Playgroud)
当我运行p <- ggplotly(p)标签时,如预期的那样,更改为我在数据集中对数据进行分组的标签。例如,检查我的新图的前 12 个标签给出:
> p$Incomes[1:12]
[1] Households..Estimate..Less.than..10.000
[2] Households..Estimate..Less.than..10.000
[3] Households..Estimate..Less.than..10.000
[4] Households..Estimate..Less.than..10.000
[5] Households..Estimate..Less.than..10.000
[6] Households..Estimate..Less.than..10.000
[7] Households..Estimate...10.000.to..14.999
[8] Households..Estimate...10.000.to..14.999
[9] Households..Estimate...10.000.to..14.999
[10] Households..Estimate...10.000.to..14.999
[11] Households..Estimate...10.000.to..14.999
[12] Households..Estimate...10.000.to..14.999
Run Code Online (Sandbox Code Playgroud)
按照此处找到的示例ggplotly - R,标记跟踪名称,我运行作为测试:
p$Incomes[[1]] <- "< 10,000" …Run Code Online (Sandbox Code Playgroud) 创建 ggplot 对象的绘图对象。是否可以全局设置模式栏选项,以便每个绘图仅显示以下模式栏按钮,如 myPlot_c 中所示。
library(ggplot2)
library(plotly)
library(dplyr)
myPlot_a <- ggplot(mtcars, aes(x=disp,y=mpg)) + geom_point()
myPlot_b <- ggplotly(myPlot_a)
#Is it possible to set this global, so don't have to repeat for each plot
myPlot_c <- myPlot_b %>%
config(displaylogo=F,
modeBarButtonsToRemove =
list("sendDataToCloud","zoom2d","pan2d","select2d","lasso2d",
"zoomIn2d","zoomOut2d","autoScale2d","hoverClosestCartesian",
"hoverCompareCartesian"))
Run Code Online (Sandbox Code Playgroud)
我可以包装在一个函数中,但发现如果每次都设置 modebarButtons,则与闪亮的应用程序结合使用 renderPlotly() 会遇到很多滞后。
我希望能够将R plotly heatmap.
我的意思是:我有一个分层聚类的基因表达数据集:
require(permute)
set.seed(1)
mat <- rbind(cbind(matrix(rnorm(2500,2,1),nrow=25,ncol=500),matrix(rnorm(2500,-2,1),nrow=25,ncol=500)),
cbind(matrix(rnorm(2500,-2,1),nrow=25,ncol=500),matrix(rnorm(2500,2,1),nrow=25,ncol=500)))
rownames(mat) <- paste("g",1:50,sep=".")
colnames(mat) <- paste("s",1:1000,sep=".")
hc.col <- hclust(dist(t(mat)))
dd.col <- as.dendrogram(hc.col)
col.order <- order.dendrogram(dd.col)
hc.row <- hclust(dist(mat))
dd.row <- as.dendrogram(hc.row)
row.order <- order.dendrogram(dd.row)
mat <- mat[row.order,col.order]
Run Code Online (Sandbox Code Playgroud)
然后,我将其离散化为特定的表达范围,因为这恰好有助于解决我的案例的颜色问题。我还创建了其他结构来帮助我按照colorbar我想要的方式绘制:
require(RColorBrewer)
mat.intervals <- cut(mat,breaks=6)
interval.mat <- matrix(mat.intervals,nrow=50,ncol=1000,dimnames=list(rownames(mat),colnames(mat)))
interval.cols <- brewer.pal(6,"Set2")
names(interval.cols) <- levels(mat.intervals)
require(reshape2)
interval.df <- reshape2::melt(interval.mat,varnames=c("gene","sample"),value.name="expr")
interval.cols2 <- rep(interval.cols, each=ncol(mat))
color.df <- data.frame(range=c(0:(2*length(interval.cols)-1)),colors=c(0:(2*length(interval.cols)-1)))
color.df <- setNames(data.frame(color.df$range,color.df$colors),NULL)
for (i in 1:(2*length(interval.cols))) {
color.df[[2]][[i]] <- interval.cols[[(i + 1) / …Run Code Online (Sandbox Code Playgroud) 我需要绘制一条路径,该路径并不严格从左到右,而是在 y 轴上交叉,但是自从我升级到plotly 4.7以来,我不能不这样做。fx 没问题。3.6
有谁知道如何以情节方式告诉路径如何排序?
library(dplyr)
library(plotly) # > 4.x
data.frame(x = c(1:5,5:1),y = c(1:10)) %>%
arrange(y) %>%
plot_ly(x = ~x,y = ~y) %>% add_lines()
Run Code Online (Sandbox Code Playgroud)
如果你查看 data.frame 它应该遵循红色路径:
data.frame(x = c(1:5,5:1),y = c(1:10)) %>% arrange(y)
x y
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 5 6
7 4 7
8 3 8
9 2 9
10 1 10
Run Code Online (Sandbox Code Playgroud) 我希望能够为当用户从绘图上的模式栏中选择“下载为 png”时创建的 ggplotly 图像指定文件名和尺寸。

我看到有一种方法可以从这里提出的绘图中做到这一点,但希望有来自 ggplotly 图表的类似控制,类似于这里提出的问题。
使用相同的代码示例:
x <- c(1:15)
y <- c(1:15)
xy <- as.data.frame(cbind(x, y))
example <- ggplot(data = xy, aes(x = x, y = y)) + geom_line()
ggplotly(example) %>% config(displaylogo = FALSE,
modeBarButtonsToRemove = c(
'sendDataToCloud',
'autoScale2d',
'resetScale2d',
'hoverClosestCartesian',
'hoverCompareCartesian'
))
Run Code Online (Sandbox Code Playgroud)
我可以利用另一个config setting来指定文件名/属性吗?downloadImage 设置具有文件名、宽度和高度,但我不确定来自 ggplot 时如何访问这些内容。我发现的配置设置列表似乎没有任何我可以重新调整用途的内容。
能够指定文件名和尺寸将非常有帮助,这样我的网站的用户就可以更轻松、更一致地下载图像。此外,向这些下载的图像添加时间戳/文本信息的能力将更加出色。
我试图以绘图方式覆盖折线图和条形图(用一条垂直线指定重要日期),但我遇到了这个问题,其中两条零线偏移而不是在同一条线上。我尝试过摆弄overlaying = 'y'其中的选项layout,并尝试更改三个组件的顺序trace,但似乎没有任何帮助。有什么想法如何修复吗?下面是我的带有虚拟数据的代码:
(此外,如果您能解决我的图例重叠 y2axis 问题,还可获得奖励积分)
date <- seq(as.Date("2015/6/1"), by = "month", length.out = 19)
wires_mnth <- c(rep(0,8),100000,750000,1200000,2500000,3100000,5500000,7500000,8000000,9900000,11300000,11000000)
wires_cnt <- c(rep(0,8),100,200,250,325,475,600,750,800,1000,1150,1200)
data <- data.frame(date, wires_mnth)
plot_ly(data) %>%
add_trace(x = ~date, y = ~wires_cnt, type = 'bar', name = 'Wires Count',
yaxis = 'y2', opacity = .5) %>%
add_trace(x = ~date, y = ~wires_mnth, type = 'scatter', mode = 'lines', name
= 'Monthly Wires') %>%
add_trace(x = c(2016,2016), y = c(0, 12000000), type = 'scatter', …Run Code Online (Sandbox Code Playgroud) 我绘制了价格图表。Y 轴刻度设置为整个价格范围内的最低和最高价格。
当我减小范围时,例如当我显示最后一小时时,Y 轴不会缩放并且给出相同的范围。如何才能使 Y 轴自动缩放到可见的价格范围?
这是我的布局
layout = go.Layout(
title=title,
legend=dict(orientation="h"),
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label='1h',
step='hour',
stepmode='backward'),
dict(count=3,
label='3h',
step='hour',
stepmode='backward'),
dict(count=6,
label='6h',
step='hour',
stepmode='backward'),
dict(count=1,
label='1d',
step='day',
stepmode='backward'),
dict(count=3,
label='3d',
step='day',
stepmode='backward'),
dict(step='all')
])
),
rangeslider=dict(),
type='date'
),
yaxis=dict(
title=y_axis_label,
showticklabels= not seperate_y_axis,
autorange=True,
type='linear'
)
)Run Code Online (Sandbox Code Playgroud)
我希望交互式(这意味着可以通过框/套索选择来选择它们)抖动点显示在分组箱线图上。我提出了这个问题:Add jitter to boxplot usingmarkers inplotly。我想要完全相同,但箱线图应该分组。
我做了一个箱线图,但要点都混淆了:
dat %>%
plot_ly(x = ~as.numeric(IC),
y = ~xval,
color = ~gene,
type = "box",
hoverinfo = "none",
boxpoints = FALSE
) %>%
add_markers(x = ~jitter(as.numeric(IC)),
y = ~xval,
color = ~gene,
marker = list(size = 3),
hoverinfo = "text",
text = txt,
showlegend = TRUE) %>%
layout(boxmode = "group")
Run Code Online (Sandbox Code Playgroud)
当我尝试按因子对 X 轴进行分组(以便每个组合都是一个级别)时,我无法对箱线图进行分组:
dat <- dat %>%
mutate(gene_x_covariate = as.factor(
paste0(get(facet_title), "-", gene)))
dat %>%
plot_ly(x …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)
如果我删除工具提示,则回归线出现在输出图中,但包含工具提示后,回归线不会出现在图中。有什么解决方案可以将两者结合在一起吗?
我正在尝试使用破折号和绘图生成散点图框,当我在标记内指定符号(例如“正方形”)时,生成地图时它不会呈现。我正在使用回调函数,并且尝试了几个符号,但没有一个有效,但圆圈标记除外。我是否缺少一些明显的东西?这是一个最小的示例,要运行它,您将需要一个 Mapbox 令牌。
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import datetime as dt
app = dash.Dash()
mapbox_access_token = 'YOUR_MAPBOX_TOKEN_HERE'
app.layout = html.Div(children=[
html.H1('Dashboard'),
dcc.DatePickerRange(id='fecha',
start_date=dt.datetime.today() - dt.timedelta(days=30),
end_date=dt.datetime.today()
),
html.Center(dcc.Graph(
id='mapa',
figure={
'data': [
go.Scattermapbox(
lat=[],
lon=[],
)
],
'layout': go.Layout(
autosize=False,
width=1300,
height=800,
hovermode='closest',
showlegend=False,
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=-32,
lon=-70
),
pitch=0,
zoom=5,
),
)
})
,)
])
@app.callback(
dash.dependencies.Output('mapa', 'figure'),
[dash.dependencies.Input('fecha', 'start_date'),
dash.dependencies.Input('fecha', 'end_date')
])
def update_graph(inicio,fin):
Data = …Run Code Online (Sandbox Code Playgroud)