R:图上的重叠点

R:图上的重叠点,r,dplyr,time-series,plotly,r-plotly,R,Dplyr,Time Series,Plotly,R Plotly,我正在使用R编程语言。我试图学习如何在图形上叠加点,然后将它们可视化 使用以下代码,我可以生成一些时间序列数据,按月汇总,取平均值/min/max,并绘制下图: 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

我正在使用R编程语言。我试图学习如何在图形上叠加点,然后将它们可视化

使用以下代码,我可以生成一些时间序列数据,按月汇总,取平均值/min/max,并绘制下图:

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
库(xts)
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
图书馆(lubridate)
种子集(123)
#时间序列1
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)

至少我总是觉得使用ggplot然后使用神奇的功能将其发送到plotly更简单
ggplotly
。希望这对你有帮助

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

dat <- final_data %>% 
  mutate(month = month(date_decision_made),
         year = year(date_decision_made),
         month_end = ceiling_date(date_decision_made, unit = "month")-1) %>% 
  group_by(month, year) %>% 
  mutate(mean_val = mean(property_damages_in_dollars,na.rm = TRUE),
         max_val = max(property_damages_in_dollars,na.rm = TRUE),
         min_val = min(property_damages_in_dollars,na.rm = TRUE))

p <- ggplot(data = dat) +
  geom_ribbon(aes(x = month_end, 
                  ymin = min_val,
                  ymax = max_val), alpha = 0.2)+
  geom_point(aes(x = month_end,
             y = property_damages_in_dollars), alpha = 0.3) +
  geom_line(aes(x = month_end,
                y = mean_val), size = 1.25) +
  labs(y = "Dollars",
       x = "Months")+
  theme_minimal()
  
ggplotly(p)
库(xts)
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
图书馆(lubridate)
种子集(123)
#时间序列1
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)
财产损失(单位:美元)
变异(平均值=平均值(财产损失,单位:美元,na.rm=真),
max_val=max(财产损失,单位:美元,na.rm=TRUE),
min_val=min(财产损失(单位:美元,na.rm=TRUE))

p至少我总是觉得使用ggplot,然后使用神奇的函数
ggplotly
将其发送到plotly更简单。希望这对你有帮助

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

dat <- final_data %>% 
  mutate(month = month(date_decision_made),
         year = year(date_decision_made),
         month_end = ceiling_date(date_decision_made, unit = "month")-1) %>% 
  group_by(month, year) %>% 
  mutate(mean_val = mean(property_damages_in_dollars,na.rm = TRUE),
         max_val = max(property_damages_in_dollars,na.rm = TRUE),
         min_val = min(property_damages_in_dollars,na.rm = TRUE))

p <- ggplot(data = dat) +
  geom_ribbon(aes(x = month_end, 
                  ymin = min_val,
                  ymax = max_val), alpha = 0.2)+
  geom_point(aes(x = month_end,
             y = property_damages_in_dollars), alpha = 0.3) +
  geom_line(aes(x = month_end,
                y = mean_val), size = 1.25) +
  labs(y = "Dollars",
       x = "Months")+
  theme_minimal()
  
ggplotly(p)
库(xts)
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
图书馆(lubridate)
种子集(123)
#时间序列1
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)
财产损失(单位:美元)
变异(平均值=平均值(财产损失,单位:美元,na.rm=真),
max_val=max(财产损失,单位:美元,na.rm=TRUE),
min_val=min(财产损失(单位:美元,na.rm=TRUE))

p在最后一行代码中添加以下内容:

fig %>% add_trace(data = final_data, 
              y = ~property_damages_in_dollars, x = ~year_month, 
              name = "Property Damage in Dollars", mode = "markers", 
              marker = list(color = " rgba(46, 49, 49, 1)", opacity = 0.2))
生成以下绘图,其中参数
颜色
不透明度
可以调整为您首选的样式。我们使用data.frame
final_data
,因为这是点所在的位置。variabel
year\u month
已经由您自己设置,因此不需要额外的数据争用。要实际生成点,请确保在
add_trace()
函数中设置
mode=“markers”

在最后一行代码中添加以下内容:

fig %>% add_trace(data = final_data, 
              y = ~property_damages_in_dollars, x = ~year_month, 
              name = "Property Damage in Dollars", mode = "markers", 
              marker = list(color = " rgba(46, 49, 49, 1)", opacity = 0.2))
生成以下绘图,其中参数
颜色
不透明度
可以调整为您首选的样式。我们使用data.frame
final_data
,因为这是点所在的位置。variabel
year\u month
已经由您自己设置,因此不需要额外的数据争用。要实际生成点,请确保在
add_trace()
函数中设置
mode=“markers”

要在设置标记格式方面具有充分的灵活性,您可以使用
添加跟踪
和数据帧的子集
最终数据
,并在代码中添加以下内容:

date_split <- split(final_data, final_data$year_month)
for (i in 1:length(date_split)) {
  fig <- fig %>% add_trace(y=date_split[[i]]$property_damages_in_dollars,
                           x=date_split[[i]]$year_month,
                           mode='markers'
                           )
}
结果2:

如果您想调整绘图的透明度,可以直接通过
rgba()
中的最后一个参数进行调整,例如:

marker=list(color='rgba(0,0,0, 0.2)')
结果3:

完整代码:
库(xts)
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
图书馆(lubridate)
种子集(123)
#时间序列1
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)

属性(单位:美元)要在设置标记格式方面具有充分的灵活性,您可以使用
add\u trace
对数据帧的子集
final\u data
使用以下代码:

date_split <- split(final_data, final_data$year_month)
for (i in 1:length(date_split)) {
  fig <- fig %>% add_trace(y=date_split[[i]]$property_damages_in_dollars,
                           x=date_split[[i]]$year_month,
                           mode='markers'
                           )
}
结果2:

如果您想调整绘图的透明度,可以直接通过
rgba()
中的最后一个参数进行调整,例如:

marker=list(color='rgba(0,0,0, 0.2)')
结果3:

完整代码:
库(xts)
图书馆(GG2)
图书馆(dplyr)
图书馆(绘本)
图书馆(lubridate)
种子集(123)
#时间序列1
作出决定的日期=序号(截止日期(“2014/1/1”)、截止日期(“2016/1/1”)、by=“天”)

是否应该使用geom_ribbon()参数?建议对您的效果如何?是否应该使用geom_ribbon()参数?建议对您的效果如何?