Hi, guys
I am still trying to reproducing the barplot in Biden vs Trump: US presidential election 2020 results . Here is what I have now.
I still have two problems: a) how to remove the redundant area above and under the bar, and b) I set up all the positions based on the plot zoom. When I saved this as pdf the text layers were overlapped a lot. I tried the method in YaRrr! The Pirate’s Guide to R but I couldn't find the right width and height. I am thinking of repositioning the text layers in markdown where pdf output seems to be the same as the preview. Is there another way to address this?
Here is part of my code
attitude <- c('solid blue', 'leaning blue', 'toss up', 'leaning red', 'solid red')
n_votes <- c(190, 108, 121, 39, 80)
group <- c(' ',' ',' ',' ', ' ')
df <- data.frame(rev(attitude), rev(n_votes), group)
df$attitude <- factor(rev(attitude), levels = attitude)
plot1_0 <- ggplot(data = df) +
geom_bar(stat = 'identity', mapping = aes (x = group, y = n_votes, fill = attitude), width = 0.12) +
geom_text(aes(label = n_votes, x = group, y = n_votes, group = attitude),
size = 5, position = position_stack(vjust = .5), colour = 'white', family="Courier") +
scale_fill_manual(values=rev(c("#1055b6", "#67b5e2", "#cccccc","#ffaca3","#ed4748"))) +
guides(fill = guide_legend(reverse = TRUE)) +
geom_segment(aes(x = 0.94, xend = 1.06, y = sum(n_votes)/2, yend = sum(n_votes)/2),
linetype = 'dashed') +
coord_flip()
plot1_1 <- plot1_0 + geom_vline(xintercept = 0.912, size = 0.5, color = 'black') +
geom_text(label = "Source: Real Clear Politics", x = 0.9, y = 0,
size = 3, colour = "#434343", hjust = 0) +
theme(panel.background = element_rect(fill = "#fdf1e5"),
plot.margin = margin(50, 10, 50, 10),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "top")
Thanks,
Danny