【发布时间】:2020-05-16 16:00:38
【问题描述】:
我一直在努力让这个情节矩阵恰到好处。它使用 ggplot2() 和 patchwork() 的组合将它们组合在一起。但是,现在条内的值是右对齐的,我需要它们左对齐。
这是我现在得到的:
Horizontal Bar plots with right aligned text inside
这是我想要的图的样子:
Horizontal Bar plot with left aligned text inside
以下代码的简化版本:
if (!require('tidyverse')) install.packages('tidyverse'); library('tidyr')
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')
if (!require('ggthemes')) install.packages('ggthemes'); library('ggthemes')
if (!require('patchwork')) install.packages('patchwork'); library('patchwork')
#######################################################
Generate data on a hypothetical individual with means
#######################################################
one_person <-
data.frame(Category = c("perf_driven"),
old_average = sample(800:900, 1, replace = TRUE),
self_eval = sample(700:800, 1, replace = TRUE),
mgr_eval = sample(700:800, 1, replace = TRUE),
peer_eval = sample(700:800, 1, replace = TRUE)
)
################################################
Horizontal bar plots
################################################
horizontal_bars <- function(data_object,metric) {
.e<-pivot_longer(data_object, -Category, names_to = "Survey", values_to = "Score")
.e <- .e %>%
subset(Category==metric)
ggplot(.e,aes(Category, Score, fill = Survey)) +
geom_bar(stat="identity", position = position_dodge2(reverse = TRUE, padding=0), width = .5) +
coord_flip() +
geom_text(aes(label=Score), position=position_dodge2(reverse = TRUE, width=.5), hjust=1.1, size = 2.5) +
#geom_text(aes(label = Score, hjust=0)) +
theme_minimal() +
theme(
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
legend.position = "none",
panel.background = element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA)
)
}
################################################
Theme for transparency
################################################
transparent_bg_theme =
theme(
# panel.border = element_blank(),
panel.background = element_rect(fill = "transparent"), # bg of the panel
plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
legend.background = element_rect(fill = "transparent"), # get rid of legend bg
legend.box.background = element_rect(fill = "transparent")
)
##################################################
Create plots and plot matrix (the real version has all different plots)
##################################################
a1 <- horizontal_bars(one_person,"perf_driven")
widths = c(.8,.8)
height = .5
plot_matrix = a1 + a1 +
a1 + a1 +
a1 + a1 +
a1 + a1 +
a1 + a1
plot_matrix = plot_matrix + plot_layout(ncol = 2, nrow = 5, widths = widths, heights = height)
plot_matrix <- plot_matrix & transparent_bg_theme
##################################################
Output png file
##################################################
ggsave('matrix_example.png', plot = plot_matrix, height=7, width=2.5, units="in", dpi="print", type="cairo", bg = 'transparent')
非常感谢您的帮助!我已经为此奋斗了太多小时,真的很感激。
【问题讨论】:
标签: ggplot2