Contact us

Send
inqury

Knowledge base

home banner

bi@unija.com

  • Knowledge

  • Categories

May 24, 2021

ggplot2 With Table of Values

Author: Branka Trifunović

When plotting charts, we often want to see the actual values listed as a table. Bellow we created ggplot2 plot with neatly listed values bellow the plot. We used AirPassengers sample dataset in R and added “K” formatting for example’s sake.

To get the plot use the following R code.

dat = AirPassengers

len = length(dat)

df = data.frame(Date=as.Date(dat), AC=as.matrix(dat), PL = as.integer(as.matrix(dat)+runif(len, -15, 15), length=0))

df = subset(df, Date < as.Date(“1950-01-01”))

df = melt(df, id.vars=”Date”)

df$variable = factor(df$variable, levels = c(“PL”, “AC”))

df = df[with(df, order(variable)),]

df$textcolor = case_when(df$variable == “AC” ~ “white”,

df$variable == “PL” ~ “black”,

TRUE ~ “white”)

df$labels = format(round(as.numeric(df$value), 1), nsmall=0, big.mark=”.”, decimal.mark = “,”)

df$labels = paste(df$labels, “K”, sep=””)

textsize = 4.3

g =

ggplot(data = df, aes(x=Date, y=value, fill=variable)) +

xlab(“”) +

ylab(“”) +

geom_bar(stat=’identity’, position = position_dodge(width = 8), width=50) +

theme(legend.position=”top”,

legend.justification=’left’,

legend.text=element_text(size=text_size*1.2),

panel.grid.major.y = element_line(colour=”azure2″, size=0.5),

axis.text.y=element_text(size = text_size),

axis.text.x=element_text(size = text_size),

panel.background = element_rect(fill = “transparent”,colour = NA)

) +

scale_x_date(date_labels=”%b \n %Y”, breaks = df$Date,expand = c(0, 0)) +

scale_y_continuous(labels = scales::number_format(accuracy = 1, decimal.mark = ‘.’, suffix = “K”), expand = c(0,0)) +

scale_fill_manual(“”, values = c(“AC” = “black”, “PL” = “grey90”)) +

geom_label(data=subset(df, variable==”AC”), aes(y=value/2, label = labels), fill = “black”, colour = “white”, label.size = 0, size = textsize, nudge_x = 2, fontface = “bold”, vjust=”inward”)

df$variable = factor(df$variable, levels = c(“PL”, “AC”))

textsize= 4

ggtable =

ggplot(df, aes(x=Date, y=variable, fill=variable)) +

xlab(“”) +

ylab(“”) +

theme(legend.position=”none”,

panel.grid.major.x = element_blank(),

panel.grid.major.y = element_blank(),

panel.grid.minor.x = element_blank(),

panel.grid.minor.y = element_blank(),

axis.text.y=element_text(size = 12),

axis.text.x=element_blank(),

panel.ontop = TRUE,

axis.ticks.x=element_blank(),

panel.background = element_rect(fill = “transparent”,colour = NA)

) +

scale_x_date(date_labels=”%b \n %Y”, breaks = df$Date) +

scale_fill_manual(“”, values = c(“AC” = “black”, “PL” = “grey90”)) +

scale_y_discrete(expand = c(0, 0)) +

geom_rect(xmin=-Inf, xmax = Inf, aes(ymin = as.numeric(variable)-0.5,

ymax = as.numeric(variable)+0.5, fill = variable), alpha = 0.1) +

geom_text(aes(y=variable, color = textcolor, label = ifelse(value == “<NA>”, “”, labels)), size = textsize, fontface = “bold”) +

scale_color_manual(“”, values = c(“black” = “black”, “white” = “white”, “grey35″= “grey50”, “grey90″=”grey90”, “green”=”palegreen3”, “red”=”indianred2”))

theme_set(theme_grey())

plot_grid(g, ggtable, ncol = 1, align = “v”, rel_heights = c(5, 1))

Tweak values of parameters to make the plot look good with dimensions you need. Happy plotting!

 

Index