我正在使用ggplotly来显示交互式时间序列图.x轴采用日期格式,但悬浮工具提示正在将日期格式转换为数字(附加截图).关于如何在工具提示中将日期显示为正确日期的任何想法?
下面是一小段代码:
output$ggplot <- renderPlotly({
plotbycity<-df_postgres %>% group_by(city, date, bedroooms) %>%
filter(city %in% input$checkGroup & bedroooms==input$radio) %>%
summarise(count=n(),rent=median(rent)) %>%
ungroup()
plotbycity$date<-as.Date(plotbycity$date)
# Error handling
plotbycity<-plotbycity[!is.na(plotbycity$city),]
if (is.null(plotbycity)) return(NULL)
#plotbycity<-ungroup(plotbycity)
#dat <- dat[c("rent", "bedroooms", "date", "City")]
#dat <- melt(dat,id.vars=c("date", "City", "bedroooms"),na.rm=TRUE) #
# draw the line plot using ggplot
gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
text = paste('obs: ', count))) +
geom_line() +
ggtitle("Monthly Rents")
# #theme_hc(bgcolor = "darkunica") +
# #scale_fill_hc("darkunica")
p …Run Code Online (Sandbox Code Playgroud) 我在论坛上实在找不到答案。我想创建一个离线绘图的 3D 表面。我使用样本点。场景显示为空,没有表面。请帮忙,谢谢。
import pandas as pd
import numpy as np
from plotly.offline import download_plotlyjs, init_notebook_mode, plot
from plotly.graph_objs import *
init_notebook_mode()
df3=pd.DataFrame({'x':[1,2,3,4,5], 'y':[10,20,30,20,10], 'z':[5,4,3,2,1]})
trace0= Surface(x=df3['x'], y=df3['y'], z=df3['z'])
data=[trace0]
fig=dict(data=data)
plot(fig)
Run Code Online (Sandbox Code Playgroud) 我现在是Plotly的初学者,有一个我似乎无法解决的问题.我有我的阴谋堆积条形图,但我不知道如何为每个类别着色.我目前正在使用R.
我目前的代码是:
p = plot_ly(x, x = day, y = count, type = "bar", group = status) %>% layout(barmode = "stack", showlegend = T)
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用"color = "参数和标记,但没有正确的颜色我的图形.
我在Anaconda3中使用Jupyter。
我Anaconda3\和Anaconda3\Scripts\文件夹已添加到%PATH%变量中。
即使位于上面jupyter.exe的Scripts文件夹中,与Jupyter相关的命令也不能不加破折号。
例如:
jupyter kernelspec --version
Error executing Jupyter command 'kernelspec': [Errno 'jupyter-kernelspec' not found] 2
Run Code Online (Sandbox Code Playgroud)带有破折号的相同命令有效:
jupyter-kernelspec --version
5.2.2
Run Code Online (Sandbox Code Playgroud)jupyter-notebook等等也一样。
我是否还需要添加其他内容%PATH%?我想念什么吗?
我也为此在Github上打开了一个问题,但不幸的是它没有引起任何注意:https : //github.com/jupyter/jupyter/issues/381
我很擅长闪亮,并试图推翻一个阴谋的饼图.单击runapp后,呈现的html仅包含标题,即"Plotly"
代码如下
UI
library(shiny)
shinyUI <- fluidPage(
titlePanel("Plotly"),
mainPanel(
plotOutput("plot2")))
Run Code Online (Sandbox Code Playgroud)
Server
library(shiny)
library(ggplot2)
library(ggthemes)
library(plotly)
library(shiny)
library(ggthemes)
library(RODBC)
library(magrittr)
synddb <- odbcConnect("Syndromic", uid="uname", pwd="pwd", believeNRows=FALSE)
totalcomplaints<-sqlQuery(channel=synddb,query= "select c.siteid,count(c.siteid) number_of_complaints, s.sitefullname from complainttmp c, site s
where s.siteid= c.siteid and c.siteid in(1,2,3,4,5,6,7,8, 10,11,19,20)
group by c.siteid,s.sitefullname
order by c.siteid asc")
shinyServer <- function(input, output) {
output$plot2 <- renderPloty({
print(
plot_ly(totalcomplaints,labels=paste(totalcomplaints$sitefullname,totalcomplaints$siteid,sep = "-"),values = ~number_of_complaints, type = 'pie',
textposition = 'inside',
textinfo = 'label+percent+values',
insidetextfont = list(color = '#FFFFFF'),
hoverinfo = 'text', …Run Code Online (Sandbox Code Playgroud) dplyr中是否有任何异常处理机制mutate()?我的意思是捕获异常并处理异常的方法。
让我们假设我有一个函数在某些情况下会引发错误(在示例中,如果输入为负),为简单起见,我定义了该函数,但实际上,它将是某些R包中的函数。让我们假设此函数是矢量化的:
# function throwing an error
my_func <- function(x){
if(x > 0) return(sqrt(x))
stop('x must be positive')
}
my_func_vect <- Vectorize(my_func)
Run Code Online (Sandbox Code Playgroud)
现在,假设我要在内部使用此函数mutate()。
如果在a中使用此函数mutate(),它将在第一个错误处停止并且不返回任何结果:
library(dplyr)
# dummy data
data <- data.frame(x = c(1, -1, 4, 9))
data %>% mutate(y = my_func_vect(x))
# Error in mutate_impl(.data, dots) : Evaluation error: x must be positive.
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以捕获错误,并NA在这种情况下执行某些操作(例如,返回),同时获取其他元素的结果?
我期望的结果是使用带有的循环可以实现的效果tryCatch(),即类似于以下内容:
y <- rep(NA_real_, length(data$x))
for(i in seq_along(data$x)) {
tryCatch({
y[i] <- my_func_vect(data$x[i])
}, error = …Run Code Online (Sandbox Code Playgroud) 包rgl包含一个非常有用的函数ellipse3d,它可以返回一个覆盖 3D 中 95% 的点的椭球体。然后可以使用该对象来rgl::plot3d绘制它。我的问题是,是否可以将 的输出转换ellipse3d为可以通过 js 绘图包(如plotly)绘制的内容?
library(rgl)
dt <- cbind(x = rnorm(100), y = rnorm(100), z = rnorm(100))
ellipse <- ellipse3d(cov(dt))
plot3d(dt)
plot3d(ellipse, add = T, color = "red", alpha = 0.5)
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做才能通过绘图绘制椭球体呢?
我想知道是否有一种方法可以为plotly的饼图提供自定义图标,而不是通常的饼图分区
到目前为止,我正在使用饼图显示性别信息,该饼图如下所示:
我正在尝试使其看起来像下面链接中的性别图:
绘图代码如下:
plot_ly(genderselection, labels = ~Gender, values = ~Freq, type = 'pie') %>%
layout(title = paste0("Gender Distribution of Patients from Boston"),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
legend=list(orientation='h'))
Run Code Online (Sandbox Code Playgroud)
性别选择数据框:
Gender Freq
F 70
M 65
Run Code Online (Sandbox Code Playgroud)
如果不使用plotly,是否还有其他库可用于使用自定义图标显示信息?
我一直在尝试制作一张美国地图,其中包含枪击事件的叠加散点图,每个点的 hoverinfo 标签中都有伤亡人数和受伤人数。但是,标签弹出为“num_killed”,我想将其格式化为“Number Killed:”。到目前为止,我已经尝试在 ggplot 部分标记变量,但无济于事。有什么方法可以更改工具提示中变量名称的打印方式吗?
这是我的代码:
library(plotly)
library(dplyr)
library(ggplot2)
us <- map_data("state")
library(maps)
g <- ggplot() +
geom_polygon(data = us, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=shootings, aes(x=lng, y=lat, size = num_killed, color = num_injured)) +
labs(color = "Number Injured", size = "Number Killed", num_killed = "Number Killed", num_injured = "Number Injured")
plot <- ggplotly(g)
Run Code Online (Sandbox Code Playgroud)
所附数据集是该数据库处理后的 csv:http : //www.gunviolencearchive.org/reports/mass-shooting? page =1 (作为数据框处理)。
我的Jupyter笔记本电脑有问题。每次我启动笔记本时,内核都会死亡。
因此,我决定使用pip卸载jupyter notebook:
pip uninstall jupyter notebook
Run Code Online (Sandbox Code Playgroud)
成功卸载后,我再次使用相同的点进行安装:
pip install jupyter notebook
Run Code Online (Sandbox Code Playgroud)
然后,像往常一样,我输入cmd:
jupyter notebook
Run Code Online (Sandbox Code Playgroud)
但是得到了这个错误:
'jupyter'无法识别为内部或外部命令,可操作程序或批处理文件。
因此,我检查了IPython的位置,发现现在要调用的正确文件是jupyter-notebook,它与jupyter notebook的破折号不同。如果我从cmd 运行jupyter-notebook,一切正常。
谁能解释,发生了什么事,为什么现在将该文件称为jupyter-notebook?
谢谢