0
library(data.table)
library(ggplot2)
d <- data.table(setvalue = c("1.c , 1.d , 1.f , 2.b ", "1.b , 1.d , 1.f , 2.f ", "1.c , 1.d , 2.f , 2.h ", "1.b , 1.d , 1.f , 2.i ","1.c , 1.d , 2.f , 3.j "),
                    pct = c(0.06, 0.04, 0.028, 0.026, 0.017),
                    cumpct = c(0.06, 0.10, 0.128, 0.156, 0.173))

break_at_comma <- function(x) {gsub(",", "\n", x)}

ggplot(d, aes(x=reorder(setvalue, cumpct, sum), y=pct))+geom_bar(stat="identity")+
  theme_bw()+
  scale_y_continuous(labels=scales::percent, name="Procent of all combinations")+
  scale_x_discrete(name="chosen combinations", labels=break_at_comma)

Produces this plot: enter image description here

However - the 'combinations' listed on the x axis have meaning. So I'd like to color text that starts with "1.[a-z]{1}" green, text that start with "2.[a-z]{1}", yellow, and thext that starts with "3.[a-z]{1}" red.

I hope this makes sense. The end result should look something like this (labels are repeated, so only look at the colors):

enter image description here

Andreas
  • 6,612
  • 14
  • 59
  • 69
  • 1
    You can use `ifelse` to individually color axis labels, as seen here: https://stackoverflow.com/a/38862452/4421870, however, I don't know that the functionality exists to have multiple colors within a single label. You'd probably have to draw each row of axis labels separately, i.e draw the 1's first, 2's second – Mako212 Feb 22 '18 at 23:37
  • You could probably get there by editing at the `grid` level, but it can be tricky to find the specific elements you're looking for: https://stackoverflow.com/a/25105646/4421870 – Mako212 Feb 22 '18 at 23:42

1 Answers1

0

Instead of formatting the labels, I chose an alternative approach.

Basically I create another plot below the xaxis, and I combine the two plots with cowplot package (I also use stringr package for dataprocessing).

The end result look like this:

enter image description here

library(data.table)
library(ggplot2)
d <- data.table(setvalue = c("1.c , 1.d , 1.f , 2.b ", "1.b , 1.d , 1.f , 2.f ", "1.c , 1.d , 2.f , 2.h ", "1.b , 1.d , 1.f , 2.i ","1.c , 1.d , 2.f , 3.j "),
                pct = c(0.06, 0.04, 0.028, 0.026, 0.017),
                cumpct = c(0.06, 0.10, 0.128, 0.156, 0.173))

break_at_comma <- function(x) {gsub(",", "\n", x)}

bars <- ggplot(d, aes(x=reorder(setvalue, cumpct, sum), y=pct))+geom_bar(stat="identity")+
  theme_bw()+
  scale_y_continuous(labels=scales::percent, name="Percent of all combinations")+
  scale_x_discrete(name="chosen combinations", labels=break_at_comma)+
  theme(panel.border = element_blank())

# Solution

d[, c("one", "two", "three", "four") := list(stringr::str_count(setvalue, "1.[a-z]{1}"),
                                                         stringr::str_count(setvalue, "2.[a-z]{1}"),
                                                         stringr::str_count(setvalue, "3.[a-z]{1}"),
                                                         stringr::str_count(setvalue, "4.[a-z]{1}")), by=setvalue]

mds <- melt(d, id.vars=c("setvalue"), measure.vars = c('one', 'two', 'three', 'four'))

levs <- ggplot(mds, aes(x=setvalue, y=value, fill=variable, label=setvalue))+geom_bar(stat="identity")+
  scale_fill_manual(values = c("two"= "#308762", "one"="#30647D" , "three"="#FFA135", 'four'='#f0f0f0'))+
  theme_minimal()+labs(x="", y="Comb. Levels")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        panel.grid = element_blank(),
        legend.position = "none")


cowplot::plot_grid(bars,levs, ncol = 1, align = 'v', nrow=2, rel_heights = c(0.8, 0.2)) + 
  annotate("rect", xmin = 0.1, xmax = 0.99, ymin = 0.03, ymax = 0.99, color = "black", fill = NA)
halfer
  • 19,824
  • 17
  • 99
  • 186
Andreas
  • 6,612
  • 14
  • 59
  • 69