1

This is my dataframe.

    Month NY_Month_Freq Chi_Month_Freq LA_Month_Freq Aus_Month_Freq
1      1           593            204           188             67
2      2           331            174           166             58
3      3           371            211           170             65
4      4           320            219           190             80
5      5           442            222           195             92
6      6           414            236           182             64
7      7           434            245           176             82
8      8           364            250           186             77
9      9           379            216           175             61
10    10           378            205           195             80
11    11           339            203           183             66
12    12           355            185           175             86

I want to plot the different frequencies by month, given the 4 cities.

1

2 Answers 2

1

Try reshaping your data:

library(tidyverse)
#Code
df %>% pivot_longer(-Month) %>%
  ggplot(aes(x=factor(Month),y=value,fill=name))+
  geom_bar(stat = 'identity',position = position_dodge(0.9))+
  labs(fill='city')+
  theme(legend.position = 'top')+xlab('Month')

Output:

enter image description here

Some data used:

#Data
df <- structure(list(Month = 1:12, NY_Month_Freq = c(593L, 331L, 371L, 
320L, 442L, 414L, 434L, 364L, 379L, 378L, 339L, 355L), Chi_Month_Freq = c(204L, 
174L, 211L, 219L, 222L, 236L, 245L, 250L, 216L, 205L, 203L, 185L
), LA_Month_Freq = c(188L, 166L, 170L, 190L, 195L, 182L, 176L, 
186L, 175L, 195L, 183L, 175L), Aus_Month_Freq = c(67L, 58L, 65L, 
80L, 92L, 64L, 82L, 77L, 61L, 80L, 66L, 86L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))
Sign up to request clarification or add additional context in comments.

Comments

0

Same as the answer from @Duck mostly. I didn't see it until after I typed it up.

library(tidyverse)

df <- tibble::tribble(
        ~Month, ~NY_Month_Freq, ~Chi_Month_Freq, ~LA_Month_Freq, ~Aus_Month_Freq,
            1L,           593L,            204L,           188L,             67L,
            2L,           331L,            174L,           166L,             58L,
            3L,           371L,            211L,           170L,             65L,
            4L,           320L,            219L,           190L,             80L,
            5L,           442L,            222L,           195L,             92L,
            6L,           414L,            236L,           182L,             64L,
            7L,           434L,            245L,           176L,             82L,
            8L,           364L,            250L,           186L,             77L,
            9L,           379L,            216L,           175L,             61L,
           10L,           378L,            205L,           195L,             80L,
           11L,           339L,            203L,           183L,             66L,
           12L,           355L,            185L,           175L,             86L
        )


df2 <- df %>% 
  pivot_longer(-Month, names_to = "location", values_to = "count") %>% 
  mutate(location = str_remove(location, "_Month_Freq")) %>% 
  rename(month = Month)

ggplot(df2, aes(month, count, fill = location)) + 
  geom_col(position = "dodge")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.