예제 #1
0
        /// <summary>
        /// Creates a CDF of the given X value organized by factor
        /// </summary>
        /// <param name="description">Description.</param>
        public static void FactorCDFPlot(PlotDescription description)
        {
            description.Validate();

            string[] corePlot = GGPlot(description, new string[]
            {
                description.Factors.Length == 1 ?
                string.Format(
                    "    stat_ecdf(aes(x={0}, color={1}), {2}) + ",
                    description.XName,
                    description.Factors[0],
                    R.Variables.X) :
                LabelAxes(description),
            });

            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                R.FactorColumn(R.Variables.X, description.Factors[0]),
                R.JoinLines(corePlot),
            },
                                description.ScriptFile);
        }
예제 #2
0
        /// <summary>
        /// Creates a boxplot of a set of values organized by a factor
        /// </summary>
        /// <param name="description">Description.</param>
        /// <param name="violin">If set to <c>true</c> violin.</param>
        /// <param name="hideOutliers">If set to <c>true</c> hide outliers.</param>
        public static void BoxPlot(PlotDescription description, bool violin, bool hideOutliers = false)
        {
            description.Validate();

            if (description.PlotVariable == null)
            {
                description.PlotVariable = R.Variables.X;
            }

            string function = violin ? "geom_violin" : "geom_boxplot";

            var plotLines = new string[]
            {
                description.Factors.Length == 1 ?
                string.Format(
                    "    {0}(aes({1}, y={2}), width=0.8, {3}{4}) + ",
                    function,
                    description.FormatFactor(0),
                    description.YNames[0],
                    R.Variables.X,
                    hideOutliers ? ", outlier.shape=NA" : string.Empty) :
                string.Format(
                    "    {0}(aes({1}, fill={2}, y={3}), width=0.8, {4}{5}) + ",
                    function,
                    description.FormatFactor(0),
                    //description.FactorLevels != null && description.FactorLevels.Count >= 2 && description.FactorLevels[1] != null ?
                    description.FormatFactor(1),
                    description.YNames[0],
                    description.PlotVariable,
                    hideOutliers ? ", outlier.shape=NA" : string.Empty),
                YCoordinateTransform(description.YAxis),
                XCoordinateTransform(description.XAxis),
                LabelAxes(description),

                description.FlipCoordinates ? "    coord_flip() + " : string.Empty,
                ColorFill(description),
            };

            string[] corePlot    = GGPlot(description, plotLines);
            string[] legendLines = GGLegend(description, plotLines);


            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.Scales),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                R.FactorColumn(R.Variables.X, description.Factors[0]),
                description.Factors.Length > 1 ? R.FactorColumn(R.Variables.X, description.Factors[1]) : string.Empty,
                R.JoinLines(corePlot),
                description.LegendFile != null ? R.JoinLines(legendLines) : string.Empty,
            },
                                description.ScriptFile);
        }
예제 #3
0
        /// <summary>
        /// Creates a boxplot of a set of values organized by a factor
        /// </summary>
        /// <param name="description">Description.</param>
        public static void FactorBarPlot(PlotDescription description, bool whiskers)
        {
            description.Validate();

            if (description.PlotVariable == null)
            {
                description.PlotVariable = R.Variables.X;
            }

            var plotLines = new string[]
            {
                description.Factors.Length == 1 ?
                string.Format(
                    "    geom_bar(aes(x=factor({0}), y={1}), position='dodge', stat='identity', width=0.8, {2}) + ",
                    description.Factors[0],
                    description.YNames[0],
                    description.PlotVariable) :
                string.Format(
                    "    geom_bar(aes(x={0}, fill={1}, y={2}), position='dodge', stat='identity', width=0.6, {3}) + ",
                    description.FormatFactor(0),
                    description.FormatFactor(1),
                    description.YNames[0],
                    description.PlotVariable),
                whiskers ? string.Format(
                    "    geom_errorbar(aes(x={0}, ymin={1}-{2}, ymax={1}+{2}, fill={3}), position='dodge', stat='identity', width=0.6, {4}) + ",
                    description.FormatFactor(0),
                    description.YNames[0],
                    description.YNames[1],
                    description.FormatFactor(1),
                    description.PlotVariable) : string.Empty,
                LabelAxes(description),

                YCoordinateTransform(description.YAxis),

                ColorFill(description),
            };

            string[] corePlot   = GGPlot(description, plotLines);
            string[] coreLegend = GGLegend(description, plotLines);

            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.LoadLibrary(R.Libraries.Scales),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                R.FactorColumn(R.Variables.X, description.Factors[0]),
                description.Factors.Length > 1 ? R.FactorColumn(R.Variables.X, description.Factors[1]) : string.Empty,
                R.JoinLines(corePlot),
                description.LegendFile != null ? R.JoinLines(coreLegend) : string.Empty,
            },
                                description.ScriptFile);
        }
예제 #4
0
        /// <summary>
        /// Plots the distributions.
        /// </summary>
        /// <param name="description">Description.</param>
        public static void Distributions(PlotDescription description)
        {
            description.Validate();

            const string Mids      = "mids";
            const string Frequency = "frequency";

            var createHistograms = ApplyToYNames(
                description,
                (name, i) => Functions.Histogram(
                    R.Variables.M + i,
                    R.Variables.X,
                    name,
                    description.XAxis.Breaks, true));

            var createFrequencies = ApplyToYNames(
                description,
                (name, i) => R.BindColumns(
                    "data" + i,
                    new string[] { R.Variables.M + i, R.Variables.M + i },
                    new string[] { Mids, Frequency }));

            var plotCurves = ApplyToYNames(
                description, (name, i) => string.Format(
                    "    geom_line(aes(x={0}, y={1}, colour={2}), {3}) +",
                    Mids,
                    Frequency,
                    GetColor(description, i),
                    "data" + i));

            string[] plotLines = GGPlot(description, new string[]
            {
                R.JoinLines(plotCurves),
                LabelAxes(description),
            });

            string[] legendLines = GGLegend(description, plotCurves);

            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                R.JoinLines(createHistograms),
                R.JoinLines(createFrequencies),
                R.JoinLines(plotLines),
                R.JoinLines(legendLines),
            },
                                description.ScriptFile);
        }
예제 #5
0
        /// <summary>
        /// Creates a scatter plot.
        /// </summary>
        /// <param name="description">Description.</param>
        public static void Scatter(PlotDescription description)
        {
            description.Validate();

            if (description.PlotVariable == null)
            {
                description.PlotVariable = R.Variables.X;
            }

            var corePlot = ApplyToYNames(
                description, (name, i) => string.Format(
                    "    geom_point(aes(x={0}, y={1}, colour={2}{3}), {4}) +",
                    description.XName,
                    name,
                    description.Factors[0],
                    description.Size != 0 ? ", size=" + description.Size : string.Empty,
                    description.PlotVariable))
                           .Concat(new string[]
            {
                LabelAxes(description),
                YCoordinateTransform(description.YAxis),
                XCoordinateTransform(description.XAxis),
                Color(description),
            })
                           .ToArray();

            string[] plotLines = GGPlot(description, corePlot);

            string[] legendLines = GGLegend(description, corePlot);

            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.Scales),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                R.FactorColumn(R.Variables.X, description.Factors[0]),
                R.JoinLines(plotLines),
                R.JoinLines(legendLines),
            },
                                description.ScriptFile);
        }
예제 #6
0
        /// <summary>
        /// Densities the specified description.
        /// </summary>
        /// <param name="description">Description.</param>
        public static void Densities(PlotDescription description)
        {
            description.Validate();

            if (description.PlotVariable == null)
            {
                description.PlotVariable = R.Variables.X;
            }

            string densities = string.Format(
                "    geom_density(aes(x={0}, fill=factor({1}), colour=factor({1})), alpha={2}, {3}) +",
                description.XName,
                description.Factors[0],
                description.Alpha,
                description.PlotVariable);

            var corePlot = new string[]
            {
                densities,
                LabelAxes(description),
                XCoordinateTransform(description.XAxis),
                Color(description),
                ColorFill(description),
            };

            string[] plotLines = GGPlot(description, corePlot);

            string[] legendLines = GGLegend(description, corePlot);

            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.LoadLibrary(R.Libraries.Scales),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                R.JoinLines(plotLines),
                R.JoinLines(legendLines),
            },
                                description.ScriptFile);
        }
예제 #7
0
        public static void LinePointPlot(PlotDescription description)
        {
            description.Validate();

            int pointSize = description.Size != 0 ? description.Size : 2;
            int lineSize  = description.Size != 0 ? description.Size / 2 : 1;
            Func <string, string> plot = (function) => description.Factors.Length == 0 ?
                                         string.Format(
                "    {0}(aes(x={1}, y={2}), size={3}, {4}) + ",
                function,
                description.XName,
                description.YNames[0],
                function == "geom_point" ? pointSize : lineSize,
                R.Variables.X) :
                                         string.Format(
                "    {0}(aes(x={1}, y={2}, color={3}), size={4}, {5}) + ",
                function,
                description.XName,
                description.YNames[0],
                description.FormatFactor(0),
                function == "geom_point" ? pointSize : lineSize,
                R.Variables.X);

            var plotLines = new string[]
            {
                plot("geom_line"),
                plot("geom_point"),
                description.YNames.Length == 3 && description.Factors.Length > 0 ?
                string.Format(
                    "    {0}(aes(x={1}, ymin={2}, ymax={3}, fill={4}), alpha=0.5, {5}) + ",
                    "geom_ribbon",
                    description.XName,
                    description.YNames[1],
                    description.YNames[2],
                    description.FormatFactor(0),
                    R.Variables.X) :
                string.Empty,

                //description.YAxis.Min != 0 && description.YAxis.Max != 0 ?
                //string.Format("coord_cartesian(ylim = c({0}, {1})) + ", description.YAxis.Min, description.YAxis.Max) :
                //string.Empty,
                YCoordinateTransform(description.YAxis),
                XCoordinateTransform(description.XAxis),
                LabelAxes(description),
                ColorFill(description),

                description.FlipCoordinates ? "    coord_flip() + " : string.Empty,
            };

            string[] corePlot    = GGPlot(description, plotLines);
            string[] legendLines = GGLegend(description, plotLines);


            R.WriteAndRunScript(new string[]
            {
                R.LoadLibrary(R.Libraries.GGPlot),
                R.LoadLibrary(R.Libraries.Scales),
                R.LoadLibrary(R.Libraries.GridExtra),
                R.ReadTable(R.Variables.X, description.TableFile, true),
                description.Factors.Length > 0 ? R.FactorColumn(R.Variables.X, description.Factors[0]) : string.Empty,
                description.Factors.Length > 1 ? R.FactorColumn(R.Variables.X, description.Factors[1]) : string.Empty,
                R.JoinLines(corePlot),
                description.LegendFile != null ? R.JoinLines(legendLines) : string.Empty,
            },
                                description.ScriptFile);
        }