3

I have some data, for the variable names are too long. When I don't have them in an angle, they overlap. When I have them in an angle they look like the example below.

What I would like to do is simply have the possibility to write the problematic variable as:

This is a very long

name specifically

for the example

But I cannot figure out how to do this in ggplot2.

library(ggplot2)
counts <- structure(list(ECOST = c("0.52", "0.52", "0.39", "0.39", "0.26", 
"0.26", "0.13", "0.13", "0.00", "This is a very long name specifically for the example"), group = c("control", 
"treatment", "control", "treatment", "control", "treatment", 
"control", "treatment", "control", "treatment"), count = c(18, 
31, 30, 35, 47, 46, 66, 68, 86, 86), percentage = c(16.3636363636364, 
31.9587628865979, 27.2727272727273, 36.0824742268041, 42.7272727272727, 
47.4226804123711, 60, 70.1030927835051, 78.1818181818182, 88.659793814433
), total = c(110, 97, 110, 97, 110, 97, 110, 97, 110, 97), negative_count = c(92, 
66, 80, 62, 63, 51, 44, 29, 24, 11), p_value = c(0.00843644912924255, 
0.00843644912924255, 0.172947686684261, 0.172947686684261, 0.497952719783453, 
0.497952719783453, 0.128982570547408, 0.128982570547408, 0.0447500820026408, 
0.0447500820026408)), row.names = c(NA, -10L), class = c("data.table", 
"data.frame"))

                                                    ECOST     group count percentage total negative_count p_value
 1:                                                  0.52   control    18         16   110             92  0.0084
 2:                                                  0.52 treatment    31         32    97             66  0.0084
 3:                                                  0.39   control    30         27   110             80  0.1729
 4:                                                  0.39 treatment    35         36    97             62  0.1729
 5:                                                  0.26   control    47         43   110             63  0.4980
 6:                                                  0.26 treatment    46         47    97             51  0.4980
 7:                                                  0.13   control    66         60   110             44  0.1290
 8:                                                  0.13 treatment    68         70    97             29  0.1290
 9:                                                  0.00   control    86         78   110             24  0.0448
10: This is a very long name specifically for the example treatment    86         89    97             11  0.0448

counts %>% 
    ggplot(aes(x = ECOST, y = percentage, fill = group, label=sprintf("%.02f %%", round(percentage, digits = 1)))) + 
    geom_col(position = 'dodge') + 
    geom_text(position = position_dodge(width = .9),    # move to center of bars
              vjust = -0.5,    # nudge above top of bar
              size = 4) +           
    scale_fill_grey(start = 0.8, end = 0.5) +
    theme_bw(base_size = 15) +
    theme(axis.text.x=element_text(angle=45,hjust=1))
    
Tom
  • 2,173
  • 1
  • 17
  • 44

2 Answers2

3

The simplest solution is to use str_wrap from stringr package to set the new lines automatically and make your plot code reproducible in other scenarios. The scales package also provides label_wrap and wrap_format which can be convenient in some cases (for example here you can also use scale_x_discrete(labels = scales::wrap_format(20))).

library(tidyverse)
library(ggplot2)
counts <- structure(list(ECOST = c("0.52", "0.52", "0.39", "0.39", "0.26", 
                                   "0.26", "0.13", "0.13", "0.00", "This is a very long name specifically for the example"), group = c("control", 
                                   "treatment", "control", "treatment", "control", "treatment", 
                                   "control", "treatment", "control", "treatment"), count = c(18, 
                                    31, 30, 35, 47, 46, 66, 68, 86, 86), percentage = c(16.3636363636364,                                                                                                            31.9587628865979, 27.2727272727273, 36.0824742268041, 42.7272727272727, 
                                    47.4226804123711, 60, 70.1030927835051, 78.1818181818182, 88.659793814433
                                    ), total = c(110, 97, 110, 97, 110, 97, 110, 97, 110, 97), negative_count = c(92, 
                                    66, 80, 62, 63, 51, 44, 29, 24, 11), p_value = c(0.00843644912924255, 
                                    0.00843644912924255, 0.172947686684261, 0.172947686684261, 0.497952719783453, 
                                    0.497952719783453, 0.128982570547408, 0.128982570547408, 0.0447500820026408, 
                                    0.0447500820026408)), row.names = c(NA, -10L), class = c("data.table", 
                                   "data.frame"))


counts %>% 
  ggplot(aes(x = ECOST, y = percentage, fill = group, label=sprintf("%.02f %%", round(percentage, digits = 1)))) + 
  geom_col(position = 'dodge') + 
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            vjust = -0.5,    # nudge above top of bar
            size = 4) +           
  scale_fill_grey(start = 0.8, end = 0.5) +
  theme_bw(base_size = 15) +
  theme(axis.text.x=element_text(angle=45,hjust=1)) +
  scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 20))

Created on 2021-02-22 by the reprex package (v0.3.0)

Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
2

You can break lines using \n.

Code:

library(ggplot2)

counts <- structure(list(ECOST = c("0.52", "0.52", "0.39", "0.39", "0.26", 
"0.26", "0.13", "0.13", "0.00", "This is a \nvery long name \nspecifically for the \nexample"), group = c("control", 
"treatment", "control", "treatment", "control", "treatment", 
"control", "treatment", "control", "treatment"), count = c(18, 
31, 30, 35, 47, 46, 66, 68, 86, 86), percentage = c(16.3636363636364, 
31.9587628865979, 27.2727272727273, 36.0824742268041, 42.7272727272727, 
47.4226804123711, 60, 70.1030927835051, 78.1818181818182, 88.659793814433
), total = c(110, 97, 110, 97, 110, 97, 110, 97, 110, 97), negative_count = c(92, 
66, 80, 62, 63, 51, 44, 29, 24, 11), p_value = c(0.00843644912924255, 
0.00843644912924255, 0.172947686684261, 0.172947686684261, 0.497952719783453, 
0.497952719783453, 0.128982570547408, 0.128982570547408, 0.0447500820026408, 
0.0447500820026408)), row.names = c(NA, -10L), class = c("data.table", 
"data.frame"))

library(dplyr)
counts %>% 
  ggplot(aes(x = ECOST, y = percentage, fill = group, label=sprintf("%.02f %%", round(percentage, digits = 1)))) + 
  geom_col(position = 'dodge') + 
  geom_text(position = position_dodge(width = .9),    # move to center of bars
            vjust = -0.5,    # nudge above top of bar
            size = 4) +           
  scale_fill_grey(start = 0.8, end = 0.5) +
  theme_bw(base_size = 15) +
  theme(axis.text.x=element_text(angle=45,hjust=1))

-output

enter image description here

bttomio
  • 2,206
  • 1
  • 6
  • 17