R 中的 Ggplot2 在创建重叠的人口统计金字塔时给出不正确的着色

Ggplot2 in R gives incorrect coloring when creating overlapping demographic pyramids

我正在使用 ggplot2 库在 R 中创建一个重叠的人口统计金字塔,以比较来自两个不同来源的人口统计数据。

不过,我 运行 遇到了 ggplot2 的问题以及使用 alpha 参数时的着色问题。我试图理解 ggplot2 和 geom_bar 结构,但到目前为止,我一无所获。交易是绘制四个 geom_bars,其中两个 geom_bars 相互重叠(分别为男性和女性)。如果我不需要使用 alpha 来证明我的数据差异,我不会有任何问题。

如果我在这里出错,我将不胜感激。作为一名 R 程序员,我非常接近初学者,如果我的代码看起来很奇怪,请多多包涵。

下面是我的代码,生成的图像也如下所示。对于这个问题,我已将我的人口统计数据更改为随机数据。

library(ggplot2)

# Here I randomise my data for Whosebug
poptest<-data.frame(matrix(NA, nrow = 101, ncol = 5))
poptest[,1]<- seq(0,100)
poptest[,2]<- rpois(n = 101, lambda = 100)
poptest[,3]<- rpois(n = 101, lambda = 100)
poptest[,4]<- rpois(n = 101, lambda = 100)
poptest[,5]<- rpois(n = 101, lambda = 100)
colnames(poptest) <- c("age","A_males", "A_females","B_males", "B_females")

myLimits<-c(-250,250)
myBreaks<-seq(-250,250,50)

# Plot demographic pyramid
poptestPlot <- ggplot(data = poptest) +
  geom_bar(aes(age,A_females,fill="black"), stat = "identity", alpha=0.75, position = "identity")+
  geom_bar(aes(age,-A_males, fill="black"), stat = "identity", alpha=0.75, position="identity")+
  geom_bar(aes(age,B_females, fill="white"), stat = "identity", alpha=0.5, position="identity")+
  geom_bar(aes(age,-B_males, fill="white"), stat = "identity", alpha=0.5, position="identity")+
  coord_flip()+

  #set the y-axis which (because of the flip) shows as the x-axis
  scale_y_continuous(name = "",
                     limits = myLimits,
                     breaks = myBreaks,
                     #give the values on the y-axis a name, to remove the negatives
                     #give abs() command to remove negative values
                     labels = paste0(as.character(abs(myBreaks))))+

  #set the x-axis which (because of the flip) shows as the y-axis
  scale_x_continuous(name = "age",breaks=seq(0,100,5)) +
  #remove the legend
  theme(legend.position = 'none')+

  # Annotate geom_bars
  annotate("text", x = 100, y = -200, label = "males",size=6)+
  annotate("text", x = 100, y = 200, label = "females",size=6)

# show results in a separate window
x11()
print(poptestPlot) 

这是我得到的结果:(抱歉,作为 Whosebug 新手,我无法嵌入我的图片)

Ggplot2 result

涂色真是无厘头。黑不黑,白不白。相反,它可能会使用某种默认着色,因为 R 或 ggplot2 无法解释我的代码。

我欢迎任何和所有的回答。谢谢。

您正在尝试将 "black" 映射到数据点。这意味着您必须添加一个手动比例尺并告诉 ggplot 以颜色 "black" 为 "black" 的每个实例着色。有一个名为 scale_colour_identity 的快捷方式。但是,如果这是您唯一的关卡,那么在 aes 之外使用填充会容易得多。这样整个geom分别用黑色或者白色填充:

poptestPlot <- ggplot(data = poptest) +
  geom_bar(aes(age,A_females),fill="black", stat = "identity", alpha=0.75, position = "identity")+
  geom_bar(aes(age,-A_males), fill="black", stat = "identity", alpha=0.75, position="identity")+
  geom_bar(aes(age,B_females), fill="white", stat = "identity", alpha=0.5, position="identity")+
  geom_bar(aes(age,-B_males), fill="white", stat = "identity", alpha=0.5, position="identity")+
  coord_flip()+

  #set the y-axis which (because of the flip) shows as the x-axis
  scale_y_continuous(name = "",
                     limits = myLimits,
                     breaks = myBreaks,
                     #give the values on the y-axis a name, to remove the negatives
                     #give abs() command to remove negative values
                     labels = paste0(as.character(abs(myBreaks))))+

  #set the x-axis which (because of the flip) shows as the y-axis
  scale_x_continuous(name = "age",breaks=seq(0,100,5)) +
  #remove the legend
  theme(legend.position = 'none')+

  # Annotate geom_bars
  annotate("text", x = 100, y = -200, label = "males",size=6)+
  annotate("text", x = 100, y = 200, label = "females",size=6)