-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26 Put_labels_over_geom_bar_examples.R
More file actions
89 lines (64 loc) · 3.48 KB
/
26 Put_labels_over_geom_bar_examples.R
File metadata and controls
89 lines (64 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 26 Put_labels_over_geom_bar_examples.R
# Include labels over geom bar for each bar in a ggplot2 chart
# Reference
# https://stackoverflow.com/questions/12018499/how-to-put-labels-over-geom-bar-for-each-bar-in-r-with-ggplot2
# Avoid scientific notation in the charts created.
options(scipen=999)
library(tidyverse)
# 1. Standard geom bar with labels over each bar in ggplot2.
df <- cbind.data.frame(drug=c("D1","D2", "D3","D4","D5"),
dose=c(4.2,10,29.5,24,23),
subject = c("Sample1","Sample2","Sample1",
"Sample2","Sample1"))
head(df)
str(df)
# geom_text(stat='count',aes(label = ..count..),vjust=-0.2)
# stats @count' will provide a value on top of each bar
DISP <- ggplot(df,aes(x=drug, y = dose, fill = subject)) +
geom_bar(position = 'dodge', stat = 'identity') +
geom_text(aes(label = dose), position = position_dodge(width = 0.9),
vjust = -0.30) + # Set vjust to -0.30 to display just a small gap between chart and figure
ggtitle("Adding mark label to ggplot")
DISP
ggsave("plots/39 Geom_bar_charts_labels_example.png", width = 10, height = 6)
# This is an example with real data from 2019 population countries from WDI website
# Script to get repeated values: Year <-rep("2019", each=3)
# Getting population figures
## if (!require("WDI")) install.packages("WDI")
# library(WDI)
# WDI_population <- WDI(indicator = c("SP.POP.TOTL"), extra = TRUE)
# WDI_population
# 2. Bar chart displaying countries population figures
Countries <-c("Armenia","Aruba","Australia","Austria","Azerbaijan","Bahrain","Bangladesh",
"Barbados","Belarus","Belgium","Belize","Benin","Bermuda")
length(Countries)
Year <-rep("2019", each=13)
Population <-c(2820602,106442,25334826,8879920,10024283,1494188,165516222,280180,
9419758,11488980,389095,12290444,63911
)
length(Population)
popcountries <- cbind.data.frame(Countries,Year,Population)
popc_sorted <- popcountries %>%
arrange(desc(Population))
# Create plot
COUNTRIES_chart <- ggplot(popc_sorted,aes(x=Countries, y = Population, fill = Year)) +
geom_bar(position = 'dodge', stat = 'identity') +
geom_text(aes(label = Population), position = position_dodge(width = 0.9),
vjust = -0.30) + # Set vjust to -0.30 to display just a small gap between chart and figure
ggtitle("Adding mark label to ggplot")
COUNTRIES_chart
ggsave("plots/40 Countries_pop_labels_geom_bar.png", width = 10, height = 6)
# 3. Bar chart displaying countries population figures in descending order
# https://stackoverflow.com/questions/16961921/plot-data-in-descending-order-as-appears-in-data-frame
# using the reorder() function
# We use the reorder function to
# This is how you use this reorder() function:
# aes(x = reorder(Countries, -Population), y = Population)
COUNTRIES_sorted <- ggplot(popc_sorted,
aes(x = reorder(Countries, -Population), y = Population), fill = Year) +
geom_bar(position = 'dodge', stat = 'identity') +
geom_text(aes(label = Population), position = position_dodge(width = 0.9),
vjust = -0.30) + # Set vjust to -0.30 to display just a small gap between chart and figure
ggtitle("Adding mark label to geom_bar() and display results sort descending order")
COUNTRIES_sorted
ggsave("plots/41 Countries_pop_labels_geom_bar_sorted.png", width = 10, height = 6)