簡體   English   中英

如何在geom_bar ggplot2中添加第二個欄

[英]how to add a second bar in geom_bar ggplot2

我在下面的這篇文章中提到了一個data.frame。 我想填充geom_bar ggplot,並添加第二列,其位置=“ dodge”保持相同的填充。

有可能嗎?

謝謝

數據:

e11 <- data.frame(stringsAsFactors=FALSE,
        Data = c("NSY", "NSY", "NSY", "NSY", "NSY", "NSY", "NSY", "NSY", "NSY",
                 "NSY", "NSY", "NSY", "IEA", "IEA", "IEA", "IEA", "IEA", "IEA",
                 "IEA", "IEA", "IEA", "IEA", "IEA", "IEA"),
   pollutant = c("CO", "CO", "CO", "CO", "CO", "CO", "CO", "CO", "CO", "CO",
                 "CO", "CO", "CO", "CO", "CO", "CO", "CO", "CO", "CO", "CO",
                 "CO", "CO", "CO", "CO"),
         veh = c("Bus", "Bus", "LCV", "LCV", "MC", "MC", "PC", "PC", "Trucks",
                 "Trucks", "Taxi", "Taxi", "Bus", "Bus", "LCV", "LCV", "MC",
                 "MC", "PC", "PC", "Trucks", "Trucks", "Taxi", "Taxi"),
    province = c("Anshan", "Baicheng", "Anshan", "Baicheng", "Anshan",
                 "Baicheng", "Anshan", "Baicheng", "Anshan", "Baicheng",
                 "Anshan", "Baicheng", "Anshan", "Baicheng", "Anshan", "Baicheng",
                 "Anshan", "Baicheng", "Anshan", "Baicheng", "Anshan", "Baicheng",
                 "Anshan", "Baicheng"),
          V1 = c(2056951311, 821731458, 6172501, 2603243, 86352447, 103963364,
                 31727831557, 18939503839, 6479654273, 2969730580, 649016656,
                 398757325, 1532636252, 594877413, 3490977, 1481795, 61479188,
                 74017395, 22588836597, 13484103276, 3426409302, 1573954393,
                 462071641, 283897878)
)

第一情節

ggplot(e11,
       aes(x = province, y = V1,
           fill = veh)) +
  geom_bar(stat = "identity") +
  # facet_wrap(~Data)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, colour = "black"),
        legend.key.size = unit(1,"line"),
        text = element_text(size=20),
        legend.position = "bottom",
        axis.text.y = element_text(colour = "black"))

在此處輸入圖片說明

第二情節

ggplot(e11,
       aes(x = province, y = V1)) +
  geom_bar(stat = "identity", position = "dodge", aes(fill = Data)) +
  # facet_wrap(~Data)
  theme(axis.text.x = element_text(angle = 90, hjust = 1, colour = "black"),
        legend.key.size = unit(1,"line"),
        text = element_text(size=20),
        legend.position = "bottom",
        axis.text.y = element_text(colour = "black"))

在此處輸入圖片說明

我正在尋找這樣的東西: 第三情節

e11$Veh <- paste(e11$province, e11$Data)

ggplot(e11,
       aes(x = Veh, 
           y = V1,
           fill = veh)) +
  geom_bar(stat = "identity") +
  # facet_wrap(~Data)
  theme(axis.text.x = element_text(angle = 90, hjust = 1, colour = "black"),
        legend.key.size = unit(1,"line"),
        text = element_text(size=20),
        legend.position = "bottom",
        axis.text.y = element_text(colour = "black"))

在此處輸入圖片說明 PD:由於imgur在中國受到審查,Im使用GitHub鏈接向禁止imgur的中國人和其他用戶顯示圖片

我認為這接近我的想法,可能會幫助其他人發展

情節

ggplot(e11,
       aes(x = Data, y = V1,
           fill = veh)) +
  geom_bar(stat = "identity") +
  labs(x = NULL, y = NULL)+
  facet_wrap(~province, nrow = 1)+
  theme_bw()+
  theme(axis.text.x = element_text(hjust = 1, angle = 90, colour = "black"),
        legend.key.size = unit(1,"line"),
        text = element_text(size=22),
        legend.position = "bottom",
        axis.text.y = element_text(colour = "black"),
        strip.text.x = element_text(size = 18,  angle = 90),
        panel.spacing = unit(0, "lines"))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM