问题描述
我试图通过使用
在 code $:
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (aes(x = hp,y = mpg,color = factor(cyl))+
geom_smooth()
#您可以为geom平滑重复指定,但这是重复的
ggplot(mtcars)+
geom_point(aes(x = hp,y = mpg,color = factor(cyl))+
geom_smooth(aes(x = hp,y = mpg,color = factor ))
#把所有层的映射放在前面以继承它
ggplot(mtcars,aes(x = hp,y = mpg,color = factor(cyl))+
geom_point()+
geom_smooth()
I am trying to add a line through my plotted data using geom_smooth, but I am running into difficulty.
Here is my code:
plot.BG = ggplot(data) + geom_point(aes(x=Mass_LT, y=BG, colour=factor(Temp.f))) plot.BG + geom_smooth(method="lm") BG #[1] 79.56304 118.63903 84.03655 95.02984 67.90585 81.39920 74.73497 95.50199 #[9] 94.51260 88.08051 110.78937 96.89154 73.96888 74.04067 70.19670 69.80033 #[17] 64.49329 76.58780 98.73740 107.75642 71.05849 98.45971 101.67881 109.35420 #[25] 79.32484 69.71360 85.94306 101.25704 87.85497 119.07206 85.72013 98.91010 #[33] 95.27523 84.89955 93.42660 112.82913 121.77451 84.56991 67.66107 102.73335 Mass_LT #[1] 0.000 6.154 0.000 2.128 3.169 5.986 1.916 0.000 5.956 0.566 0.000 0.000 0.530 4.813 #[15] 2.943 2.645 0.605 2.747 0.726 0.000 0.479 0.000 2.195 0.000 0.000 2.636 3.057 2.777 #[29] 1.909 4.657 0.000 0.000 0.000 0.000 3.203 0.000 0.000 6.157 0.635 0.000 sort(Mass_LT) #[1] 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 #[15] 0.000 0.000 0.479 0.530 0.566 0.605 0.635 0.726 1.909 1.916 2.128 2.195 2.636 2.645 #[29] 2.747 2.777 2.943 3.057 3.169 3.203 4.657 4.813 5.956 5.986 6.154 6.157
Temp.f is a factor with 2 levels, The graph and points come out fine, but without a line and can't quite figure out why. Any help would be greatly appreciated.
Aesthetic mappings defined in ggplot() are inherited by subsequent layers. However, if mappings defined in a layer (e.g., inside geom_point()) are local to that layer only. Since you want the same mappings to be used by both the geom_point and the geom_smooth layers, put them in the initial ggplot() call and they will be inherited by both.
Reproducibly using mtcars:
# only the points are displayed ggplot(mtcars) + geom_point(aes(x = hp, y = mpg, color = factor(cyl)) + geom_smooth() # you could respecify for the geom smooth, but that's repetitive ggplot(mtcars) + geom_point(aes(x = hp, y = mpg, color = factor(cyl)) + geom_smooth(aes(x = hp, y = mpg, color = factor(cyl)) # put the mapping up front for all layers to inherit it ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl)) + geom_point() + geom_smooth()
这篇关于ggplot2 geom_smooth行没有显示在我的图表上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!