您如何同时将“ group_by()”和“ ggplot_build()”与构面一起使用?

时间:2019-01-21 15:05:30

标签: r ggplot2

getLogs.setOnClickListener(new View.OnClickListener()
        {
            @Override

            public void onClick(View v) {
                new AsyncTask<Integer, Void, Void>(){
                    @Override
                    protected Void doInBackground(Integer... params) {
                        try {
                            executeSSHcommand(mainActivity.user, mainActivity.pass, mainActivity.host, mainActivity.port, "cat /etc/openvpn/server.ovpn");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                }.execute(1);
            }
        });

public void executeSSHcommand(String user, String password, String host, int port, String command)
    {
        JSch lJSCH;
        Session lSession;

        lJSCH = new JSch();

        String CommandOutput = null;

        try
        {
            lSession = lJSCH.getSession(user, host, port);
            lSession.setConfig("StrictHostKeyChecking", "no");
            lSession.setPassword(password);
            lSession.connect();

            ChannelExec lChannelExec = (ChannelExec)lSession.openChannel("exec");
            lChannelExec.setCommand(command);

            ((ChannelExec)lChannelExec).setErrStream(System.err);
            InputStream ins=lChannelExec.getInputStream();

            lChannelExec.connect();
            byte[] tmp=new byte[1024];
            while(true)
            {
                while(ins.available()>0) {
                    int i = ins.read(tmp, 0, 1024);
                    if (i < 0) break;
                    getOutput.setText(new String(tmp, 0,i));
                }
                if(lChannelExec.isClosed())
                {
                    if(ins.available()>0) continue;
                    getOutput.setText("exit-status: "+lChannelExec.getExitStatus());
                    break;
                }
                try{Thread.sleep(1000);}catch(Exception ee){}
            }
            lChannelExec.disconnect();
            lSession.disconnect();
        }
        catch(Exception e)
        {

        }
    }

circled outlier

上面的代码块通过# Create the Data Frame library(tidyverse) library(ggQC) set.seed(5555) Golden_Egg_df <- data.frame(month = 1:12, egg_diameter = rnorm(n=12, mean=1.5, sd=0.2)) %>% mutate(grp = c(rep("A", 3), rep("B", 9))) Golden_Egg_df$egg_diameter[3] <- 5 # Determine the control limit values (red lines) p <- ggplot(Golden_Egg_df, aes(x = month, y = egg_diameter)) + geom_point() + geom_line() + stat_QC(method = "XmR") pb <- ggplot_build(p) thres <- range(pb$data[[3]]$yintercept) # Circle anything outside the control limits (red lines) p + geom_point( data = subset(Golden_Egg_df, egg_diameter > max(thres) | egg_diameter < min(thres)), shape = 21, size = 4, col = "red" ) 函数确定控制限制(红线)的y值。然后,它在异常值周围绘制红色圆圈。直到我介绍情节之前,这都很好。这是因为ggplot_build()的逻辑不够“聪明”,无法遍历不同的方面分组。

thres <- range(pb$data[[3]]$yintercept)

如何使下面的代码块正常工作并圈出异常值?我显然需要更复杂的# ONLY ONE 'Y-INTERCEPT' RANGE HERE TO WORRY ABOUT WITHOUT FACETING #> $`data`[[3]] #> yintercept y x label #> 1 -0.2688471 -0.2688471 -Inf LCL #> 2 3.7995203 3.7995203 -Inf UCL #> 3 -0.2688471 -0.2688471 Inf -0.3 #> 4 3.7995203 3.7995203 Inf 3.8 # MULTIPLE 'Y-INTERCEPT' RANGES HERE TO WORRY ABOUT WITH FACETING #> $`data`[[3]] #> yintercept y x label #> 1 -0.8759612 -0.8759612 -Inf LCL #> 2 4.5303358 4.5303358 -Inf UCL #> 3 -0.8759612 -0.8759612 Inf -0.9 #> 4 4.5303358 4.5303358 Inf 4.5 #> 5 1.2074161 1.2074161 -Inf LCL #> 6 1.9521532 1.9521532 -Inf UCL #> 7 1.2074161 1.2074161 Inf 1.2 #> 8 1.9521532 1.9521532 Inf 2 ,它可以识别出不同方面之间的控制限制(红线)分组。

thres2

outlier faceted

1 个答案:

答案 0 :(得分:1)

我认为最好的方法是在与数据相同的data.frame中获得范围。我不确定这是否是最优雅的解决方案,但可以与您的示例一起使用:

library(tidyverse)
library(ggQC)
set.seed(5555)
Golden_Egg_df <- data.frame(month = 1:12, 
                            egg_diameter = rnorm(n=12, mean=1.5, sd=0.2)) %>% 
  mutate(grp = c(rep("A", 3), rep("B", 9)))
Golden_Egg_df$egg_diameter[3] <- 5
Golden_Egg_df$egg_diameter[11] <- 5

# create the plot
p2 <- ggplot(Golden_Egg_df, aes(x = month,
                                y = egg_diameter)) +
  geom_point() + 
  geom_line() + 
  stat_QC(method = "XmR") + 
  facet_grid(~ grp,
             scales = "free_x",
             space = "free_x") + 
  scale_x_continuous(breaks = 1:12,
                     labels = month.abb)

# get all the info about the plot
pb2 <- ggplot_build(p2)
# extract the UCL and LCL for each plot (facet)
Golden_Egg_df <- Golden_Egg_df %>% 
  mutate(min = ifelse(grp == "A", 
                      min(pb2$data[[3]]$yintercept[1:4]),    # LCL of 1st plot 
                      min(pb2$data[[3]]$yintercept[5:8])),   # LCL of 1st plot
         max = ifelse(grp == "A", 
                      max(pb2$data[[3]]$yintercept[1:4]),    # UCL 2nd plot 
                      max(pb2$data[[3]]$yintercept[5:8])))   # UCL 2nd plot

# add the circled outlier
p2 + geom_point(data = subset(Golden_Egg_df,
                              egg_diameter > max |
                                egg_diameter < min),
                shape = 21,
                size = 4,
                col = "red")

干杯,里科