ggplot子集功能内的列表 [英] ggplot subset a list within a function

查看:32
本文介绍了ggplot子集功能内的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案,可以很好地格式化嵌套的构面标签.提供解决方案

解决方案

我建议两种方法来解决您的问题.您可以使用 $ 删除元素或使用要删除的名称定义矢量.这是使用您的数据框 data 的代码:

我们有两种删除对象的方法.首先,我们可以使用 $ 并调用该元素以将其删除:

 库(ggplot2)图书馆(dplyr)图书馆(拼凑而成)#创建列表列表<-split(data,data $ Fac_Map)#去掉List $ Fac2<-空#阴谋#现在绘制函数myplotfun<-函数(x){G<-ggplot(x,aes(x = Period,y = Result))+geom_point()+facet_grid(.〜S_Residency,比例=免费",空间=免费")+ylim(范围(x $ Result))+xlab(")+ ylab(")+ ggtitle(唯一(x $ Fac_Map))回报(G)}#申请地块列表2<-lapply(列表,myplotfun)#包装最终情节wrap_plots(List2,nrow = 1) 

输出:

第二种方法,我们可以设置要删除的名称向量,并使用索引:

 列表<-split(data,data $ Fac_Map)#定义名称的向量vecrem<-c("Fac2")#去掉列表[[vecrem]]<-空#现在绘制函数myplotfun<-函数(x){G<-ggplot(x,aes(x = Period,y = Result))+geom_point()+facet_grid(.〜S_Residency,比例=免费",空间=免费")+ylim(范围(x $ Result))+xlab(")+ ylab(")+ ggtitle(唯一(x $ Fac_Map))回报(G)}#申请地块列表2<-lapply(列表,myplotfun)#包装最终情节wrap_plots(List2,nrow = 1) 

输出:

更新:

您可以通过在新列表中隔离所需的元素,然后从大列表中删除以继续进行绘图,从而获得单独的绘图.然后以常规方式应用该函数,最后可以将新的对象保存在 G1 G2 中.最后,您可以根据需要合并或安排:

 #创建列表列表<-split(data,data $ Fac_Map)#定义名称的向量vecrem<-c("Fac2")#隔离新列表List.single<-List [vecrem]#从大名单中删除列表[[vecrem]]<-空#现在绘制函数myplotfun<-函数(x){G<-ggplot(x,aes(x = Period,y = Result))+geom_point()+facet_grid(.〜S_Residency,比例=免费",空间=免费")+ylim(范围(x $ Result))+xlab(")+ ylab(")+ ggtitle(唯一(x $ Fac_Map))回报(G)}#申请地块列表2<-lapply(列表,myplotfun)#将图应用于单个元素List.single2<-lapply(List.single,myplotfun)#包装最终情节G1<-wrap_plots(List2,nrow = 1)G2<-wrap_plots(List.single2,nrow = 1)#合并地块G1 + G2 

输出 G1 :

输出 G2 :

组合输出 G1 + G2 :

I have a solution to format nested facet labels that is working really well. The solution was provided here .

I needed to raise another question as I need to be able to subset a list, but apparently I cant figure it out without help!

What I need to do: I want to be able to exclude Fac2 from the list. So the graph will only show Fac1 and Fac3. I think I need to subset this list List <- split(data,data$Fac_Map).

  • In the real data it is a long list of Facs and a wide graph so I want to exclude rather than list all of the inclusions.
  • I will be putting Fac2 at the end of the graph joined by ggarrange (there are two reasons for this but no need to go into the complexity of the real data). So I need to be able to filter the list within the first function G.

Data:

data <- structure(
  list(
    Fac_Map = structure(
      c(1L, 1L, 1L, 1L, 2L, 2L,
        2L, 2L, 3L, 3L, 3L, 3L),
      .Label = c("Fac1", "Fac2", "Fac3"),
      class = "factor"
    ),
    S_Residency = structure(
      c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
        1L, 1L, 2L, 2L),
      .Label = c("Intl", "Local"),
      class = "factor"
    ),
    Period = structure(
      c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
        2L, 1L, 2L),
      .Label = c("2019 P2", "2020 P2"),
      class = "factor"
    ),
    Result = c(92.9, 91.1, 85.8, 87.9, 94.1, 91.7, 87.5, 88.6,
               90, 90.1, 87.4, 88.9)
  ),
  class = "data.frame",
  row.names = c(NA,-12L)
)

Working solution to format facet labels provided by @Duck here

#Create list
List <- split(data,data$Fac_Map)
#Now function to plot
myplotfun <- function(x)
{
  G <- ggplot(x,aes(x=Period,y=Result))+
    geom_point() +
    facet_grid(. ~ S_Residency,
               scales = "free", space = "free") +
    ylim(range(x$Result)) +
    xlab("")+ylab("")+ggtitle(unique(x$Fac_Map))
  return(G)
}
#Apply for plots
List2 <- lapply(List,myplotfun)
#Wrap final plot
wrap_plots(List2,nrow = 1)

Is it possible to filter the list so I can just have Fac1 and Fac3 without having to subset the whole dataset which would prevent a ggarrange?

The output of the code:

解决方案

I would suggest two approaches in order to solve your issue. You could use $ to remove the element or defining a vector with names to be removed. Here the code using your dataframe data:

We have two ways to remove the object. First we can use $ and invoke the element in order to remove:

library(ggplot2)
library(dplyr)
library(patchwork)
#Create list
List <- split(data,data$Fac_Map)
#Remove
List$Fac2 <- NULL
#Plot
#Now function to plot
myplotfun <- function(x)
{
  G <- ggplot(x,aes(x=Period,y=Result))+
    geom_point() +
    facet_grid(. ~ S_Residency,
               scales = "free", space = "free") +
    ylim(range(x$Result)) +
    xlab("")+ylab("")+ggtitle(unique(x$Fac_Map))
  return(G)
}
#Apply for plots
List2 <- lapply(List,myplotfun)
#Wrap final plot
wrap_plots(List2,nrow = 1)

Output:

Second method we can set a vector of names to be removed and use indexing:

List <- split(data,data$Fac_Map)
#Define vector with names
vecrem <- c("Fac2")
#Remove
List[[vecrem]] <- NULL
#Now function to plot
myplotfun <- function(x)
{
  G <- ggplot(x,aes(x=Period,y=Result))+
    geom_point() +
    facet_grid(. ~ S_Residency,
               scales = "free", space = "free") +
    ylim(range(x$Result)) +
    xlab("")+ylab("")+ggtitle(unique(x$Fac_Map))
  return(G)
}
#Apply for plots
List2 <- lapply(List,myplotfun)
#Wrap final plot
wrap_plots(List2,nrow = 1)

Output:

Update:

You can have individual plots by isolating the desired element in a new list and removing from the big one to continue with the scheme of plotting. Then you apply the function in a normal way and in the end you can save in a new objects G1 and G2. Finally you can merge or arrange as you want:

#Create list
List <- split(data,data$Fac_Map)
#Define vector with names
vecrem <- c("Fac2")
#Isolate in a new list
List.single <- List[vecrem]
#Remove from big list
List[[vecrem]] <- NULL
#Now function to plot
myplotfun <- function(x)
{
  G <- ggplot(x,aes(x=Period,y=Result))+
    geom_point() +
    facet_grid(. ~ S_Residency,
               scales = "free", space = "free") +
    ylim(range(x$Result)) +
    xlab("")+ylab("")+ggtitle(unique(x$Fac_Map))
  return(G)
}
#Apply for plots
List2 <- lapply(List,myplotfun)
#Apply the plot for single element
List.single2 <- lapply(List.single,myplotfun)
#Wrap final plot
G1 <- wrap_plots(List2,nrow = 1)
G2 <- wrap_plots(List.single2,nrow = 1)
#Merge plots
G1+G2

Output G1:

Output G2:

Combined output G1+G2:

这篇关于ggplot子集功能内的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆