Home > Blockchain >  Two line label slopegraph with newggslopegraph, GGPfunctions
Two line label slopegraph with newggslopegraph, GGPfunctions

Time:01-30

I am looking to make a nice slopegraph with CGPfunctions package, exactly newggslopegraph. I am making a graph with not only one but two labels. Below I report the code I wrote with two columns concerning the two labels.

library(ggplot2)
library(tidyverse)
library(CGPfunctions)




###### dataframe definition ####
group = c("lt:23719", "lt:23719", "lt:23719", "lt:23719", "lt:23719", "lt:23719", "lt:23719", "lt:23719")
year = c(1:8)
value = c(0.076, 0.052, 0.069, 0.088, 0.09, 0.107, 0.107, 0.117)
etichette = c("A", "B", "C", "D", "E", "F", "G", "H")
label1 = c("0,076%", "0,052%", "0,069%", "0,088%", "0,090%", "0,107%", "0,107%", "0,117%")
label2 = c("15000 cP", "16000 cP", "17000 cP", "18000 cP", "19000 cP", "20000 cP", "21000 cP", "22000 cP")
source_df <- data.frame(group, year, value, etichette, label1, label2)
source_df <- transform(source_df, year=factor(year, levels=c(1,2,3,4,5,6,7,8), labels=source_df$etichette))


#######  Graph creation  ######
g <- newggslopegraph(dataframe = source_df,
                Times = year,
                Measurement = value,
                Grouping = group,
                Data.label = label1,
                DataLabelPadding = 0.35,
                Caption = NULL
                )


g

With this output:

enter image description here

What I am willing to do is to add not a single line label but two lines one, containing above label1 defined in dataframe source_df and on the line below label2.

I tried to do the following:

g <- newggslopegraph(dataframe = source_df,
                Times = year,
                Measurement = value,
                Grouping = group,
                Data.label = paste0(label1, "\n", label2),
                DataLabelPadding = 0.35,
                Caption = NULL
                )


g

Getting this error:

Error: 'paste0(label1, "\n", label2)' is not the name of a variable in the dataframe

How can I make a two line label graph? Is it possible to make?

Thank you in advance for every support!

CodePudding user response:

It looks like Data.label is expecting a column of labels. Instead, create a new column in your data frame with the desired label, and pass that to the Data.label argument.

source_df[, 'new_label'] <- paste0(label1, '\n', label2)

g <- newggslopegraph(dataframe = source_df,
                     Times = year,
                     Measurement = value,
                     Grouping = group,
                     Data.label = new_label,
                     DataLabelPadding = 0.35,
                     Caption = NULL
)

enter image description here

  •  Tags:  
  • Related