1

I am trying to get a little more distance between the legend box (indicator) and the legend text. I have a code adapted from this amazing page. Here is my MWE:

library(openxlsx)           # for reading in Excel data
library(dplyr)          # for data manipulation
library(tidyr)          # for data manipulation
library(magrittr)       # for easier syntax in one or two areas
library(gridExtra)      # for generating some comparison plots
library(ggplot2)        # for generating the visualizations

mwedata <- data.frame(Metro=c(rep("Dayton,OH",6)))
mwedata$class <- as.character(c("Lower","Middle","Upper","Lower","Middle","Upper"))
mwedata$year <- as.numeric(c(rep(2000,3),rep(2014,3)))
mwedata$value <- as.numeric(c(0.221,0.580,0.199,0.269,0.527,0.204))
mwedata <- mwedata %>%
  mutate(y_label = paste0(round(value*100, 1), "%")) 


plot <- ggplot(mwedata, aes(x = class, y = value, fill = factor(year))) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#29ABE2", "#217693")) +
  geom_text(aes(label = y_label), position = position_dodge(0.9),
            vjust = 1.5, color = "white", family = "Georgia")

plot <- plot +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = c("Lower" = "Lower Class",
                              "Middle" = "Middle Class", "Upper" = "Upper Class")) +
  labs(title = "Distribution of Adults by Income in Dayton, OH",
       subtitle = "The percentage of adults in the middle class eroded by 5.3% from 2000 to 2014. Although a small \nfraction of these individuals moved into the upper class (+0.5%), the majority of these middle class \nindividuals moved into the lower income class (+4.8%).",
       caption = "Source: Pew Research Center analysis of the \n2000 decennial census and 2014 American \nCommunity Survey (IPUMS)")

plot +
  theme_minimal() +
  theme(axis.title = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = c(1,1), legend.justification = c(1,1),
        legend.background = element_blank(),
        legend.direction="vertical",
        text = element_text(family = "Georgia"),
        plot.title = element_text(size = 18, margin = margin(b = 10)),
        plot.subtitle = element_text(size = 10, color = "darkslategrey", margin = margin(b = 25)),
        plot.caption = element_text(size = 8, margin = margin(t = 10), color = "grey70", hjust = 0),
        legend.title = element_blank(),
        legend.text.align = 2)

The last line of code legend.text.align is supposed to move the text from the legend coloured boxes, but it only seem to apply for the lower of the two. See the image below. Can anyone help me?

EDIT 1:

I totally forgot to include the defined data.frame. I have now updated the MWE so it really is an WE with this line of code

mwedata <- data.frame(Metro=c(rep("Dayton,OH",6)))

Im sorry for the confusion..

Final chart with adjusted legend text

LHM
  • 31
  • 5
  • Where is `mwedata` defined? – Z.Lin Feb 23 '19 at 03:03
  • 2
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Feb 23 '19 at 03:29
  • Thanks for noting this @Z.Lin . I have now updated the example above. Sorry for the confusion. – LHM Feb 23 '19 at 04:36
  • And thanks for the `reprex` tips @Tung – LHM Feb 23 '19 at 04:39

1 Answers1

1

This helps resolve the issue:

  1. Remove legend.title = element_blank() and legend.text.align = 2 from theme()
  2. Add fill = "" to labs()

Curious observation while debugging: using your original code, just changing the font family, e.g. from "Georgia" to "Open Sans", removes the discrepancy in alignment between the two labels in the legend.


user2363777
  • 947
  • 8
  • 18
  • Thanks a lot @user2363777 - this was very helpful. I really appreciate it. – LHM Feb 26 '19 at 20:17
  • No problem! If this resolves your question, please mark my answer as accepted. (You can click on the check mark besides the answer to fill it in.) – user2363777 Feb 26 '19 at 21:38