首页 > 解决方案 > 将来自其他数据集的多条线添加到 R 中的堆叠条形图

问题描述

我正在尝试找到有关如何在现有 ggplot 条形图上添加多行的解决方案。我知道如何在条形图上添加线条,以防所有数据都来自一个数据框,以及如何将来自其他数据集的线条添加到一个简单的条形图中,但我正在努力将数据集 A 中的线条添加到数据集 B 中的堆叠条形图中。

我的堆积条形图是这样创建的:

report_Official <- structure(list(Competency.Official.Rating = structure(c(1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
), .Label = c("demonstrates the value", "development area", "role model"
), class = "factor"), Competency.Name = structure(c(1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(305L, 114L, 70L, 352L, 72L, 80L, 
333L, 88L, 80L, 293L, 38L, 167L, 313L, 20L, 171L, 358L, 59L, 
85L)), class = "data.frame", row.names = c(NA, -18L))

ggplot(report_Official, aes(x = Competency.Name, y = Freq, 
                            fill = factor(Competency.Official.Rating, levels = c("role model", "demonstrates the value", "development area")),
                            label = Freq)) +
  geom_bar(stat = "identity") +  # position = fill will give the %; stack will give #of people
  geom_text(size = 5, position = position_stack(vjust = 0.5))   + 
#   facet_wrap(  ~ Subject.Direction) +
  labs(x = "Competence Name", 
       y = "Number of Employees") + # or Percentage of Employees [%] or Number of Employees
  theme_bw()  +
  ggtitle("Behavior Official Rating") +
  theme(panel.grid.major.x = element_blank(),
          panel.grid.major.y = element_line(colour = "grey50"),
          plot.title = element_text(size = rel(1.5),
                                    face = "bold", vjust = 1.5),
          axis.title = element_text(face = "bold"),
          legend.key.size = unit(0.5, "cm"),
          #legend.box.background = element_rect(color="red", size=2),
          #legend.box.margin = margin(116, 6, 6, 6),
          legend.key = element_rect(colour = "gray"),
          axis.title.y = element_text(vjust= 1.8),
          axis.title.x = element_text(vjust= -0.5)) +
  # scale_fill_manual(name="Manager Rating", values = wes_palette("Zissou1", n = 5)) +
  scale_fill_brewer(palette = "PuBu")+ #Oranges for Values
  guides(fill=guide_legend(title="Competency Official Rating")) +
   theme(panel.grid.major = element_blank(), text = element_text(size=15), panel.grid.minor = element_blank(),
         panel.background = element_blank(), axis.line = element_line(colour = "black")) +
   theme(axis.text.x = element_text(face = "bold",
                                     size = 15, angle = 45, hjust = 1),
          axis.text.y = element_text(face = "bold",
                                     size = 15, angle = 90))

我要添加的行来自这三个数据集:

rm <- structure(list(Competency.Self.Rating = structure(c(3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("demonstrates the value", "development area", 
"role model"), class = "factor"), Competency.Name = structure(1:6, .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(75L, 94L, 113L, 180L, 189L, 116L
)), row.names = c(3L, 6L, 9L, 12L, 15L, 18L), class = "data.frame")

dv <- structure(list(Competency.Self.Rating = structure(c(1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("demonstrates the value", "development area", 
"role model"), class = "factor"), Competency.Name = structure(1:6, .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(309L, 337L, 334L, 286L, 294L, 
338L)), row.names = c(1L, 4L, 7L, 10L, 13L, 16L), class = "data.frame")

da <- structure(list(Competency.Self.Rating = structure(c(2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("demonstrates the value", "development area", 
"role model"), class = "factor"), Competency.Name = structure(1:6, .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(105L, 73L, 54L, 32L, 21L, 48L)), row.names = c(2L, 
5L, 8L, 11L, 14L, 17L), class = "data.frame")

我尝试将行添加到我的 ggplot 中geom_point(data = rm, mapping = aes(x = Competency.Name, y = Freq))geom_point(data = dv, mapping = aes(x = Competency.Name, y = Freq))并将geom_point(data = da, mapping = aes(x = Competency.Name, y = Freq))这些行添加到我的堆叠条形图中,但它给了我一个错误

Error in factor(Competency.Official.Rating, levels = c("role model", "demonstrates the value",  : 
  object 'Competency.Official.Rating' not found

任何人都知道如何将数据集 rm、da 和 dv 中的线绘制到堆叠的条形图上?先感谢您!

标签: rggplot2stacked-chart

解决方案


你有几个选项来解决这个问题。正如评论所述,将填充移出全局美学将防止该错误。这可能是最好的方法,也是最简单的方法。如果您不想这样做,您可以将NULL所有其他几何图形的填充属性设置为,或者将inherit.aes参数设置FALSE为它们。如果inherit.aes设置为FALSE,则您需要为每个提供数据 x 和 y。在下面的代码中,我将填充移到了aes()in geom_bar()。这些数据集的点用 加上geom_point(),线用 加上geom_line()

ggplot(report_Official, aes(x = Competency.Name, 
                            y = Freq, 
                            label = Freq)) +
  geom_bar(data = report_Official,
           aes(fill = factor(Competency.Official.Rating,
                             
                             levels = c("role model", "demonstrates the value", "development area"))),
           stat = "identity") +  # position = fill will give the %; stack will give #of people
  geom_text(size = 5,
            aes(group = factor(Competency.Official.Rating,
                               
                               levels = c("role model", "demonstrates the value", "development area"))), 
            position = position_stack(vjust = 0.5))   + 
  geom_point(data = rm,
             color = "black") +
  geom_line(data = rm, 
            aes(group = 1)) +
  geom_point(data = dv,
             color = "red") +
  geom_line(data = dv,
            aes(group = 1),
            color = "red") +
  geom_point(data = da,
             color = "blue") +
  geom_line(data = da,
            aes(group = 1),
            color = "blue") +
  labs(x = "Competence Name", 
       y = "Number of Employees") + 
  theme_bw()  +
  ggtitle("Behavior Official Rating") +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(colour = "grey50"),
        plot.title = element_text(size = rel(1.5),
                                  face = "bold", vjust = 1.5),
        axis.title = element_text(face = "bold"),
        legend.key.size = unit(0.5, "cm"),
        
        legend.key = element_rect(colour = "gray"),
        axis.title.y = element_text(vjust= 1.8),
        axis.title.x = element_text(vjust= -0.5)) +
  scale_fill_brewer(palette = "PuBu")+ #Oranges for Values
  guides(fill=guide_legend(title="Competency Official Rating")) +
  theme(panel.grid.major = element_blank(), text = element_text(size=15), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text.x = element_text(face = "bold",
                                   size = 15, angle = 45, hjust = 1),
        axis.text.y = element_text(face = "bold",
                                   size = 15, angle = 90))

结果:

在此处输入图像描述

编辑:要为线条中绘制的每个数据集添加图例,您需要将它们合并为单个数据集,并带有一个表示它们来自哪里的列。

dv$set <- "dv"
da$set <- "da"
rm$set <- "rm"
line_df <- rbind(dv, da, rm)

然后,您可以将它们组合geom_line()成一个调用。

geom_line(data = line_df,
          aes(color = set,
              group = set)) +

在此处输入图像描述


推荐阅读