How do I drop the redundant area in ggplot

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

Hi, since you did not share the data here I can't fix it so just some ideas to solve your fisrt problem:
I guess the range of your y scale is too large so you can add smt like

  • scale_y_continuous(limits = c(a, b)
  • scale_y_continuous(expand = c(0, 0))
  • theme(plot.margin = margin(t = -30, b = -30)

The first should do the job but you can also play around with the expand argument or the margin of the plot if other approaches fail.

For your second problem: Yes, it relates to absolute width and height in your. A good practice is to use the output in the markdown (you can specify fig.width and fig.height in the chunk option or global knitr options) and then use the same dimensions in your ggsave call.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.