gpt4 book ai didi

r - ggplotly 从单个方面删除数据

转载 作者:行者123 更新时间:2023-12-03 20:31:51 24 4
gpt4 key购买 nike

我正在尝试为 Shiny 的应用程序创建一个绘图图。我遇到了某些多面图布局的问题。每当有四个分面并且它们位于 2x2 网格中时,即使数据在 ggplot 中,左下分面也不显示任何数据。数字。当 3x2 网格中有 6 个面时也会发生这种情况,左下角再次被丢弃。 plotly ggplot examples page 上的示例显示几个具有 2x2 网格和正在显示的数据的示例。我不确定问题是什么。任何帮助深表感谢。

代码:

library(ggplot2)
library(dplyr)
library(plotly)

data %>%
filter(!is.na(result)) %>%
ggplot(aes(date_time, result)) +
facet_wrap(~group, scales = "free_y") +
geom_point() +
geom_line()


ggplotly()

ggplot输出:
<code>ggplot</code> output

ggplotly输出:
<code>ggplotly</code> output

数据:
data <- structure(list(group = c("w", "w", "w", "w", "w", "w", "w", 
"w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w",
"w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w",
"y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y",
"y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y", "y",
"y", "y", "y", "y", "y", "y", "y", "x", "x", "x", "x", "x", "x",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x",
"x", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z",
"z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z", "z",
"z", "z", "z", "z", "z", "z", "z", "z"),
date_time = structure(c(1501581060, 1501668000, 1501752000,
1501837740, 1502101200, 1502184600, 1502271840,
1502357280, 1502442720, 1502701860, 1502789280,
1502876340, 1502961600, 1503047640, 1503305520,
1503394200, 1503480900, 1503567600, 1503651300,
1503915120, 1503997800, 1504086300, 1504171920,
1504259100, 1504607100, 1504691040, 1504777500,
1504865820, 1505122680, 1505211600, 1505296560,
1505382720, 1505470140, 1501581060, 1501668000,
1501752000, 1501837740, 1502101200, 1502184600,
1502271840, 1502357280, 1502442720, 1502701860,
1502789280, 1502876340, 1502961600, 1503047640,
1503305520, 1503394200, 1503480900, 1503567600,
1503651300, 1503915120, 1503997800, 1504086300,
1504171920, 1504259100, 1504607100, 1504691040,
1504777500, 1504865820, 1505122680, 1505211600,
1505296560, 1505382720, 1505470140, 1501581060,
1501668000, 1501752000, 1501837740, 1502101200,
1502184600, 1502271840, 1502357280, 1502442720,
1502701860, 1502789280, 1502876340, 1502961600,
1503047640, 1503305520, 1503394200, 1503480900,
1503567600, 1503651300, 1503915120, 1503997800,
1504086300, 1504171920, 1504259100, 1504607100,
1504691040, 1504777500, 1504865820, 1505122680,
1505211600, 1505296560, 1505382720, 1505470140,
1501581060, 1501668000, 1501752000, 1501837740,
1502101200, 1502184600, 1502271840, 1502357280,
1502442720, 1502701860, 1502789280, 1502876340,
1502961600, 1503047640, 1503305520, 1503394200,
1503480900, 1503567600, 1503651300, 1503915120,
1503997800, 1504086300, 1504171920, 1504259100,
1504607100, 1504691040, 1504777500, 1504865820,
1505122680, 1505211600, 1505296560, 1505382720,
1505470140),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
result = c(2.1, 1.75, 1.65, 1.65, 1.75, 1.65, 1.3, 1.65, 1.9, 1.6, 1.65,
1.35, 1.6, 1.85, 2.05, 1.95, 2, 1.95, 1.9, 1.9, 2.05, 2, 2,
2, 2.1, 2.1, 2, 2.05, 2.05, 2.3, 2.2, 2, NA, 7.14, 7.1, 7.09,
7.09, 7.13, 7.12, 7.13, 7.07, 7.04, 7.06, 7.12, 7.08, 7.06,
7.07, 7.05, 7.05, 7.1, 7.09, 7.06, 7.18, 7.16, 7.11, 7.17,
7.14, 7.13, 7.08, 7.11, 7.09, 7.12, 7.05, 7.08, 7.02, 7.12,
0.214, 0.181, 0.357, 0.49, 0.294, 0.369, 0.406, 0.325, 0.197,
0.36, 0.358, 0.408, 0.37, 0.254, 0.24, 0.178, 0.201, 0.166, 0.203,
0.163, 0.147, 0.177, 0.155, 0.177, 0.133, 0.133, 0.182, 0.203,
0.161, 0.151, 0.171, 0.117, NA, 26, 26, 26, 26, 25, 26, 25, 26,
26, 26, 26, 25, 26, 26, 25, 26, 26, 26, 26, 26, 26, 26, 26, 25,
25, 24, 24, 24, 23, 23, 23, 24, 23)),
.Names = c("group", "date_time", "result"),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -132L))

最佳答案

可能这是 ggplotly 中的一个错误。 .
一个解决方案是使用 scales="free"里面 facet_wrap然后手动定义 4 个图的公共(public) x 轴。

p <- data %>%
filter(!is.na(result)) %>%
ggplot(aes(date_time, result)) +
facet_wrap(~group, scales = "free") +
geom_point() +
geom_line()

g <- plotly_build(p)
# Set "x3" axis as common x-axis of Plot 1 and 3
g$x$data[[1]]$xaxis <- "x3"
# Set "x4" axis as common x-axis of Plot 2 and 4
g$x$data[[2]]$xaxis <- "x4"
print(g)

enter image description here
其他解决方案是 herehere .

关于r - ggplotly 从单个方面删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46281534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com