【问题标题】:Multiple trendlines for factors ggplot2 and colors因素 ggplot2 和颜色的多条趋势线
【发布时间】:2020-12-18 20:14:05
【问题描述】:

我对 ggplot 不太熟悉,但它看起来比我用 plot_ly 得到的要好 我无法为该系列的每个因素获取趋势线。趋势线只是没有显示在生成的图表中

这是我一直在使用的代码

ggplot(subset(df,FACTOR %in% c("1","2")), aes(x= DUR, y= TEMP, color=FACTOR)) +
geom_point() +
geom_smooth(data=subset(df, FACTOR=="1"), method=lm , se=FALSE) +
geom_smooth(data=subset(df, FACTOR=="2"), method=lm , se=FALSE) +
xlab("Duration (min)") +
ylab('Change in Temperature (C)')

我的 df 看起来像这样

DUR    TEMP    FACTOR
#      #        1
#      #        1
#      #        2
#      #        3
#      #        4
...   ...      ...

谢谢

【问题讨论】:

    标签: r ggplot2 graph scatter-plot trendline


    【解决方案1】:

    尝试在 aes 语句中将因子作为组添加,并使用一个 geom_smooth 调用进行绘制(不确定方法是否应该是“lm”而不是只有 lm):

    library(ggplot2)
    
    ggplot(subset(df,GROUP %in% c("1","2")), aes(x= DUR, y= TEMP, color=FACTOR, group = FACTOR)) +
    geom_point() +
    geom_smooth(method=lm , se=FALSE) +
    xlab("Duration (min)") +
    ylab('Change in Temperature (C)')
    

    不知道为什么要在子集中过滤“GROUP” - 根据我对您的代码和数据的理解,应该是 FACTOR

    【讨论】:

    • 我忘了改问题,GROUP 是 FACTOR
    • @mcChris 的代码是否符合您的要求?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2021-08-19
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多