r - R:去除一半的置信带
问题描述
我正在使用 R 编程语言。我创建以下数据和图表:
library(xts)
library(ggplot2)
library(dplyr)
library(plotly)
library(lubridate)
set.seed(123)
#time series 1
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
property_damages_in_dollars <- rnorm(731,100,10)
final_data <- data.frame(date_decision_made, property_damages_in_dollars)
#####aggregate
final_data$year_month <- format(as.Date(final_data$date_decision_made), "%Y-%m")
final_data$year_month <- as.factor(final_data$year_month)
f = final_data %>% group_by (year_month) %>% summarise(max_value = max(property_damages_in_dollars), mean_value = mean(property_damages_in_dollars), min_value = min(property_damages_in_dollars))
####plot####
fig <- plot_ly(f, x = ~year_month, y = ~max_value, type = 'scatter', mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE, name = 'max_value')
fig <- fig %>% add_trace(y = ~min_value, type = 'scatter', mode = 'lines',
fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),
showlegend = FALSE, name = 'min_value')
fig <- fig %>% add_trace(x = ~year_month, y = ~mean_value, type = 'scatter', mode = 'lines',
line = list(color='rgb(0,100,80)'),
name = 'Average')
fig <- fig %>% layout(title = "Average Property Damages",
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
xaxis = list(title = "Months",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE),
yaxis = list(title = "Dollars",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE))
fig
我尝试如下删除下半部分:
fig <- plot_ly(f, x = ~year_month, y = ~max_value, type = 'scatter', mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE, name = 'max_value')
fig <- fig %>% add_trace(x = ~year_month, y = ~mean_value, type = 'scatter', mode = 'lines',
line = list(color='rgb(0,100,80)'),
name = 'Average')
fig <- fig %>% layout(title = "Average Property Damages",
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
xaxis = list(title = "Months",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE),
yaxis = list(title = "Dollars",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE))
我正在尝试删除此图的底部:
但这会取消顶部和底部:
有人可以告诉我我做错了什么吗?如何删除底部带并仅保留顶部?
谢谢
解决方案
您在 plotly 中使用的绘图功能正在对 和 之间的所有内容进行min_value
着色max_value
。您希望它遮蔽 和 之间的所有mean_value
内容max_value
。因此尝试替换min_value
为mean_value
.
# unchanged
fig <- plot_ly(f, x = ~year_month, y = ~max_value, type = 'scatter', mode = 'lines',
line = list(color = 'transparent'),
showlegend = FALSE, name = 'max_value')
# the replacement happens here
fig <- fig %>% add_trace(y = ~mean_value, type = 'scatter', mode = 'lines',
fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),
showlegend = FALSE, name = 'min_value')
# unchanged
fig <- fig %>% add_trace(x = ~year_month, y = ~mean_value, type = 'scatter', mode = 'lines',
line = list(color='rgb(0,100,80)'),
name = 'Average')
推荐阅读
- scala - 如何在播放框架中以 Scala 形式使用 POST 方法?
- vhdl - 这个 VHDL 代码会造成分配冲突吗?
- angular7 - this.xxxx.emit 不是函数
- python - Airflow - 在 BranchPythonOperator 中访问 Xcom
- typescript - 函数的返回类型可以取决于输入参数的值吗?
- php - Symfony yml - 带@的密码
- matlab - 在 MATLAB 的 txtfile 中查找所有可见字符(并检查是否没有现有文件)
- reactjs - 如何在没有 {" "} 的情况下使用 ESLint、Prettier 和 Flow 结束标记在 VSCode 中格式化 React Project
- r - 如何在R中对向量进行分类以绘制饼图
- typescript - 如何在 Typescript 中声明多个相同数据类型的变量,就像我们在 JavaScript 中所做的那样:- var a,b,c;