ggplot labeller from column names

I'm importing blocks of data from Excel spreadsheets, processing then plotting with ggplot.

I've discovered how to use a 'labeller' but so far have to hand create them. I'd like to have the top row in my spreadsheet have the desired titles and import that, then the data, then use both to create a labeller.
I've got several spreadsheets to process, some which combine data from other sources so I can't just name the columns with what I want in the first place.

Sample code

# Reads in the top row then the data
sc_titles <- read_excel("data/S_Testing_data.xlsx",  sheet = "dhb", range = "A1:D1")
sc_data<- read_excel("data/S_Testing_data.xlsx",  sheet = "dhb", range = "A2:D100")

# Extracts the titles and the column names from the data
col_names <- names(sc_titles)
col_names_data <- names(sc_data)

# This is a hand written labeller which does the job but I need to write and maintain one for each spreadsheet
# The desired code would generate this from 'col_names' and 'col_names_data' above.
sc_labels <- as_labeller(c(
       `data_1` = "Data Field 1",
       `data_2` = "Data Field 2",
       `data_3` = "Data Field 3",
       `data_4` = "Data Field 4" ))

Many thanks

Can you post a small data set and the ggplot code that you would use? That will make it much easier to make a well targeted answer.

Alternatively, I found it's usually best to pivot_longer the relevant columns in your data before ggplotting. Then you will have a column for the variable names and another column for the values. You could then use dplyr::mutate to create a column of labels, and use this in ggplot rather than using the labeller function. Would that work? It's hard to know without you providing an example.

1 Like

Thanks for replying folks.
It's hard to make a runable example. I'm new to R and have hacked together some code (600 lines) to process this data, learning as I go. I'm now trying to fill gaps in my knowledge before rewriting the code from scratch in a more structured and efficient way.

What led me to this question was scale_color_manualwhich can be used as part of a ggplot structure to define labels and colours for the data series in a chart.

# This can be done by declaring the values in place
scale_color_manual(labels =c("England",	"Northern Ireland", "Scotland",	"Wales"), values = c( "blue3", "green3", "purple3","grey50")) + 

# or by creating named lists once and reusing them as needed
lset_4Nations = c("England",	"Northern Ireland", "Scotland",	"Wales")
cset_x4n = c( "blue3", "green3", "purple3","grey50")

# Then this is much tidier
scale_color_manual(labels = lset_4Nations, values = cset_x4n ) +

I expected there would be similar ability for labeller blocks but can't find the information on line.
The issue isn't where the data comes from for the labeller. It is the ability to combine the two lists to avoid having to generate them explicitly as in the example above, copied here.

sc_labels <- as_labeller(c(
       `data_1` = "Data Field 1",
       `data_2` = "Data Field 2",
       `data_3` = "Data Field 3",
       `data_4` = "Data Field 4" ))

This might need to wait until I've done the rewrite and I can try to pull out an example as I go.

All the best


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.