как добавить второй бар в geom_bar ggplot2 - PullRequest
0 голосов
/ 27 июня 2019

У меня есть data.frame, упомянутый в этом посте ниже.Я хотел бы заполнить ggplot geom_bar и добавить второй столбец с position = "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"))

enter image description here

Второй участок

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"))

enter image description here

I`Я ищу что-то вроде этого: Третий сюжет

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"))

enter image description here PD: Поскольку imgur подвергается цензуре в Китае, я использую ссылки GitHub, чтобы показать картинкикитайским и другим пользователям, где заблокирован imgur

1 Ответ

0 голосов
/ 28 июня 2019

Я думаю, что это близко к тому, что я, и может помочь другим развиваться

участок

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"))

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...