{
const svg = d3.create('svg')
.attr('width', widthPop)
.attr('height', heightPop);
const x = d3.scaleBand()
.domain(populations.map(d => d.country))
.range([marginPop.left, widthPop - marginPop.right])
.padding(0.2);
const y = d3.scaleLinear()
.domain([0, d3.max(populations, d => d.population)]).nice()
.range([heightPop - marginPop.bottom, marginPop.top]);
const xAxis = d3.axisBottom(x);
const yAxis = d3.axisLeft(y)
.tickFormat(d3.format(".2s"));
svg.append('g')
.selectAll('line')
.data(populations)
.join('line')
.attr('x1', d => x(d.country) + x.bandwidth() / 2)
.attr('x2', d => x(d.country) + x.bandwidth() / 2)
.attr('y1', y(0))
.attr('y2', d => y(d.population))
.attr('stroke', 'gray')
.attr('stroke-width', 2);
svg.append('g')
.selectAll('circle')
.data(populations)
.join('circle')
.attr('cx', d => x(d.country) + x.bandwidth() / 2)
.attr('cy', d => y(d.population))
.attr('r', 5)
.attr('fill', '#69b3a2')
.attr('stroke', 'black');
const avg = d3.mean(populations, d => d.population);
svg.append("line")
.attr("x1", marginPop.left)
.attr("x2", widthPop - marginPop.right)
.attr("y1", y(avg))
.attr("y2", y(avg))
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("stroke-dasharray", "4,4");
svg.append('g')
.attr('transform', `translate(${marginPop.left},0)`)
.call(yAxis)
.call(g => g.select('.domain').remove());
svg.append('g')
.attr('transform', `translate(0, ${heightPop - marginPop.bottom})`)
.call(xAxis)
.call(g => g.select('.domain').remove())
.selectAll('text')
.attr('transform', 'rotate(-45)')
.style('text-anchor', 'end');
svg.append('text')
.attr('fill', 'black')
.attr('font-family', 'sans-serif')
.attr('x', widthPop / 2)
.attr('y', heightPop - 5)
.attr('text-anchor', 'middle')
.text("País");
return svg.node();
}