我想部分禁用绘图中的悬停文本,将其限制为 ggplot 中的一个数据框或几何图形。在下面的情况下,我只想将鼠标悬停在“城市”而不是地图轮廓上。我在 Python 中看到了一个解决方案,但在 R 中没有看到。我将如何控制图像大小以保持地图尺寸正确?https://plot.ly/ggplot2/interactive-tooltip/ 上的地图演示似乎并不在意!
library(mapdata)
library(ggplot2)
library(plotly)
Japan <- map_data("world2Hires", region="Japan")
Longitude <- 140
Latitude <- 36.5
df <- cbind.data.frame(Longitude,Latitude)
df$Name <- "Tokyo"
df$Name_2 <- "T?ky?"
XX <- ggplot() + geom_polygon(data=Japan, aes(x=long, y=lat, group=group), color="black", fill="white", text="") + coord_equal() + geom_point(data=df, aes(x=Longitude, y=Latitude, text=Name), color="green")
XX
ggplotly(XX) ##How to get hover text only on df not Japan, and remove "[object Object]"
XX <- ggplot() + geom_polygon(data=Japan, aes(x=long, y=lat, group=group), color="black", fill="white", text="") + coord_equal() …Run Code Online (Sandbox Code Playgroud) 我希望能够在 plotly 图形中选择和复制悬停文本。R API 可以实现吗?例如,在此热图中,将鼠标悬停在坐标 (X=a, Y=d) 上时,可以看到悬停文本显示:
x=a,
y=d,
z=0.71886
但是,文本是不可选择的。我们的目标将是如显示一个对话框(也许在相应的单元格单击鼠标右键),这里面的文字能够文本内容复制到剪贴板。任何帮助将不胜感激。
Plotly 是 R 中最有用的绘图包之一,但我正面临一个非常基本的问题。
当我绘制条形图时,它在小屏幕上看起来还不错,但是一旦我调整窗口大小,它就会覆盖整个窗口,使其变得丑陋。
由于我的要求是动态的,图上的条形数量会发生变化,但我想让条形宽度看起来不错。
但是当它被调整大小时
我用来绘制条形图的代码很简单,我希望不需要解释。宽度选项不会对图形产生任何影响。
我错过了什么吗?
ds <- as.data.frame(matrix(c('some_name',2300),nrow = 1,ncol=2))
colnames(ds) <- c('name','value')
plot_ly(ds,x=name,y=value,type='bar') %>% layout(width = 0.1)
Run Code Online (Sandbox Code Playgroud) 我试图在放大散点图时检索 x 轴的范围,但是当我尝试使用以下方法访问它时: figure['layout']['xaxis']['range'] 它只是返回'没有任何'。
这是我用来创建图形的代码:
# self.plot contains the dataframe passed to the function to be plotted
self.plot = df
# Creates the plotly plot figure
self.fig = self.plot.iplot(asFigure=True,kind='scatter', xTitle='Date', yTitle='Temperature')
# Displays the plot
iplot(self.fig, show_link=False)
Run Code Online (Sandbox Code Playgroud)
我正在使用袖扣从 Pandas DataFrame 创建绘图,所以我没有明确地将 layout.xaxis.range 设置为任何内容。此外,我正在使用 Jupyter Notebook 来显示图表,如果这有帮助的话。那么有没有办法获得绘图当前视图窗口的x轴范围?提前致谢!
我想生成一个图形,plotly如下图所示,使用基本 R 图形生成:
上图的R代码如下:
x = c(1,2,3,4,5)
y = c(0.1, 1, 10, 100, 1000)
axseq = y
plot(x, log10(y), yaxt="n")
axis(2, at=log10(axseq), labels=as.character(axseq))
for (i in 1:5){
bb = 1:10; a = (bb*10^(i-2)); axis(2, at=log10(a), tcl=-0.25, labels=F)
}
Run Code Online (Sandbox Code Playgroud)
plotly到目前为止,我对同一图的代码如下:
p = plot_ly(x=x, y=log10(y), mode="markers") %>%
layout(yaxis = list(tickmode="array", tickvals=log10(axseq), ticktext=as.character(axseq), zeroline=F, showline=T, ticks="outside"),
xaxis = list(showline=T, ticks="outside"))
Run Code Online (Sandbox Code Playgroud)
它有主要的刻度,但我找不到如何在 y 轴上添加次要刻度。
我正在尝试开始对 R 使用 plotly,但是我遇到了关于用户名的错误。当我尝试执行以下代码时...
test=ggplot(diamonds,aes(x=diamonds$carat,y=diamonds$price))+geom_point()
test2=plotly(test)
plotly(test)
Run Code Online (Sandbox Code Playgroud)
...我收到错误消息:
Storing 'username' as the environment variable 'plotly_username'
Error in Sys.setenv(plotly_username = username) :
wrong length for argument
Run Code Online (Sandbox Code Playgroud)
我认为 plotly 的 R 包可以通过 htmlwidgets 在没有用户名的情况下使用。据我所知,我使用的是最新版本的 plotly、ggplot 和 htmlwidgets。我究竟做错了什么?
Creating a plot by adding one column at a time works just fine
exPlot <- plot_ly(data.table(matrix(1:9,ncol = 3)))
theCols <- c("V2","V3")
exPlot <- exPlot %>% add_lines(x = ~V1, y = ~get(theCols[1]), type = "scatter",mode = "lines")
exPlot <- exPlot %>% add_lines(x = ~V1, y = ~get(theCols[2]), type = "scatter",mode = "lines")
exPlot
Run Code Online (Sandbox Code Playgroud)
but if I try to do the same thing in a for loop, it only displays the second trace, overwriting the first one.
exPlot <- plot_ly(data.table(matrix(1:9,ncol = 3))) …Run Code Online (Sandbox Code Playgroud) 在像下面这样的图表中,我想将千位分隔符更改为点“.”,但我不知道如何使用该tickformat参数来获取它。
require(plotly)
p <- plot_ly(x=~c(2012,2013,2014,2015,2016), y=~c(1200000,975000,1420000,1175000,1360000), type='bar') %>%
layout(yaxis = list(title="Income in €", tickformat=",d", gridcolor = "#bbb"), xaxis = list(title="Year"), margin=list(l=80))
p
Run Code Online (Sandbox Code Playgroud)
我正在自动化几个双周报告,所以我决定使用 plot.ly 创建一个线图。此线图具有不同数量的跟踪,具体取决于正在运行的报告。
我已经能够成功创建绘图,但我发现的所有方法都无法在我的电子邮件中内联显示绘图。这是我的代码:
SMTP_SERVER = "smtp.office365.com"
SMTP_PORT = 587
SMTP_USERNAME = username
SMTP_PASSWORD = password
EMAIL_TO = email_to
EMAIL_FROM = email_from
#here we loop through our data-set and pull out rows to make different traces
for deal in winGraph['DEAL_IDENTIFIER'].unique():
matched_rows = winGraph.loc[winGraph['DEAL_IDENTIFIER'] == deal]
date = matched_rows.DATE.tolist()
winRate = matched_rows.WIN_RATE.tolist()
traces.append(
go.Scatter(
x = date,
y = winRate,
name = str(deal)
)
)
plotly.tools.set_credentials_file(username = 'username', api_key = 'api_key')
fig = dict(data = traces)
imageURL = py.plot(fig,auto_open = False, …Run Code Online (Sandbox Code Playgroud) 当我在plotly堆积条形图中只添加了一条轨迹时,图例丢失了。
我的数据集如下:
myDF <- structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2017", class = "factor"),
Month = c("Mar", "Mar", "Mar", "Apr", "Apr", "Apr"), Date = structure(c(17241,
17242, 17242, 17259, 17260, 17261), class = "Date"), Quarter = c("Q1",
"Q1", "Q1", "Q2", "Q2", "Q2"), Week = structure(c(2L, 2L,
2L, 3L, 3L, 3L), .Label = c("2017-03-05", "2017-03-12", "2017-04-02"
), class = "factor"), NPS = structure(c(NA, NA, NA, 3L, 3L,
3L), .Label = c("Detractor", "Passive", "Promoter"), class = "factor")), …Run Code Online (Sandbox Code Playgroud)