仅输出r中标题中某些数据列的非空内容?

发布于 2025-02-14 01:02:58 字数 566 浏览 2 评论 0原文

我有一个动态输出的图的标题,每次我们使用设置的预定义组合过滤数据时会更改。标题应该显示一些粘贴到来自加载该设置的excel文件的数据列的内容的字符串。我的问题。是有些列是空心的。

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 = ",")))

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 = ",")))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2025-02-21 01:02:58

由于过滤器 dataFrame具有缺少值或空值的列,因此距离我已经了解了您的问题,远离我已经了解了您的问题na出现在图标题中。因此,要删除它,我试图通过更改过滤器数据框架来解决它。

library(dplyr)
library(tidyr)
library(ggplot2)

data(mpg)

filters <- data.frame(manufacturer = "audi", model=NA, displ=1.8, year=1999)

filters <- filters %>%
  mutate(
    across(everything(), as.character)
  ) %>%
  tidyr::replace_na(list(manufacturer = "", model = "", displ = "", year = ""))


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 = " "))
  )

“”

reprex软件包(v2.0.1)

So far from what I have understood your problem since the filters dataframe has a column with missing value or empty value, suppose model has NA value, and NA appears in the plot title. So to remove it, I have tried to solve it by changing the filters data frame a bit.

library(dplyr)
library(tidyr)
library(ggplot2)

data(mpg)

filters <- data.frame(manufacturer = "audi", model=NA, displ=1.8, year=1999)

filters <- filters %>%
  mutate(
    across(everything(), as.character)
  ) %>%
  tidyr::replace_na(list(manufacturer = "", model = "", displ = "", year = ""))


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 = " "))
  )

Created on 2022-07-10 by the reprex package (v2.0.1)

错爱 2025-02-21 01:02:58

您的代码不可复制,但这应该做:

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 = ", ")))

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 = ", ")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文