1 New years trends using R
1.2 Downloading trends
ob <- trendy(c("подарунки", "схуднення", "олів'є", "один вдома", "ялинка",
"кутя", "колядки", "маланка", "знижки", "конкурси"),
from = lubridate::ymd("2019-01-01"),
to = lubridate::ymd("2019-12-31")) %>%
get_interest()
ob
## # A tibble: 520 x 7
## date hits geo time keyword gprop category
## <dttm> <int> <chr> <chr> <chr> <chr> <chr>
## 1 2019-01-06 00:00:00 19 world 2019-01-01 2019-… подарун… web All categor…
## 2 2019-01-13 00:00:00 18 world 2019-01-01 2019-… подарун… web All categor…
## 3 2019-01-20 00:00:00 17 world 2019-01-01 2019-… подарун… web All categor…
## 4 2019-01-27 00:00:00 20 world 2019-01-01 2019-… подарун… web All categor…
## 5 2019-02-03 00:00:00 24 world 2019-01-01 2019-… подарун… web All categor…
## 6 2019-02-10 00:00:00 29 world 2019-01-01 2019-… подарун… web All categor…
## 7 2019-02-17 00:00:00 23 world 2019-01-01 2019-… подарун… web All categor…
## 8 2019-02-24 00:00:00 23 world 2019-01-01 2019-… подарун… web All categor…
## 9 2019-03-03 00:00:00 32 world 2019-01-01 2019-… подарун… web All categor…
## 10 2019-03-10 00:00:00 11 world 2019-01-01 2019-… подарун… web All categor…
## # … with 510 more rows
1.3 Visualization
ggplot(ob, aes(date, hits, group = keyword, color = keyword)) +
geom_path(size = 0.75) +
geom_jitter(aes(color = ifelse(hits == 100, keyword, NA)),
width = 0.7, height = 0.7) +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-02-01 00:00:00"),
y = 110, xend = lubridate::ymd_hms("2019-01-13 00:00:00"), yend = 100,
curvature = .1, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-02-05 00:00:00"),
y = 111, label = "дізнаються, хто \nтака Маланка",
hjust = "left", family = "Lato") +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-01-15 00:00:00"),
y = 106, xend = lubridate::ymd_hms("2019-01-07 00:00:00"), yend = 100,
curvature = .1, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-01-01 00:00:00"),
y = 111, label = "вчать колядки, \nварять кутю",
hjust = "left", family = "Lato") +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-04-01 00:00:00"),
y = 94, xend = lubridate::ymd_hms("2019-02-03 00:00:00"), yend = 100,
curvature = .3, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-04-01 00:00:00"),
y = 91, label = "намагаються схуднути \nпісля свят",
hjust = "left", family = "Lato") +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-10-01 00:00:00"),
y = 96, xend = lubridate::ymd_hms("2019-11-24 00:00:00"), yend = 100,
curvature = -.1, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-09-01 00:00:00"),
y = 91, label = "купують товари зі знижками в чорну \n
пятницю (або думають, що зі знижками)",
hjust = "left", family = "Lato") +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-11-05 00:00:00"),
y = 107, xend = lubridate::ymd_hms("2019-12-15 00:00:00"), yend = 100,
curvature = -.1, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-10-10 00:00:00"),
y = 108, label = "купують ялинку\nта подарунки",
hjust = "left", family = "Lato") +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-11-25 00:00:00"),
y = 107, xend = lubridate::ymd_hms("2019-12-22 00:00:00"), yend = 100,
curvature = -.1, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-11-10 00:00:00"),
y = 111, label = "шукають конкурси \nна корпоративи",
hjust = "left", family = "Lato") +
annotate(
geom = "curve", x = lubridate::ymd_hms("2019-12-15 00:00:00"),
y = 116, xend = lubridate::ymd_hms("2019-12-29 00:00:00"), yend = 100,
curvature = -.1, arrow = arrow(length = unit(2, "mm"))
) +
annotate(geom = "text", x = lubridate::ymd_hms("2019-12-01 00:00:00"),
y = 120, label = "їдять олів'є дивлячись \nОдин вдома",
hjust = "left", family = "Lato") +
scale_y_continuous(breaks = c(0, 20, 40, 60, 80, 100), expand=c(0,0,0,0)) +
scale_color_manual(values = c(
"#a6cee3",
"#1f78b4",
"#b2df8a",
"#33a02c",
"#fb9a99",
"#e31a1c",
"#fdbf6f",
"#ff7f00",
"#cab2d6",
"#6a3d9a"), guide = guide_legend(title = "пошукові запити:", ncol = 10)
) +
xlab("") +
ylab("") +
labs(title = "Різдвяна пісня у трендах",
subtitle = "Чим займаються українці до та після Нового року?\n
(якщо вірити їхнім запитам у Google)",
caption = "На онові даних trends.google.com") +
scale_x_datetime(date_breaks = "1 month", date_labels = "%b", expand=c(0,0,0,0)) +
hrbrthemes::theme_ipsum(base_family = "Lato") +
theme(panel.grid.minor = element_blank(),
axis.text = element_text(size = 14),
plot.title = element_text(size = 35, face = "bold",
colour = "black", vjust = -1),
plot.subtitle = element_text(size = 25, colour = "black", vjust = -1),
legend.text=element_text(size=14),
legend.title = element_text(size = 14, face = "bold"),
legend.position = "bottom",
panel.grid.major = element_line(linetype = "dotted"))
2 New years trends using Python
2.1 Packages
2.2 Downloading trends
kw_list = [["подарунки"], ["схуднення"], ["олів'є"], ["один вдома"], ["ялинка"],
["кутя"], ["колядки"], ["маланка"], ["знижки"], ["конкурси"]]
def get_trend(kw):
pytrends = TrendReq(hl='ua-UK', tz=360)
pytrends.build_payload(kw, cat=0, geo='UA',
gprop='', timeframe='2019-01-01 2019-12-31')
return pd.melt(pytrends.interest_over_time() \
.loc['2019-01-01':] \
.reset_index(level=0), id_vars=['date'], value_vars=kw)
2.3 Visualization
## index date variable value
## 0 0 2019-01-06 подарунки 22
## 1 1 2019-01-13 подарунки 17
## 2 2 2019-01-20 подарунки 24
## 3 3 2019-01-27 подарунки 28
## 4 4 2019-02-03 подарунки 31
## .. ... ... ... ...
## 515 47 2019-12-01 конкурси 51
## 516 48 2019-12-08 конкурси 54
## 517 49 2019-12-15 конкурси 65
## 518 50 2019-12-22 конкурси 100
## 519 51 2019-12-29 конкурси 68
##
## [520 rows x 4 columns]
(ggplot(df)
+ aes(x='date', y = 'value', group = 'variable', color = 'variable')
+ geom_path(size=0.5)
+ annotate(geom = "segment", x = "2019-02-01", y = 110,
xend = "2019-01-13", yend = 100)
+ annotate(geom = "text", x = "2019-02-05", y = 111,
label = "дізнаються, хто \nтака Маланка", hjust = "left")
+ annotate(geom = "segment", x = "2019-01-15", y = 106,
xend = "2019-01-07", yend = 100)
+ annotate(geom = "text", x = "2019-01-01", y = 111,
label = "вчать колядки, \nварять кутю", hjust = "left")
+ annotate(geom = "segment", x = "2019-04-01", y = 94,
xend = "2019-03-10", yend = 100)
+ annotate(geom = "text", x = "2019-04-01", y = 91,
label = "намагаються схуднути \nпісля свят", hjust = "left")
+ annotate(geom = "segment", x = "2019-10-01", y = 96,
xend = "2019-11-24", yend = 100)
+ annotate(geom = "text", x = "2019-09-01", y = 91,
label = """купують товари зі знижками в чорну \n
пятницю (або думають, що зі знижками)""", hjust = "left")
+ annotate(geom = "segment", x = "2019-11-05", y = 107,
xend = "2019-12-15", yend = 100)
+ annotate(geom = "text", x = "2019-10-10", y = 108,
label = "купують ялинку\nта подарунки", hjust = "left")
+ annotate(geom = "segment", x = "2019-11-25", y = 107,
xend = "2019-12-22", yend = 100)
+ annotate(geom = "text", x = "2019-11-10", y = 111,
label = "шукають конкурси \nна корпоративи", hjust = "left")
+ annotate(geom = "segment", x = "2019-12-15", y = 116,
xend = "2019-12-29", yend = 100)
+ annotate(geom = "text", x = "2019-12-01", y = 120,
label = "їдять олів'є дивлячись \nОдин вдома", hjust = "left")
+ scale_color_manual(
values = ["#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99",
"#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a"],
guide = guide_legend(
title = "пошукові запити:", ncol = 10, title_position = "left"
)
)
+ theme_minimal()
+ scale_y_continuous(
breaks = [0, 20, 40, 60, 80, 100],
expand = [0,0,0,0]
)
+ scale_x_datetime(
date_breaks = "1 month",
date_labels = "%b",
expand = [0,0,0,0]
)
+ ylab("")
+ xlab("")
+ labs(title = """Різдвяна пісня у трендах\n
Чим займаються українці до та після Нового року?\n
(якщо вірити їхнім запитам у Google)""")
+ theme(panel_grid_minor = element_blank(),
figure_size = (20, 12),
axis_text = element_text(size = 14),
plot_title = element_text(
size = 35, face = "bold",
colour = "black"
),
legend_text=element_text(size=12),
legend_title = element_text(size = 12, face = "bold"),
legend_position = (.5, .05),
panel_grid = element_line(linestyle = 'dotted'))
)
## <ggplot: (7554841737)>
Conclusion
ggplot2
is much flexible than plotnine
because it has geoms
for a curve, function to create subtitle and caption. Also, in ggplot2
we can better modify title style and choice pre-ready themes with custom fonts.