ggplot2根据所有数据的颜色geom_point,但基于geom_smooth [英] ggplot2 colour geom_point by factor but geom_smooth based on all data

查看:188
本文介绍了ggplot2根据所有数据的颜色geom_point,但基于geom_smooth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ggplot2中,以下命令从 p < - qplot(wt,mpg,data = mtcars,color = factor(cyl)) http://docs.ggplot2.org/current/geom_smooth.html\">这里绘制散点图,每个点根据因子着色。

我会喜欢使用geom_smooth来适应所有数据,而不考虑因素,但要根据因子保留各个点的颜色。 p + geom_smooth(method =lm)对每个因子都进行线性拟合。如何做到这一点?

解决方案

您可以通过从'qplot'包装函数中退出并使用'ggplot'和直接的几何函数。

$ $ p $ $ $ $ $ ggplot(mtcars,aes(x = wt,y = mpg))+
geom_point(aes(color = factor(cyl)))+
geom_smooth(method =lm)



第1步:设置您的初始'ggplot'设置。这些是您想要作为几何函数的默认设置。

  ggplot(mtcars,aes(x = wt,y = mpg))

在这种情况下,我们使用'mtcars' wt'分配给x轴,'mpg'分配给y轴。通过在开始时指定这些参数,我们可以减少在复制粘贴到几何函数时将某些东西搞乱的风险。



第2步:绘图点几何,使用'cyl'的因子着色点。这就是原来的'qplot'函数所做的,但我们更加明确地指定它。

  geom_point(aes (color = factor(cyl)))

第3步:绘制平滑的线性模型。这正是OP之前所写的内容,但现在着色美学不再是默认设计的一部分,该模型将按照预期进行绘制。

  geom_smooth(method =lm)



仅供参考:你可以通过明确的每一层,如下所示:

  ggplot()+ 
geom_point(data = mtcars,aes(x = wt, y = mpg,color = factor(cyl)))+
geom_smooth(data = mtcars,method =lm,aes(x = wt,y = mpg))


In ggplot2, the following command p <- qplot(wt, mpg, data=mtcars, colour=factor(cyl)) taken from here plots a scatter plot with each point coloured according to factor

I would like to fit all data with a geom_smooth irrespective of factor but keeping the colour of individual points according to factor. p + geom_smooth(method="lm") does a linear fit on each factor. How do I do this?

解决方案

You can do this fairly easily by stepping back from the 'qplot' wrapper function and using the 'ggplot' and geometry functions directly.

ggplot(mtcars, aes(x=wt, y=mpg)) +
    geom_point(aes(colour=factor(cyl))) +
    geom_smooth(method="lm")

Step 1: Set your initial 'ggplot' settings. These are the settings that you want to be defaults for the geometry functions.

ggplot(mtcars, aes(x=wt, y=mpg))

In this case, we are using the 'mtcars' data for all geometries with 'wt' assigned to the x-axis and 'mpg' assigned to the y-axis. By specifying these at the beginning, we lessen the risk of messing something up when copy-pasting into the geometry functions.

Step 2: Draw the point geometry, using the factors of 'cyl' to color the points. This is what the original 'qplot' function was doing, but we're specifying it a little more explicitly.

    geom_point(aes(colour=factor(cyl)))

Step 3: Draw the smoothed linear model. This is exactly what the OP wrote before, but now that the aesthetic of coloring is no longer part of the defaults, the model draws as intended.

    geom_smooth(method="lm")

Chain it all together with the + et voila!

For reference: You could just as easily do this by being explicit in each layer, like so:

ggplot() +
    geom_point(data=mtcars, aes(x=wt, y=mpg, colour=factor(cyl))) +
    geom_smooth(data=mtcars, method="lm", aes(x=wt, y=mpg))

这篇关于ggplot2根据所有数据的颜色geom_point,但基于geom_smooth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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