i have a title of dynamically outputed plots which change every time we filter the data using predefined combinations of settings .The title is supposed to show some string pasted to the contents of the columns of data coming from loading that excel file of settings.My problem is that some columns are left empty intentionnaly.The title is nice but showing also NA and i want to get rid of that .Code below.
filters<-readxl("settings1.xlsx") {settings1 is defined as follows:manufacturer=audi,model="",displ=1.8,year=1999)
data<-mpg
ggplot(filtrated data, aes(x = displ, y = cty))
geom_point(size = 7, colour = "#EF783D", shape = 17)
geom_line(color = "#EF783D")
ggtitle(paste0("Insights :"," ",paste0(filters$manufacturer,filters$model,filters$displ,filters$year,collapse = ",")))
CodePudding user response:
Your code is non-reproducible, but this should do:
filters<-readxl("settings1.xlsx") {settings1 is defined as follows:manufacturer=audi,model="",displ=1.8,year=1999)
data<-mpg
ggplot(filtrated data, aes(x = displ, y = cty))
geom_point(size = 7, colour = "#EF783D", shape = 17)
geom_line(color = "#EF783D")
ggtitle(paste("Insights : ", na.omit(filters$manufacturer), na.omit(filters$model), na.omit(filters$displ), na.omit(filters$year), sep = ", ")))
CodePudding user response:
So far from what I have understood your problem, since the filters
dataframe has a column, suppose model
has NA
value, NA
appears in the plot title, like in the 1st plot.
library(dplyr)
library(tidyr)
library(ggplot2)
data(mpg)
filters <- data.frame(manufacturer = "audi", model = NA, displ = 1.8, year = 1999)
ggplot(mpg, aes(x = displ, y = cty))
geom_point(size = 7, colour = "#EF783D", shape = 17)
geom_line(color = "#EF783D")
ggtitle(
paste("Insights :", paste(unlist((filters)), collapse = " "))
)
So I have tried to solve it by changing the filters
data frame a bit.
filters <- filters %>%
mutate(
across(everything(), as.character)
) %>%
tidyr::replace_na(list(manufacturer = "0", model = "0", displ = "0", year = "0"))
ggplot(mpg, aes(x = displ, y = cty))
geom_point(size = 7, colour = "#EF783D", shape = 17)
geom_line(color = "#EF783D")
ggtitle(
paste("Insights :", paste(unlist((filters)), collapse = " "))
)
So Does this solve your problem?