从R中的Facet_wrap ggplot2移除一个级别/组 [英] Removing one level/group from Facet_wrap ggplot2 in R

查看:41
本文介绍了从R中的Facet_wrap ggplot2移除一个级别/组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的tbl_df:

> str(p2p_dt_SKILL_A)
Classes ‘tbl_dt’, ‘tbl’, ‘data.table’ and 'data.frame': 693 obs. of  35 variables:
 $ Patch          : Factor w/ 7 levels "BVG1","BVG11",..: 1 2 3 4 5 6 7 1 2 3 ...
 $ Skill          : Factor w/ 15 levels "A","BROADBAND",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Date           : Date, format: "2015-12-04" "2015-12-04" "2015-12-04" "2015-12-04" ...
 $ SOD_FWIH_A     : num  1.09 1.14 1.09 1.1 1.09 1.07 1.09 1.07 1.12 1.07 ...
 $ Prod_MWF       : num  5.06 5.06 5.44 5.34 4.22 4.72 4.89 4.68 4.68 5.22 ...
 $ Prod_MA        : num  4.41 4.41 4.81 4.41 4.68 4.15 4.26 4.44 4.44 4.63 ...
 $ Prod_DL        : num  3.43 3.49 3.45 3.57 3.43 3.47 3.28 3.44 3.42 3.72 ...

使用ggplot2创建了以下折线图:

created the following line plot using ggplot2:

ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
        geom_line(data = p2p_dt_SKILL_A[p2p_dt_SKILL_A$Patch %in% c("BVG1"),],aes(colour="red"),lwd=1.3) +
        geom_smooth() +
        scale_x_date(labels=date_format("%b-%y"),breaks ="1 month")+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
        theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
              plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
              axis.title.x = element_blank(),
              legend.position="none")

现在,我正在尝试使用 facet_wrap ,但没有"BVG1"补丁.但是facet_wrap也可以获取BVG1补丁:

Now I am trying to use facet_wrap but without the "BVG1" Patch. But facet_wrap picks up the BVG1 patch as well:

ggplot(p2p_dt_SKILL_A,aes(x=Date,y=Prod_DL)) +
        geom_line(data = p2p_dt_SKILL_A[p2p_dt_SKILL_A$Patch != "BVG1",],aes(colour="red"),lwd=1.3) +
        scale_colour_discrete(drop = FALSE)+
        geom_smooth() +
        geom_line(stat = "hline", yintercept = "mean")+
        scale_x_date(labels=date_format("%b-%y"),breaks ="2 month")+
        geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-09-18"]))+

         geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-02"]))+
         geom_vline(xintercept = as.numeric(p2p_dt_SKILL_A$Date[p2p_dt_SKILL_A$Date=="2015-10-23"]))+
        ylab("DL Prod for All Skills")+
        ggtitle("BVG1 DL Prod for All Skills 2014-2015")+
        theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
              plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
              axis.title.x = element_blank(),
              legend.position="none")+
        facet_wrap(~Patch, ncol = 3)

如何获取 facet_wrap 仅从 BVG11 打印到 BVG16 并排除 BVG1 组?

How to get facet_wrap to just print from BVG11 to BVG16 and exclude BVG1 group?

我尝试使用不包含BVG1的新数据框,但它的打印效果不佳,并且到处都是行:

I tried using a new dataframe excluding BVG1 but it doesn't print nicely and lines are all over the place like:

som_data <- p2p_dt %>%
                        filter(Patch != "BVG1" & Skill %in% c("A"))

> str(som_data)
Classes ‘tbl_dt’, ‘tbl’, ‘data.table’ and 'data.frame': 594 obs. of  39 variables:
 $ Patch          : Factor w/ 6 levels "BVG11","BVG12",..: 1 2 3 4 5 6 1 2 3 4 ...
 $ Skill          : Factor w/ 1 level "A": 1 1 1 1 1 1 1 1 1 1 ...
 $ Date           : Date, format: "2015-12-04" "2015-11-27" "2015-11-20" "2015-11-13" ...
 $ SOD_FWIH_A     : num  1.14 1.12 1.15 1.09 1.13 1.14 1.15 1.16 1.14 1.11 ...
 $ Prod_MWF       : num  5.06 4.68 4.69 4.76 4.63 4.98 4.45 5.12 4.44 4.54 ...
 $ Prod_MA        : num  4.41 4.44 4.14 5.63 4.86 4.97 4.65 4.99 4.52 4.9 ...
 $ Prod_DL        : num  3.49 3.42 3.6 3.29 3.19 3.1 3.18 3.28 3.26 3.21 ...

使用上面新创建的数据框,它创建以下图形,并且均不正确

using newly created dataframe above, it creates the following graph and means are also not correct

ggplot(som_data,aes(x=Date,y=Prod_DL)) +
        geom_line(aes(colour="red"),lwd=1.3) +
        geom_smooth() +
        geom_line(stat = "hline", yintercept = "mean")+
        scale_x_date(labels=date_format("%b-%y"),breaks ="2 month")+
        geom_vline(xintercept = as.numeric(som_data$Date[som_data$Date=="2015-09-18"]))+
        geom_vline(xintercept = as.numeric(som_data$Date[som_data$Date=="2015-10-02"]))+
        geom_vline(xintercept = as.numeric(som_data$Date[som_data$Date=="2015-10-23"]))+
        ylab("DL Prod for All Skills")+
        ggtitle("BVG1 SOMs DL Prod for All Skills 2014-2015")+
        theme(axis.title.y = element_text(size = 15,face="bold",color="red"),
              plot.title = element_text(size = 15,lineheight = .8,face="bold",color="red"),
              axis.title.x = element_blank(),
              legend.position="none")+
        facet_wrap(~Patch)

任何想法?

推荐答案

我看不到问题出在哪里.这是您提供的数据的最小示例.

I can't see where the problem is. Here is a minimal example with the data you provided.

library(dplyr)
# I first need to get rid of the data.table
sample_data <- as.data.frame(sample_data)
# I don't want all those other variables you gave me either..
sample_data <- select(sample_data, Date, Prod_DL, Patch)

# Now filter out the Patch that we don't want
sample_data2 <- filter(sample_data, Patch != "BVG1")

# Create a minimal plot
ggplot(sample_data2, aes(x = Date, y = Prod_DL)) +
  geom_line(colour="red", lwd = 1.3) +
  geom_smooth() +
  facet_wrap(~Patch, ncol = 3)

并不是一个很好的图(数据有限),但是只有六个面板.

Not exactly a great plot (there is limited data) but only six panels.

这篇关于从R中的Facet_wrap ggplot2移除一个级别/组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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