예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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);
        }