0

I'm trying to create a stacked bar graph faceted into country income groups where countries are the unit for each bar. I am using the following code and it is producing an extra bar in three of the four income groups. Any ideas for how to remove the extra bars?

plot<- ggplot(data, aes(x = as.factor(country), y = rank_mean, fill = theme)) +
    geom_bar(stat = "identity") +
    geom_text(aes(label = country), y = (data$tot+20), size = 1.5, color = "black", angle = 90) +
    facet_wrap(incgrp ~ ., scales = "free_x") +
    labs(title="", x="", y="") + 
    scale_fill_manual(values = Blues) +
    theme(legend.position = 'bottom', 
      axis.text.x=element_blank(),
      axis.ticks.x=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks.y=element_blank(),
      axis.title=element_text(size=10),
      legend.text=element_text(size=10),
      legend.title = element_blank()) +
    guides(fill = guide_legend(nrow = 2)) 

Here is the resulting figure: enter image description here

  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. It's probably a problem with the data. – MrFlick Jul 05 '23 at 20:42
  • Without a reproducible example of your data data frame it is difficult to help you. But by the looks of your image, you have some observations with text with no characters or with only spaces in the variable "country". Those observations are then plotted as the last bar, whith no visible label. – Nicolás Velasquez Jul 05 '23 at 22:39

0 Answers0