1

I can see that this has been asked several times before, but I can no find a method that works. Are there any clever people out there that can figure this out?

    data.frame(
    fast_car = sample(0:1, 35, replace = TRUE),
    slow_bus = sample(0:1, 35, replace = TRUE),
    fast_bus = sample(0:1, 35, replace = TRUE),
    slow_car = sample(0:1, 35, replace = TRUE)) %>% 
  upset(order.by = "freq")

This makes a very nice upset graph but I need to get rid of the the underscore in the set labels. I tried the answer suggested in this post: UpsetR change set name labels in graph

fast_car <- as.list(sample(0:1, 35, replace = TRUE))
slow_bus <- as.list(sample(0:1, 35, replace = TRUE))
fast_bus <- as.list(sample(0:1, 35, replace = TRUE))
slow_car <-  as.list(sample(0:1, 35, replace = TRUE))

listinput = list(fast_bus, fast_car, slow_bus, slow_car)
names(listinput) = c("fast bus", "fast car", "slow_bus", "slow car")
upset(fromList(listinput), order.by = "freq")

However this results in nice labels but the graph doesn't come out correctly. I have also tried usuing a tibble with labeled columns but the package doesnt seem to work with the tibble.

Is there a way of doing this? I would just use one word but it really needs 2 word labels.

Any help would be greatly appreciated, if a solution indeed exists?

Many thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    I don't have the `upsetr` package installed to test, but you could try including the spaces in your column names as "non-standard" names. If you wrap them in backticks, it will work, e.g., `data.frame(\`fast car\` = c(0, 1), check.names = FALSE)` – Gregor Thomas Feb 23 '22 at 15:37
  • Or change the column names after the data frame is created, `data.frame(<>) %>% setNames(c("fast car", "slow bus", "fast bus", "slow car") %>% upset(order.by = "freq")` – Gregor Thomas Feb 23 '22 at 15:39
  • Thank you so much @GregorThomas! This is exactly what I was trying to do and both methods worked first time. Unfortunatly I can not give you reputation as I am a new member. I will link this answer on the un-answered versions of this question previoulsy asked – William Hurt Feb 23 '22 at 16:45
  • I didn't post as an answer since I wasn't sure it would work, but I will now. – Gregor Thomas Feb 23 '22 at 17:04

1 Answers1

0

Generally "non-standard" column names should be avoided, but this might be an exception. You can use spaces and other special characters in column names if you quote them or surround them in backticks (and make sure they don't get autocorrected).

A couple options:

## creating the data frame with nonstandard names
data.frame(`fast car` = c(0, 1), check.names = FALSE)

## modifying names of an existing data frame
your_data %>% 
  setNames(c("fast car", "slow bus", "fast bus", "slow car") %>% 
  upset(order.by = "freq")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294