【问题标题】:R Plotly, composite scatter graphR Plotly,复合散点图
【发布时间】:2020-05-15 14:52:50
【问题描述】:

我有一个包含三列的数据集:Date 没有时间戳,当天的TimeTS 转换为与Date 关联的特定事件自午夜以来的小时数。第三列是Phase,它可以是morningafternooneveningnight。相位源自TimeTS

下面是一个示例

Date       TimeTS   Phase  
2020-01-29 2.75     Night 
2020-01-29 4.83     Morning
2020-01-29 7        Morning
2020-01-30 10.6     Afternoon

我需要使用像plotly 这样的交互式图表来显示以下内容:

  • 一个散点图,x 轴上带有Datey 上带有Time,每个日期标记了每个时间实例
  • Phase 的半透明波段在图表上水平排列,以便于解释时间波段和 Phase 标记的分布

下面的代码给了我接近我想要的结果,但不完全是。

p <- ggplot(clean.data, aes(x=Date,y=TimeTS)) +
  geom_rect(data=clean.data, mapping = aes(
    xmin=min(Date),
    xmax=max(Date),
    ymin=0,
    ymax=24,
    fill=Phase), alpha=0.1) +
  # scale_fill_manual(values=c("Morning" = "red", "Afternoon" = "blue", "Evening" = "green", "Night" = "black")) +
  geom_point()
p <- ggplotly(p)
p

这让我只得到图表上的Phase 乐队之一,而不是其他乐队。另外,我认为它是垂直运行而不是水平运行。

感谢任何想法。

【问题讨论】:

标签: r plotly scatter-plot r-plotly


【解决方案1】:

感谢@user12728748,我能够完成这项工作。代码更改如下:

df.phase <- data.frame(start = c(0,4,10,16,22), end = c(4,10,16,22,24), phase = c("Night", "Morning", "Afternoon", "Evening", "Night"))

p <- ggplot(clean.data, aes(x=Date,y=TimeTS)) +
  geom_rect(data=df.phase, inherit.aes = FALSE, aes(
    xmin= min(clean.data$Date),
    xmax= max(clean.data$Date),
    ymin=start,
    ymax=end,
    fill=phase), alpha=0.5) +
  scale_fill_manual(values=c("Morning" = "orange", "Afternoon" = "blue", "Evening" = "pink", "Night" = "black")) +
  geom_point()
p <- ggplotly(p)
p

代码将生成以下输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2021-04-17
    • 2021-11-04
    • 2017-01-17
    相关资源
    最近更新 更多