我有以下数据(dotplot_data
)
structure(list(Kanton = structure(c(8L, 8L, 13L, 13L, 18L, 18L,
25L, 25L, 17L, 17L, 24L, 24L, 20L, 20L, 19L, 19L, 5L, 5L, 22L,
22L, 7L, 7L, 16L, 16L, 4L, 4L, 9L, 9L, 21L, 21L, 26L, 26L, 14L,
14L, 23L, 23L, 15L, 15L, 11L, 11L, 6L, 6L, 3L, 3L, 10L, 10L,
2L, 2L, 1L, 1L, 12L, 12L), .Label = c("GE", "NE", "VD", "BL",
"ZG", "TI", "SO", "ZH", "SH", "VS", "TG", "JU", "BE", "SG", "AG",
"BS", "SZ", "LU", "GL", "NW", "AR", "FR", "GR", "OW", "UR", "AI"
), class = "factor"), Jahr = structure(c(1L, 7L, 1L, 7L, 1L,
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L,
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L,
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L), .Label = c("2009",
"2010", "2011", "2012", "2013", "2014", "2015"), class = "factor"),
Verhältnis = c(0.18245716727989, 0.234567735045611, 0.24932014665891,
0.282991259307219, 0.291667314781689, 0.34810778972169, 0.434262510974539,
0.47438822065533, 0.290624277074589, 0.333502152443657, 0.405097486492835,
0.444295001120825, 0.318638617178079, 0.366800362271963,
0.344886447973876, 0.366559250036619, 0.158794683184927,
0.200541172129635, 0.310803517571986, 0.373802102210946,
0.17589414669752, 0.223276715892796, 0.29494992542084, 0.306316282635547,
0.194661570513323, 0.197384284340806, 0.193617140613607,
0.236622748050551, 0.33538817183587, 0.367132867132867, 0.444847450394709,
0.511295616914462, 0.243694965319972, 0.283000330472218,
0.326857179802159, 0.380211750671041, 0.253870012888241,
0.301849570094465, 0.213009063138175, 0.262879934417959,
0.166988156513917, 0.20723066336873, 0.121289379668724, 0.160165035560148,
0.214926378302262, 0.262849338486695, 0.125824437342762,
0.145818371743814, 0.0331743010407139, 0.0607599044903447,
0.229837043837503, 0.277344272528649), GDEKT = c("ZH", "ZH",
"BE", "BE", "LU", "LU", "UR", "UR", "SZ", "SZ", "OW", "OW",
"NW", "NW", "GL", "GL", "ZG", "ZG", "FR", "FR", "SO", "SO",
"BS", "BS", "BL", "BL", "SH", "SH", "AR", "AR", "AI", "AI",
"SG", "SG", "GR", "GR", "AG", "AG", "TG", "TG", "TI", "TI",
"VD", "VD", "VS", "VS", "NE", "NE", "GE", "GE", "JU", "JU"
), Verhältnis_label = c(" 18.2%", " 23.5%", " 24.9%",
" 28.3%", " 29.2%", " 34.8%", " 43.4%", " 47.4%",
" 29.1%", " 33.4%", " 40.5%", " 44.4%", " 31.9%",
" 36.7%", " 34.5%", " 36.7%", " 15.9%", " 20.1%",
" 31.1%", " 37.4%", " 17.6%", " 22.3%", " 29.5%",
" 30.6%", " 19.5%", " 19.7%", " 19.4%", " 23.7%",
" 33.5%", " 36.7%", " 44.5%", " 51.1%", " 24.4%",
" 28.3%", " 32.7%", " 38%", " 25.4%", " 30.2%",
" 21.3%", " 26.3%", " 16.7%", " 20.7%", " 12.1%",
" 16%", " 21.5%", " 26.3%", " 12.6%", " 14.6%",
" 3.3%", " 6.1%", " 23%", " 27.7%"), Kanton_label = c("ZH 18.2% ",
"ZH 23.5% ", "BE 24.9% ", "BE 28.3% ", "LU 29.2% ",
"LU 34.8% ", "UR 43.4% ", "UR 47.4% ", "SZ 29.1% ",
"SZ 33.4% ", "OW 40.5% ", "OW 44.4% ", "NW 31.9% ",
"NW 36.7% ", "GL 34.5% ", "GL 36.7% ", "ZG 15.9% ",
"ZG 20.1% ", "FR 31.1% ", "FR 37.4% ", "SO 17.6% ",
"SO 22.3% ", "BS 29.5% ", "BS 30.6% ", "BL 19.5% ",
"BL 19.7% ", "SH 19.4% ", "SH 23.7% ", "AR 33.5% ",
"AR 36.7% ", "AI 44.5% ", "AI 51.1% ", "SG 24.4% ",
"SG 28.3% ", "GR 32.7% ", "GR 38% ", "AG 25.4% ",
"AG 30.2% ", "TG 21.3% ", "TG 26.3% ", "TI 16.7% ",
"TI 20.7% ", "VD 12.1% ", "VD 16% ", "VS 21.5% ",
"VS 26.3% ", "NE 12.6% ", "NE 14.6% ", "GE 3.3% ",
"GE 6.1% ", "JU 23% ", "JU 27.7% "), hjust = c(1.2,
0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0,
1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2,
0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0,
1.2, 0, 1.2, 0, 1.2, 0)), .Names = c("Kanton", "Jahr", "Verhältnis",
"GDEKT", "Verhältnis_label", "Kanton_label", "hjust"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -52L))
根据该数据,我使用ggplot2
生成以下点图。
代码:
p <- ggplot(dotplot_data) +
geom_path_interactive(aes(x = Verhältnis, y = Kanton, group = Kanton), colour = "#AAAAAA"
# arrow = arrow(length = unit(0.2, "cm"), type = "closed")
) +
geom_point(aes(x = Verhältnis, y = Kanton, colour = Jahr)) +
geom_text(data = subset(dotplot_data, Jahr == 2015),
aes(x = Verhältnis, y = Kanton, label = Verhältnis_label),
hjust = 0, colour = "#555555", size = 4) +
geom_text(data = subset(dotplot_data, Jahr == 2009),
aes(x = Verhältnis, y = Kanton, label = Kanton_label),
hjust = 1, colour = "#555555", size = 4) +
scale_x_continuous(labels = percent, limits = c(-.05,.6)) +
scale_colour_discrete(guide = F) +
labs(x = NULL,
y = NULL,
title = "Anteil umweltfreundlicher Heizungen",
subtitle = "2009, 2015",
caption = "") +
xlim(c(-.05, .6)) +
theme_minimal(
# axis.text = element_blank(),
# axis.ticks = element_blank()
)
ggsave(p, filename = "output/dotplot.png", width = 7, height = 5)
现在我想将该图表保存为(响应式)HTML,因此我可以将它包含在桌面和移动设备上(水平收缩)。
我遇到了plotly
包,因为它生成了自动调整大小的图(但不是1:1,例如,在ggiraph
中)。
实际上,我只将它用于此目的。我不知道它是否是最好的工具,或者是否有更好的解决方案。也让我失望的是每个情节(~2MB)附带的相当大的库src文件。
无论如何,这里是代码:
ply <- ggplotly(p, collaborate = F, doubleClick = F, displayModeBar = F)
plotly::config(ply, collaborate = F, doubleClick = F, displayModeBar = F)
saveWidget(ply, "test.html", libdir = "src", selfcontained = F)
有了这个我得到一个情节html文件响应但有工具提示和右上角的丑陋栏,我想知道我是否可以禁用该剧情的所有互动功能。正如你所看到的,我已经尝试过使用一些配置选项,但不管怎么说它们都没有用,或者我用的方法不对。
非常感谢任何方向 - 也适用于其他包装或工作流程。
答案 0 :(得分:4)
在Plotly中禁用图例切换当前为not possible,但您可以手动执行此操作。
prependContent(htmltools::tags$style('
.plot-container .legend .traces .legendtoggle {display: none;}
.plot-container .legend .traces .legendtext {cursor: default;}'))
您获得了plotly::config
调用权限,但需要将其分配给plotly
对象才能保存更改。
ply <- plotly::config(ply, collaborate = F, doubleClick = F, displayModeBar = F)
出于美观原因,让我们将标签移离线条,移除无用的hoverinfo
ply[['x']][['data']][[5]][['x']] <- ply[['x']][['data']][[5]][['x']] - 0.05
ply[['x']][['data']][[4]][['x']] <- ply[['x']][['data']][[4]][['x']] + 0.05
ply[['x']][['data']][[4]][['hoverinfo']] <- 'none'
ply[['x']][['data']][[5]][['hoverinfo']] <- 'none'
关于Plotly Javascript库的大小,您可以指向https://cdn.plot.ly/plotly-latest.min.js
而不是自己提供它,希望其中一个用户访问过之前使用Plotly的网站,并且浏览器仍然在缓存中。
library(plotly)
library(ggiraph)
library(htmlwidgets)
dotplot_data <- structure(list(Kanton = structure(c(8L, 8L, 13L, 13L, 18L, 18L,
25L, 25L, 17L, 17L, 24L, 24L, 20L, 20L, 19L, 19L, 5L, 5L, 22L,
22L, 7L, 7L, 16L, 16L, 4L, 4L, 9L, 9L, 21L, 21L, 26L, 26L, 14L,
14L, 23L, 23L, 15L, 15L, 11L, 11L, 6L, 6L, 3L, 3L, 10L, 10L,
2L, 2L, 1L, 1L, 12L, 12L), .Label = c("GE", "NE", "VD", "BL",
"ZG", "TI", "SO", "ZH", "SH", "VS", "TG", "JU", "BE", "SG", "AG",
"BS", "SZ", "LU", "GL", "NW", "AR", "FR", "GR", "OW", "UR", "AI"
), class = "factor"), Jahr = structure(c(1L, 7L, 1L, 7L, 1L,
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L,
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L,
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L), .Label = c("2009",
"2010", "2011", "2012", "2013", "2014", "2015"), class = "factor"),
Verhältnis = c(0.18245716727989, 0.234567735045611, 0.24932014665891,
0.282991259307219, 0.291667314781689, 0.34810778972169, 0.434262510974539,
0.47438822065533, 0.290624277074589, 0.333502152443657, 0.405097486492835,
0.444295001120825, 0.318638617178079, 0.366800362271963,
0.344886447973876, 0.366559250036619, 0.158794683184927,
0.200541172129635, 0.310803517571986, 0.373802102210946,
0.17589414669752, 0.223276715892796, 0.29494992542084, 0.306316282635547,
0.194661570513323, 0.197384284340806, 0.193617140613607,
0.236622748050551, 0.33538817183587, 0.367132867132867, 0.444847450394709,
0.511295616914462, 0.243694965319972, 0.283000330472218,
0.326857179802159, 0.380211750671041, 0.253870012888241,
0.301849570094465, 0.213009063138175, 0.262879934417959,
0.166988156513917, 0.20723066336873, 0.121289379668724, 0.160165035560148,
0.214926378302262, 0.262849338486695, 0.125824437342762,
0.145818371743814, 0.0331743010407139, 0.0607599044903447,
0.229837043837503, 0.277344272528649), GDEKT = c("ZH", "ZH",
"BE", "BE", "LU", "LU", "UR", "UR", "SZ", "SZ", "OW", "OW",
"NW", "NW", "GL", "GL", "ZG", "ZG", "FR", "FR", "SO", "SO",
"BS", "BS", "BL", "BL", "SH", "SH", "AR", "AR", "AI", "AI",
"SG", "SG", "GR", "GR", "AG", "AG", "TG", "TG", "TI", "TI",
"VD", "VD", "VS", "VS", "NE", "NE", "GE", "GE", "JU", "JU"
), Verhältnis_label = c(" 18.2%", " 23.5%", " 24.9%",
" 28.3%", " 29.2%", " 34.8%", " 43.4%", " 47.4%",
" 29.1%", " 33.4%", " 40.5%", " 44.4%", " 31.9%",
" 36.7%", " 34.5%", " 36.7%", " 15.9%", " 20.1%",
" 31.1%", " 37.4%", " 17.6%", " 22.3%", " 29.5%",
" 30.6%", " 19.5%", " 19.7%", " 19.4%", " 23.7%",
" 33.5%", " 36.7%", " 44.5%", " 51.1%", " 24.4%",
" 28.3%", " 32.7%", " 38%", " 25.4%", " 30.2%",
" 21.3%", " 26.3%", " 16.7%", " 20.7%", " 12.1%",
" 16%", " 21.5%", " 26.3%", " 12.6%", " 14.6%",
" 3.3%", " 6.1%", " 23%", " 27.7%"), Kanton_label = c("ZH 18.2% ",
"ZH 23.5% ", "BE 24.9% ", "BE 28.3% ", "LU 29.2% ",
"LU 34.8% ", "UR 43.4% ", "UR 47.4% ", "SZ 29.1% ",
"SZ 33.4% ", "OW 40.5% ", "OW 44.4% ", "NW 31.9% ",
"NW 36.7% ", "GL 34.5% ", "GL 36.7% ", "ZG 15.9% ",
"ZG 20.1% ", "FR 31.1% ", "FR 37.4% ", "SO 17.6% ",
"SO 22.3% ", "BS 29.5% ", "BS 30.6% ", "BL 19.5% ",
"BL 19.7% ", "SH 19.4% ", "SH 23.7% ", "AR 33.5% ",
"AR 36.7% ", "AI 44.5% ", "AI 51.1% ", "SG 24.4% ",
"SG 28.3% ", "GR 32.7% ", "GR 38% ", "AG 25.4% ",
"AG 30.2% ", "TG 21.3% ", "TG 26.3% ", "TI 16.7% ",
"TI 20.7% ", "VD 12.1% ", "VD 16% ", "VS 21.5% ",
"VS 26.3% ", "NE 12.6% ", "NE 14.6% ", "GE 3.3% ",
"GE 6.1% ", "JU 23% ", "JU 27.7% "), hjust = c(1.2,
0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0,
1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2,
0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0,
1.2, 0, 1.2, 0, 1.2, 0)), .Names = c("Kanton", "Jahr", "Verhältnis",
"GDEKT", "Verhältnis_label", "Kanton_label", "hjust"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -52L))
p <- ggplot(dotplot_data) +
geom_path_interactive(aes(x = Verhältnis, y = Kanton, group = Kanton), colour = "#AAAAAA"
# arrow = arrow(length = unit(0.2, "cm"), type = "closed")
) +
geom_point(aes(x = Verhältnis, y = Kanton, colour = Jahr)) +
geom_text(data = subset(dotplot_data, Jahr == 2015),
aes(x = Verhältnis, y = Kanton, label = Verhältnis_label),
hjust = 0, colour = "#555555", size = 4) +
geom_text(data = subset(dotplot_data, Jahr == 2009),
aes(x = Verhältnis, y = Kanton, label = Kanton_label),
hjust = 1, colour = "#555555", size = 4) +
scale_x_continuous(labels = percent, limits = c(-.05,.6)) +
scale_colour_discrete(guide = F) +
labs(x = NULL,
y = NULL,
title = "Anteil umweltfreundlicher Heizungen",
subtitle = "2009, 2015",
caption = "") +
xlim(c(-.05, .6)) +
theme_minimal(
# axis.text = element_blank(),
# axis.ticks = element_blank()
)
p
ply <- ggplotly(p) %>%
prependContent(htmltools::tags$style('
.plot-container .legend .traces .legendtoggle {display: none;}
.plot-container .legend .traces .legendtext {cursor: default;}')
)
ply <- plotly::config(ply, collaborate = F, doubleClick = F, displayModeBar = F)
ply[['x']][['data']][[5]][['x']] <- ply[['x']][['data']][[5]][['x']] - 0.05
ply[['x']][['data']][[4]][['x']] <- ply[['x']][['data']][[4]][['x']] + 0.05
ply[['x']][['data']][[4]][['hoverinfo']] <- 'none'
ply[['x']][['data']][[5]][['hoverinfo']] <- 'none'
ply
saveWidget(ply, "test.html", libdir = "src", selfcontained = F)