【发布时间】:2022-01-02 20:52:23
【问题描述】:
我用ggmap() 和geom_sf() 创建了一个ggplot 图,但是当我尝试用ggplotly() 对其进行可视化时,有些元素没有被翻译。
-
似乎使用
scale_fill_manual()时的标签没有被plotly解释。 -
此外,标签必须格式化,如 ggplot 生成的图表所示,并按照 ggplot 中的标签参数
scale_fill_manual()进行修改。 -
标签应位于彩色框上。您也可以在使用 ggplot 生成的图表中看到。
-
添加情节的框架对我来说似乎不美观,我不希望它显示出来。
-
用ggplot2显示的字幕在绘图生成的图中找不到。
-
是否可以从图形中隐藏菜单(下载、缩放、...)?
-
最后,以不同的方式,当鼠标悬停时,我尝试向图形的每个区域添加信息,但它不显示任何内容,只显示变量 geosmunicipios$Renta.media.por.hogar 的值.2016.quantile,默认已经显示,在
ggplotly()中使用hoverinfo()。 -
它只显示我在第 7 点中通过将鼠标悬停在多边形的线上而不是在多边形区域中指示的默认文本。
简而言之,ggplotly 图表应尽可能接近使用 ggplot2 生成的图表。
下载文件位于: https://www.dropbox.com/s/9nmy0uj00jhc1y4/geosmunicipios.R?dl=0
> str(geosmunicipios)
Classes ‘sf’ and 'data.frame': 45 obs. of 21 variables:
$ CODIGOINE : chr "30001" "30002" "30003" "30004" ...
$ OBJECTID : num 577 578 579 580 581 582 583 584 585 586 ...
$ INSPIREID : chr "ES.IGN.SIGLIM34143030001" "ES.IGN.SIGLIM34143030002" "ES.IGN.SIGLIM34143030003" "ES.IGN.SIGLIM34143030004" ...
$ NATCODE : chr "34143030001" "34143030002" "34143030003" "34143030004" ...
$ NAMEUNIT : chr "Abanilla" "Abarán" "Águilas" "Albudeite" ...
$ CODNUT1 : chr "ES6" "ES6" "ES6" "ES6" ...
$ CODNUT2 : chr "ES62" "ES62" "ES62" "ES62" ...
$ CODNUT3 : chr "ES620" "ES620" "ES620" "ES620" ...
$ Shape__Are : num 0 0 0 0 0 0 0 0 0 0 ...
$ Shape__Len : num 0 0 0 0 0 0 0 0 0 0 ...
$ Unidades.territoriales : chr "30001 Abanilla" "30002 Abarán" "30003 Águilas" "30004 Albudeite" ...
$ Renta.media.por.persona.2016 : num 8444 8401 8269 7680 8465 ...
$ Renta.media.por.persona.2015 : num 8274 8338 7982 7458 8305 ...
$ Renta.media.por.hogar.2016 : num 21569 23597 23222 19855 24567 ...
$ Renta.media.por.hogar.2015 : num 21017 23522 22368 19713 23875 ...
$ CPRO : int 30 30 30 30 30 30 30 30 30 30 ...
$ CMUN : int 1 2 3 4 5 6 7 8 9 10 ...
$ DC : int 1 6 2 7 0 3 9 5 8 2 ...
$ NOMBRE : chr "Abanilla" "Abarán" "Águilas" "Albudeite" ...
$ geometry :sfc_MULTIPOLYGON of length 45; first list element: List of 1
..$ :List of 1
.. ..$ : num [1:27, 1:2] -1.14 -1.12 -1.1 -1.08 -1.06 ...
..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
$ Renta.media.por.hogar.2016.quantile: Factor w/ 4 levels "[1.98e+04,2.19e+04)",..: 1 3 2 1 4 4 3 4 3 2 ...
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:20] "CODIGOINE" "OBJECTID" "INSPIREID" "NATCODE" ...
图表的R代码
library(ggplot2)
library(plotly)
dget("geosmunicipios.R")
quantile.interval = quantile(geosmunicipios$Renta.media.por.hogar.2016, probs = c(0.00, 0.25, 0.50, 0.75, 1.00))
geosmunicipios$Renta.media.por.hogar.2016.quantile = cut(geosmunicipios$Renta.media.por.hogar.2016, breaks=quantile.interval, right = FALSE, include.lowest = TRUE)
colors = c("#fee5d9","#fcae91","#fb6a4a","#de2d26")
cuartiles <- quantile(geosmunicipios$Renta.media.por.hogar.2016, probs = c(0.00, 0.25, 0.50, 0.75, 1.00))
rmurcia <- ggplot(data = geosmunicipios) +
geom_sf(
aes(
fill=Renta.media.por.hogar.2016.quantile
),
color="#FFFFFF",
size=0.5
) +
theme_void() +
scale_fill_manual(
values = colors,
labels = c(
paste("[1Q)\n", format(cuartiles[2], big.mark=".", decimal.mark=","), "€", sep=""),
paste("[2Q)\n", format(cuartiles[3], big.mark=".", decimal.mark=","), "€", sep=""),
paste("[3Q)\n", format(cuartiles[4], big.mark=".", decimal.mark=","), "€", sep=""),
paste("[4Q]\n", format(cuartiles[5], big.mark=".", decimal.mark=","), "€", sep="")
),
guide = guide_legend(
direction = "horizontal",
nrow = 1,
#title.position = "top",
label.position = "top",
label.hjust = 1,
keyheight = 0.75
)
) +
labs(
title = "Región de Murcia",
subtitle = "Renta media por hogar (2016)",
caption = "",
fill = "" # Etiqueta para la Leyenda
) +
theme(
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#ffffff", color = NA),
panel.background = element_rect(fill = "#ffffff", color = NA),
legend.background = element_rect(fill = "#ffffff", color = NA),
plot.title = element_text(size= 22, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
plot.subtitle = element_text(size= 17, hjust=0.5, color = "#4e4d47", margin = margin(b = -0.1, t = 0.43, l = 2, unit = "cm")),
plot.caption = element_text(size=12, color = "#4e4d47", margin = margin(b = 0.3, r=-99, unit = "cm") ),
#legend.position = c(0.85, 0.08)
legend.position = "bottom"
)
rmurcia
ggplotly(
rmurcia,
hoverinfo = 'text',
text = ~paste(
'</br> Municipio: ', NOMBRE,
'</br> Renta Hogar 2016: ', Renta.media.por.hogar.2016,
'</br> Renta Hogar 2015: ', Renta.media.por.hogar.2015
)
) %>%
layout(
legend = list(
orientation = "h",
xanchor = "center",
x = 0.5,
y = -0.01
)
)
【问题讨论】:
-
ggplot 和 plotly 之间没有一对一的“翻译”。并不是一个支持的所有东西都存在于另一个包中。此外,ggplotly 可能不支持某些功能。总而言之,我在这里没有看到任何让我感到惊讶的东西。