R 交互式图形 - 删除一个因素 level\element 并让轴适应
R interactive graph - remove a factor level\element and have axes adapt
我希望能够在 R 中呈现交互式图形,它呈现 lines/bars/whatever 按特定因素分组的度量,并能够使一个或多个因素的级别(组)消失并让 x 轴和 y 轴适应它,响应这个选择。
示例:
df <- data.frame(factor1 = c(rep("a", 3), rep("b", 3), rep("c", 3)),
xAxisVar = c(1:7, 5, 9),
yAxisVar = c(1:7, 5, 25))
ggplot(df, aes(xAxisVar, yAxisVar, group = factor1, color = factor1)) +
geom_line() +
geom_point()
在此图中,y 轴扩展到 25,因为在因子水平 "c" 中有 1 "large" 个观察值。我希望能够按下 "c" 或将其过滤掉并让 y 轴响应,重新渲染达到 6 左右的图。
我试过 plotly
的 ggplotly
,你可以自动让组 "c" 消失,但没有重新渲染情节来解决这个问题。建议?
你提到了ggplotly
,所以如果你对plotly
持开放态度,你可以试试:
library(plotly)
library(reshape2)
#from long to wide
df_wide <- dcast(df, xAxisVar ~ factor1, value.var="yAxisVar")
plot_ly(df_wide, x = ~xAxisVar, y = ~a, name='a', type='scatter', mode='lines+markers') %>%
add_trace(y = ~b, name = 'b', type='scatter', mode='lines+markers') %>%
add_trace(y = ~c, name = 'c', type='scatter', mode='lines+markers', connectgaps = TRUE) %>%
layout(
updatemenus = list(
list(
type = "buttons",
x = -0.1,
y = 0.7,
label = 'Category',
buttons = list(
list(method = "restyle",
args = list('visible', c(TRUE, FALSE, FALSE)),
label = "a"),
list(method = "restyle",
args = list('visible', c(FALSE, TRUE, FALSE)),
label = "b"),
list(method = "restyle",
args = list('visible', c(FALSE, FALSE, TRUE)),
label = "c")
)
)))
我希望能够在 R 中呈现交互式图形,它呈现 lines/bars/whatever 按特定因素分组的度量,并能够使一个或多个因素的级别(组)消失并让 x 轴和 y 轴适应它,响应这个选择。
示例:
df <- data.frame(factor1 = c(rep("a", 3), rep("b", 3), rep("c", 3)),
xAxisVar = c(1:7, 5, 9),
yAxisVar = c(1:7, 5, 25))
ggplot(df, aes(xAxisVar, yAxisVar, group = factor1, color = factor1)) +
geom_line() +
geom_point()
在此图中,y 轴扩展到 25,因为在因子水平 "c" 中有 1 "large" 个观察值。我希望能够按下 "c" 或将其过滤掉并让 y 轴响应,重新渲染达到 6 左右的图。
我试过 plotly
的 ggplotly
,你可以自动让组 "c" 消失,但没有重新渲染情节来解决这个问题。建议?
你提到了ggplotly
,所以如果你对plotly
持开放态度,你可以试试:
library(plotly)
library(reshape2)
#from long to wide
df_wide <- dcast(df, xAxisVar ~ factor1, value.var="yAxisVar")
plot_ly(df_wide, x = ~xAxisVar, y = ~a, name='a', type='scatter', mode='lines+markers') %>%
add_trace(y = ~b, name = 'b', type='scatter', mode='lines+markers') %>%
add_trace(y = ~c, name = 'c', type='scatter', mode='lines+markers', connectgaps = TRUE) %>%
layout(
updatemenus = list(
list(
type = "buttons",
x = -0.1,
y = 0.7,
label = 'Category',
buttons = list(
list(method = "restyle",
args = list('visible', c(TRUE, FALSE, FALSE)),
label = "a"),
list(method = "restyle",
args = list('visible', c(FALSE, TRUE, FALSE)),
label = "b"),
list(method = "restyle",
args = list('visible', c(FALSE, FALSE, TRUE)),
label = "c")
)
)))