如何将geom_rect与离散轴值一起使用

时间:2015-09-18 02:22:39

标签: r ggplot2

我想绘制矩形,但我的x轴是离散值。如何将xmin和xmax值设置为从离散值之间的某个点开始?

public class AjaxResult : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.Result = new HttpNotFoundResult();
        }

        base.OnActionExecuting(filterContext);
    }
}

这段代码绘制了一个矩形,但我想将它向左移动一点。我有点希望能够设置xmin =“b”-0.3和xmax =“b”+0.3,如果这是有意义的。

1 个答案:

答案 0 :(得分:11)

您可以尝试:

ggplot(data = df) +
  geom_rect(data = df, aes(x = x, y=y), xmin = as.numeric(df$x[[2]]) - 0.3,
                                        xmax = as.numeric(df$x[[3]]) + 0.3,
                                        ymin = 0, ymax = 2)

这很有效,就像你在aes调用之外调用xmin和xmax等一样,你可以使用你想要的任何东西。图中使用因子水平绘制出来,因此as.numeric将确保您获得正确的因子水平。