我注意到,当使用该函数将ggplot2绘图转换为交互式plotly绘图时ggplotly,可能会发生奇怪的事情.
我正在绘制一个"Punchcard情节",这是一种很好的方式来呈现数据集的4个维度:
df <- data.frame(cat1 = rep(c("a","b","c"), 3), cat2 = c(rep("A", 3),
rep("B", 3), rep("C", 3)), var1 = 1:9, var2 = 10:18)
ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +
geom_point(shape=21)
Run Code Online (Sandbox Code Playgroud)
但是,当我ggplotly用来转换为交互式时,plotly只呈现一个传说:
p <- ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +
geom_point(shape=21)
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
为什么这样plotly做,我该如何避免这种行为?
看到我遇到越来越多这些奇怪的事情 - 任何人都有一个链接到某个地方我可以阅读如何ggplotly工作,以某种方式我可以在将来自己解决这些问题?
第二个图例在转换期间丢失(或者至少我在数据中找不到).您可以查看结果ggplotly并修改从原始数据到布局的gp[['x']][['layout']]所有内容,例如,包含从中传递的所有布局变量ggplotly.
更多代码行,但您可以完全控制图形的所有方面.
library(plotly)
df <- data.frame(cat1 = rep(c("a","b","c"), 3),
cat2 = c(rep("A", 3),
rep("B", 3),
rep("C", 3)),
var1 = 1:9,
var2 = 10:18)
size_multi <- 2 #multiplies your size to avoid pixel sized objects
color_scale <- list(c(0, "#000000"), list(1, "#00BFFF"))
p <- plot_ly(df,
type='scatter',
mode='markers',
x = ~cat1,
y = ~cat2,
marker = list(color = ~var2,
size=~var1 * size_multi,
colorscale = color_scale,
colorbar = list(len = 0.8, y = 0.3),
line = list(color = ~var2,
colorscale = color_scale,
width = 2)
),
showlegend = F)
#adds some dummy traces for the punch card markers
markers = c(min(df$var1), mean(df$var1), max(df$var1))
for (i in 1:3) {
p <- add_trace(p,
df,
type = 'scatter',
mode = 'markers',
showlegend = T,
name = markers[[i]],
x = 'x',
y = 'x',
marker = list(size = markers[[i]] * size_multi,
color='rgba(255,255,255,0)',
showscale = F,
line = list(color = 'rgba(0,0,0,1)',
width = 2))
)
}
#fix the coordinate system
spacer <- 0.2
p <- layout(p, xaxis=list(range=c(-spacer, length(levels(df$cat1)) - 1 + spacer)), yaxis=list(range=c(-spacer, length(levels(df$cat1)) - 1 + spacer)))
p
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
720 次 |
| 最近记录: |