使用ggplot绘制曲线拟合

时间:2016-11-17 02:33:36

标签: r ggplot2

如何使用ggplot绘制最佳拟合线的曲线?我最好的猜测是我需要以某种方式更改stat_smooth参数,但我不知道如何。我的目标类似于下图中的黑线。enter image description here

vv<-structure(list(X = 16:19, school = structure(c(3L, 3L, 3L, 3L), .Label = c("UCB", "UCD", "UIUC"), class = "factor"), year = 2009:2012, mean = c(15.60965, 16.785, 16.77725, 15.91729), sd = c(6.483547,6.852999, 6.327013, 6.74991)), .Names = c("X", "school", "year", "mean", "sd"), row.names = 16:19, class = "data.frame")

ggplot(vv, aes(x = year, y = mean)) + 
  ggtitle("scores")+
  geom_point() +
  stat_smooth(method = "lm", col = "red")

2 个答案:

答案 0 :(得分:0)

您可以尝试更改公式:

ggplot(vv, aes(x = year, y = mean)) + 
  ggtitle("scores")+
  geom_point() +
  stat_smooth(method = "lm", formula = y ~ splines::bs(x, 3), col = "red")

enter image description here

答案 1 :(得分:0)

或者只是这个(使用黄土)虽然你会得到一些警告,因为你的小数据的范围太小,但是它有效:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section class="content apc banner no-padding" id="pos_1197">
  <div class="slides slick-initialized slick-slider slick-dotted" role="toolbar">
    <div class="slick-prev slick-arrow" style="display: table;"><i class="ion-chevron-left"></i>
    </div>
    <div aria-live="polite" class="slick-list draggable" style="height: 550px;">
      <div class="slick-track" role="listbox" style="opacity: 1; width: 3000px;">
        <div class="item myclass medium  c5-bg odd slick-slide slick-current slick-active" style="background-image: url(&quot;/media/1006/1140x550.png?anchor=center&amp;mode=crop&amp;width=1600&amp;height=700&amp;rnd=131237861660000000&quot;); width: 1500px; position: relative; left: 0px; top: 0px; z-index: 999; opacity: 1;"
        data-slick-index="0" aria-hidden="false" tabindex="-1" role="option" aria-describedby="slick-slide00">
          <div class="image hidden-lg hidden-md">
            <img src="/media/1006/1140x550.png?anchor=center&amp;mode=crop&amp;width=1140&amp;height=600&amp;rnd=131237861660000000" alt="Banner_Home">
          </div>
          <div class="table-cell">
            <div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12 text-center " data-os-animation="fadeInUp" data-os-animation-delay="0s">
              <p class="heading main c3-text">WIn a Spectacular gift by donating through us</p>
              <p class="link"><a class="btn c1-bg c5-text" href="/" tabindex="0">Donate</a>
              </p>

              <h2 class="timer"></h2>
              <input class="campaignValidity" type="hidden" value="11/17/2016 6:03:34 PM" tabindex="0">
            </div>
          </div>
        </div>
      </div>
      <div class="item myclass medium  c5-bg even slick-slide" style="background-image: url(&quot;/media/1006/1140x550.png?anchor=center&amp;mode=crop&amp;width=1600&amp;height=700&amp;rnd=131237861660000000&quot;); width: 1500px; position: relative; left: -1500px; top: 0px; z-index: 998; opacity: 0;"
      data-slick-index="1" aria-hidden="true" tabindex="-1" role="option" aria-describedby="slick-slide01">
        <div class="image hidden-lg hidden-md">
          <img src="/media/1006/1140x550.png?anchor=center&amp;mode=crop&amp;width=1140&amp;height=600&amp;rnd=131237861660000000" alt="Banner_Home2">
        </div>
        <div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1 col-xs-12 text-center " data-os-animation="fadeInUp" data-os-animation-delay="0s">
          <p class="heading main c3-text">WIn a Spectacular gift by donating through us</p>
          <p class="link"><a class="btn c1-bg c5-text" href="/" tabindex="-1">Donate</a>
          </p>

          <h2 class="timer"></h2>
          <input class="campaignValidity" type="hidden" value="11/19/2016 6:03:34 PM" tabindex="-1">
        </div>
      </div>
    </div>
  </div>


</section>

enter image description here