简体   繁体   中英

No wrapping wanted in a legend of a ggplot graph

If I run the following R code:

    g <- ggplot(results_table, aes(x = factor(results_table$Criteria, as.character(results_table$Criteria)), 
                               y = Pct*100)) + 
  geom_bar(aes (fill = factor(results_table$Criteria, as.character(results_table$Criteria))),
           stat = "identity", width = 1) + 
  scale_fill_manual(values=Palcolor) +
  theme_classic() + 
  theme(legend.position = "bottom", legend.title=element_blank(), axis.line.x=element_blank(), 
        axis.line.y=element_blank(), axis.ticks.x = element_blank(), axis.ticks.y = element_blank(), 
        axis.text.x = element_blank(), axis.text.y = element_blank(), 
        axis.title.x = element_text(margin = margin(t = -0.3, unit = "cm"),size = 9)) + 
  guides(fill = guide_legend(nrow = 4, byrow = TRUE)) +
  xlab("% of \nOrganizations") + 
  ylab("") + 
  geom_text(aes(label = paste0(sprintf("%0.1f", round(Pct*100, digits = 1)),"%"), vjust = -0.8, 
                hjust = 0.5), size = 3.2, color = "black") 

it produces the following graph: 在此处输入图像描述

first question: I would like to have the legend's labels in one line, then no wrapping. Second question: I would like to have smaller squares. How can I proceed?

Updated to include removal of line breaks from legend text to prevent wrapping, and functions to allow maximum space for legend presentation.

library(tibble)
library(ggplot2)
library(dplyr)
library(stringr)

data

tib <- tibble(v1 = letters[1:7],
              v2 = 7:1,
              g = c("long legend text, blah di blah blah blah, blah di blah  blah blah 1",
                    "long legend text, blah di blah blah blah, blah di blah  blah blah 2",
                    "long legend text, blah di blah blah blah\nwith a line break 3",
                    "long legend text, blah di blah blah blah, blah di blah  blah blah 4",
                    "long legend text, blah di blah blah blah, blah di blah  blah blah 5",
                    "long legend text, blah di blah blah blah\nwith a line break 6",
                    "long legend text, blah di blah blah blah, blah di blah  blah blah 7"))

data wrangle

# remove line break with base r
tib$g <- gsub("\\n", "", tib$g)

# or with dplyr and stringr
tib <- mutate(tib, g = str_remove(g, "\\n"))
  

plot

ggplot(tib, aes(v1, v2, fill = g)) +
  geom_col()+
  labs(fill = NULL)+
  theme(legend.position = "bottom",
        legend.key.size = unit(3, "mm"), #smaller squares
        legend.box.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "mm"), #maximise legend extents
        legend.text = element_text(size = 8))+ #modify text size to fit  
  guides(fill = guide_legend(nrow = 4, byrow = TRUE))

Created on 2020-05-14 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM