label_with_unit: Make a vector with the labels for plots with unit measure

View source: R/label_with_unit.R

label_with_unitR Documentation

Make a vector with the labels for plots with unit measure

Description

This function take the colnames (e.g. from the Olv_select_col()), add an unit measure and substitute "_" with space. It's useful to make a better label for a plot.

Usage

label_with_unit(data, colname, unit, optional = "")

Arguments

data

An "opposite dataframe". For example if I want to change the column name of "Polifenoli_totali" (from the datapoltot dataframe), I will put in data parameter the opposite dataframe called datapolind containing the default columns (i.e. "Codice_azienda, "N_campionamento", ...), but also the individual polyphenols. Doing this if "colname" has no corresponding column (for example "Polifenoli_totali" is not present in datapolind, but "Codice_azienda" does), it will be modified. If not, the column names doesn't change.

colname

The column (e.g. from the Olv_select_col()) that has to be changed.

unit

The unit measure that has to be printed next to the column names (e.g. "(mg/g drupe)").

optional

This parameter is empty by default. If you need to specify another column that has not to be modified put here the column name. For example in datapoltot there are two column that has no corresponding column in the opposite dataframe (datapolind) i.e. "Polifenoli_totali" and "Presenza_larve". If you add "Presenza_larve in this parameter, it won't be changed and won't be added the unit measure.

Examples

## Not run: 

 #create a dataframe
 polifind = data.frame(
 Codice_azienda = c("SA_1", "SA_2", "AV_3", "NA_1", "BN_6"), 
 Acido_Gallico = c(600,550,172,37,109))
 
 #create the selected column 
 selectedcol = data.frame(Polifenoli_totali = c(100,400,321,67,99))
 
 #The following function will give the colname with the unit measure and 
 #with spaces instead of underscore.
 label_with_unit(data = polifind, colname = selectedcol, unit = "(mg)")
 
 Polifenoli totali (mg)
 
 #but for example if I use:
 
 selectedcol2 = data.frame(Presenza_larve = c(1,0,0,2,0))
 
 label_with_unit(data = polifind, colname = selectedcol2, unit = "(mg)")
 #it will print:
 
 Presenza larve (mg)
 
 #because "Presenza_larve" is still not present in "polifind" but it's a 
 #categorical value and so doesn't need a unit measure. To solve this problem
 #I can specificy the \code{optional} parameter:
 
 label_with_unit(data = polifind, colname = selectedcol2, unit = "(mg)", 
 optional = "Presenza_larve")
 
 Presenza larve

## End(Not run)


ShinyFabio/OliveHealthR documentation built on Oct. 17, 2024, 2:24 a.m.