我的 ggplot 条形图的条形内的这些红线是什么?

what are these gray lines inside the bars of my ggplot bargraph?

我想创建一个图表来显示多年来哪个大陆的人均二氧化碳排放总量最高, 所以我这样编码:

barchart <- ggplot(data = worldContinents, 
                   aes(x = Continent, 
                   y = `Per.Capita`, 
                   colour = Continent)) 
         + geom_bar(stat = "identity")
barchart

这是我的数据框:

Geometry只是为了以后做一些地理映射。

我检查了 which(is.na(worldContinents$Per.Capita)) 以查看是否有 NA 个值,但它没有返回任何内容 是什么导致这些灰色线条? 我该如何摆脱它们?

这些是条形图中的灰线

谢谢

你有几个问题。首先,我猜您希望 fill 映射到大陆,而不是 color,它只控制条形轮廓的颜色。

其次,您的数据中每个大洲都有多个值,因此它们只是相互叠加。这就是条形图中出现线条的原因,可能不是您想要的。如果你想要每个大洲的人均平均值,你要么需要事先汇总你的数据,要么像这样使用stat_summary

barchart <- ggplot(data = worldContinents, 
                   aes(x = Continent, 
                       y = `Per.Capita`, 
                       fill = Continent))  +
            stat_summary(geom = "col", fun = mean, width = 0.7,
                         col = "gray50") +
            theme_light(base_size = 20) +
            scale_fill_brewer(palette = "Spectral")
barchart


使用的数据

显然,我们没有您的数据,所以我使用了 gapminder 数据集的修改版本来匹配您自己的数据结构

worldContinents <- gapminder::gapminder
worldContinents$Per.Capita <- worldContinents$gdpPercap
worldContinents$Continent <- worldContinents$continent
worldContinents <- worldContinents[worldContinents$year == 2007,]