我一直在使用的时候有一个问题,ggplotly()一个ggplot图:y轴消失.这是一个使用iris数据集的可重现的例子(这个例子非常简单,但无论如何)
data(iris)
g = ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, fill = Species)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(name = "legend", values = c("blue", "red", "green")) +
ylab("Y title") +
ylim(c(0,3)) +
xlab("X title") +
ggtitle("Main title")
g
ggplotly(g)
Run Code Online (Sandbox Code Playgroud)
如您所见,Y轴标题消失了.
好吧,如果ylim删除它可行,但我想指定y限制.
我试着做以下事情:
data(iris)
g = ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, fill = Species)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(name …Run Code Online (Sandbox Code Playgroud) 更新10/3/2018:这不是R独有的.它之前已被报道,目前被标记为功能请求,在这里:https://github.com/plotly/plotly.js/issues/260
我正在为r中的plotly图形添加散点图点的自定义悬停文本.它似乎是将文本左对齐,居中对齐或右对齐,具体取决于文本框是分别显示在绘图点的右侧,中间还是左侧.无论盒子的位置如何,我都希望文本始终对齐.我已经能够设置字体样式,例如设置颜色和大小,但无法更改文本对齐方式.
这是一个MRE.我删除了传说,所以最右边的点被迫让他们的悬停框出现在点的左边.
plot_ly(iris,
x = ~Sepal.Length,
y = ~Sepal.Width,
type = 'scatter',
mode = 'markers',
hoverinfo = 'text',
hoverlabel = list(font = list(color = 'white')),
text = ~paste0('Some rather long text',
'\nSepal.Length: ', Sepal.Length,
'\nSepal.Width: ', Sepal.Width)) %>%
layout(showlegend = FALSE)
Run Code Online (Sandbox Code Playgroud)
我真的很喜欢 Plotly 中可用的平行坐标图,但我遇到了一个可以使用帮助的问题。
正如您在下面的示例中所看到的,执行 log10 转换可以更好地区分较小的值。然而,通过转换数据,我们失去了解释值的能力。我更喜欢记录缩放轴而不是数据,但找不到方法来做到这一点。
我确实在 github 问题https://github.com/plotly/plotly.js/issues/1071#issuecomment-264860379 中找到了与“轴样式”相关的内容,但不是解决此问题的方法。
我将不胜感激任何想法/指针。

library(plotly)
# Setting up some data that span a wide range.
df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")
df$sepal_width[1] = 50
df$sepal_width_log10 = log10(df$sepal_width)
p <- df %>%
plot_ly(type = 'parcoords',
line = list(color = ~species_id,
colorscale = list(c(0,'red'),c(0.5,'green'),c(1,'blue'))),
dimensions = list(
list(range = c(~min(sepal_width),~max(sepal_width)),
label = 'Sepal Width', values = ~sepal_width),
list(range = c(~min(sepal_width_log10),~max(sepal_width_log10)),
tickformat='.2f',
label = 'log10(Sepal Width)', values = ~sepal_width_log10),
list(range = c(4,8),
constraintrange = …Run Code Online (Sandbox Code Playgroud) 下图与我要找的很接近,但是我想知道以下是否可行:
require(dplyr); require(plotly); require(RColorBrewer); require(stringr)
# Summarise flow data
dat <- data.frame(customer = c(1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5),
holiday_loc = c("SA", "SA", "AB", "SA", "SA", "SA", "SA", "AB", "AB", "SA", "SA", "SA")) %>%
group_by(customer) %>%
mutate(holiday_num = seq_along(customer),
source=paste0(holiday_loc, '_', holiday_num),
target = lead(source),
last_hol …Run Code Online (Sandbox Code Playgroud) 我想覆盖在顶部的线的Plotly等高线图,类似于覆盖在基质的图像上的线,其中所述强度表示位置z内R3:
# Generate an arbitrary matrix
m <- matrix(sin(1:6^2) * 1:6, nrow = 6)
# Define a path
path <- data.frame(x = c(0:7), y = c(0, 1, 2, 2, 3, 3, 4, 6))
image(x = 1:6, y = 1:6, z = m, col = gray.colors(20), xlab = "x", ylab = "y")
lines(path$x, path$y)
Run Code Online (Sandbox Code Playgroud)
其中呈现:
使用 Plotly,我尝试过
library(plotly)
plot_ly(x = 1:6, y = 1:6, z = t(m), type = "contour") %>%
add_lines(x = path$x, y = path$y) …Run Code Online (Sandbox Code Playgroud) 我正在构建一个应用程序,允许用户使用 selectInput 在绘图图上动态添加和删除跟踪。
我试图从 plotly 包中使用 plotlyProxy () 和 plotlyProxyInvoke () ,但无济于事。
以下是我的基本代码:
library(shiny)
library(shinydashboard)
library(plotly)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Search", tabName = "Tabs", icon = icon("object-ungroup"))
)
),
dashboardBody(
tabItem(tabName = "Tabs",
fluidRow(
column(width=3,
box(
title="SELECT ",
solidHeader=TRUE,
collapsible=TRUE,
width=NULL,
selectInput(
inputId="Player",
selected = NULL, multiple = TRUE,
label=" Choose Player",
choices=c("Messi", "Suarez", "Ronaldo" )),
selectInput(
inputId="Delete",
selected = NULL, multiple = TRUE,
label=" Choose Player",
choices=c("Messi", "Suarez", "Ronaldo" )),
submitButton("Select")
)
),
column( width=9,
tabBox(
width="100%", …Run Code Online (Sandbox Code Playgroud) 我正在使用 R、ggplot 和 plotly 构建一个包含转推的堆叠条形图。如果单击条形图的一部分,我希望打开一个新的浏览器选项卡,并显示该特定日期的推文以及指定的转推量。但是,当我单击下面示例中的其中一个条时,会打开一个不同的链接,表明 url 未与这些条正确连接。我该如何解决这个问题?
我以前从未工作过,甚至从未见过 JavaScript,所以答案很可能就在那里。该图最终将出现在 Shiny 应用程序中。
library(rtweet)
library(ggplot2)
library(plotly)
# Get tweets
tweets <- get_timeline("BBC", n = 10)
# Create dataframe
data <- data.frame("retweet_count" = tweets$retweet_count,
"week" = c(1,1,1,2,2,3,4,5,5,6),
"url" = tweets$status_url)
# Create ggplot
ggplot(data = data,
aes(x = week,
y = retweet_count,
label = url)) +
geom_bar(stat = 'sum',
fill = "darkblue")
# Convert to plotly
p <- ggplotly(
p,
tooltip = c("y", 'label'))
# Add URL data to plot
p$x$data[[1]]$customdata <- data$url …Run Code Online (Sandbox Code Playgroud) install.packages(\'plotly\')\nRun Code Online (Sandbox Code Playgroud)\n我尝试通过在RStudioplotly中运行上面的代码来安装该包。但是,它失败并返回以下内容。我是新手,无法正确阅读此内容。我应该怎么做才能解决这个问题?
我也尝试先手动安装curl、openssl安装httr,但没有什么区别。另外,如果相关的话,我正在使用Ubuntu 20.04 LTS。
Installing package into \xe2\x80\x98/home/ariel/R/x86_64-pc-linux-gnu-library/4.0\xe2\x80\x99\n(as \xe2\x80\x98lib\xe2\x80\x99 is unspecified)\nalso installing the dependencies \xe2\x80\x98curl\xe2\x80\x99, \xe2\x80\x98openssl\xe2\x80\x99, \xe2\x80\x98httr\xe2\x80\x99\n\ntrying URL \'https://cloud.r-project.org/src/contrib/curl_4.3.tar.gz\'\nContent type \'application/x-gzip\' length 673779 bytes (657 KB)\n==================================================\ndownloaded 657 KB\n\ntrying URL \'https://cloud.r-project.org/src/contrib/openssl_1.4.3.tar.gz\'\nContent type \'application/x-gzip\' length 1207708 bytes (1.2 MB)\n==================================================\ndownloaded 1.2 MB\n\ntrying URL \'https://cloud.r-project.org/src/contrib/httr_1.4.2.tar.gz\'\nContent type \'application/x-gzip\' length 159950 bytes (156 KB)\n==================================================\ndownloaded 156 KB\n\ntrying URL \'https://cloud.r-project.org/src/contrib/plotly_4.9.2.1.tar.gz\'\nContent type \'application/x-gzip\' length 3709741 bytes (3.5 MB)\n==================================================\ndownloaded 3.5 MB\n\n* installing *source* package …Run Code Online (Sandbox Code Playgroud) 考虑以下 reprex 生成的图。请注意,ggplot 具有合理的图例,而在plotly 中,图例大量重复,每次相同类别(“制造商”)出现在每个方面时都会有一个条目。如何使情节图例更好地匹配 ggplot2 的图例?
library(plotly)
library(ggplot2)
p <- mpg %>%
ggplot(aes(year)) +
geom_ribbon(aes(ymin=cty, ymax=hwy, fill = manufacturer), alpha=0.2) +
geom_line(aes(y = hwy, col=manufacturer)) +
facet_wrap(~class)
p
Run Code Online (Sandbox Code Playgroud)
plotly::ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
我想通过 R 绘制具有独立图例的图,同时尊重色阶。
这就是我所拥有的:
library(plotly)
X <- data.frame(xcoord = 1:6,
ycoord = 1:6,
score = 1:6,
gender = c("M", "M", "M", "F", "F", "F"),
age = c("young", "old", "old", "old", "young", "young"))
plot_ly(data = X, x = ~xcoord, y = ~ycoord, split = ~interaction(age, gender),
type = "scatter", mode = "markers",
marker = list(color = ~score,
colorbar = list(len = .5, y = .3)))
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,颜色条混乱,两个类别纠缠在一起。
我需要为age(年轻与年老)和gender(男与女)分别提供图例,可以彼此独立地单击。这将是预期的结果:

编辑 1
这相当于ggplot2:
gg <- ggplot(X, aes(x …Run Code Online (Sandbox Code Playgroud) r-plotly ×10
r ×9
plotly ×8
ggplot2 ×4
ggplotly ×3
hover ×1
javascript ×1
package ×1
scatter-plot ×1
shiny ×1
ubuntu-20.04 ×1