VegaLite.jl
VegaLite.jl copied to clipboard
cannot compute shape centroids for map labels
I'm unable to implement the geoCentroid()
function that is shown in the Vega docs. This is needed to be able to place labels on maps.
The geoCentroid()
function is a Vega command not a VegaLite command. As this package interfaces with VegaLite I would not expect that to work. I believe there is an experimental Vega interface but I could be wrong.
I think in theory these should work in vega-lite as well: vega-lite specs are compiled into vega specs under the hood, and I would have assumed that one can therefore use the same functions in expressions. @mthelm85 could you post the code you are trying to run? Another good debug technique is to export the spec you are creating to JSON (p |> save("foo.vegalite")
) and then try whether it works in the vega-lite online editor.
I have tried to implement this code but could not figure out how to do it - even to reproduce the bug it reports.
I want to plot the names of the countries in each country in the map below created by the following code code (I have renamed the uploaded files by appending .txt to be able to upload it): africa.topopjson.txt africa_output.csv.txt
using VegaLite #, VegaDatasets
using Queryverse
africa_output= "africa_output.csv"
africa = joinpath(".", "africa.topojson")
@vlplot(width=800, height=600) +
@vlplot(
mark={
:geoshape,
stroke=:black
},
data={
url=africa,
format={
typ=:topojson,
feature=:africa
}
},
transform=[{
lookup="properties.ISO_A3",
from={
data={
url=africa_output,
format={
typ=:csv
}
},
key=:iso3166_12,
fields=["count"]
}
}],
color={
"count:q",
scale={domain=[0, 250000], scheme=:blues},
legend={title="Output"}
},
projection={
typ=:naturalEarth1
}
)
It's a bit clunky, but this may solve the issue:
using VegaLite #, VegaDatasets
# using Queryverse
africa_output= "africa_output.csv"
africa = joinpath(".", "africa.topojson")
@vlplot(
width=800, height=600,
projection={typ=:naturalEarth1},
data={
url=africa,
format={
typ=:topojson,
feature=:africa
}
},
transform=[
{
lookup="properties.ISO_A3",
from={
data={
url=africa_output,
format={
typ=:csv
}
},
key=:iso3166_12,
fields=["count", "official_3"]
}
},
{
calculate="geoCentroid(null, datum)",
as="centroid"
},
{
calculate="datum.centroid[0]",
as="centroidx"
},
{
calculate="datum.centroid[1]",
as="centroidy"
}
]) +
@vlplot(color={
"count:q",
scale={domain=[0, 250000], scheme=:blues},
legend={title="Output"}
},
mark={
:geoshape,
stroke=:black
}) +
@vlplot(mark=:text , text="official_3", longitude=:centroidx, latitude=:centroidy)
It looks like a more elegant solution may be in the pipeline for vega-lite, but it also doesn't look like it's a particularly high priority. Will this work for now?