Problem
We want to reduce the space between bars using geom_bar with ggplot2.
Vertical bar plot
Horizontal bar plot
library(ggthemes)
library(ggplot2)
df <- data.frame(x = c("firm", "vendor"), y = c(50, 20))
# Vertical
ggplot(df, aes(x = x, y = y)) +
geom_bar(stat = "identity", width = 0.4) +
theme_tufte() +
labs(x = "", y = "")
# Horizontal
ggplot(df, aes(x = x, y = y)) +
geom_bar(stat = "identity", width = 0.4) +
theme_tufte() + coord_flip() +
labs(x = "", y = "")
Solution
We just need to to introduce theme(aspect.ratio = xx), then we need to play with the ratio to find the desired width. To flip cartesian coordinates we use coord_flip()
# Vertical
ggplot(df, aes(x = x, y = y)) +
geom_bar(stat = "identity") +
theme_tufte() + theme(aspect.ratio = 2) +
labs(x = "", y = "")
# Horizontal
ggplot(df, aes(x = x, y = y)) +
geom_bar(stat = "identity") +
theme_tufte() + theme(aspect.ratio = .2) +
coord_flip() +
labs(x = "", y = "")
Results
Vertical bar plot
Horizontal bar plot
No hay comentarios:
Publicar un comentario