将图例添加到两个geom_point()

时间:2018-05-29 17:35:42

标签: r ggplot2

我只是想知道是否有可能添加传奇,当我们得到两组不同的点时。

require 'flickraw'

class QuotesController < ApplicationController
  before_action :set_quote, only: [:show, :edit, :update, :destroy, :like]
  before_action :authenticate_user!, except: [:index, :show]


  # GET /quotes
  # GET /quotes.json
  def index
    @quotes = Quote.all.order('created_at DESC').page params[:page]
    if @quote && @quote.image_url.blank?      
      @quote.image_url = get_flickr_image_url(@quote)
      @quote.save
    end

    #puts "using #{sort_column}"
    #puts "#{params[:key]}"


  end

  # GET /quotes/1
  # GET /quotes/1.json
  def show
  end

  # GET /quotes/new
  def new
    @quote = current_user.quotes.build
  end

  # GET /quotes/1/edit
  def edit
  end

  # POST /quotes
  # POST /quotes.json
  def create
    @quote = current_user.quotes.build(quote_params)

    respond_to do |format|
      if @quote.save
        format.html { redirect_to @quote, notice: 'Quote was successfully created.' }
        format.json { render action: 'show', status: :created, location: @quote }
      else
        format.html { render action: 'new' }
        format.json { render json: @quote.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /quotes/1
  # PATCH/PUT /quotes/1.json
  def update
    respond_to do |format|
      if @quote.update(quote_params)
        format.html { redirect_to @quote, notice: 'Quote was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @quote.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /quotes/1
  # DELETE /quotes/1.json
  def destroy
    @quote.destroy
    respond_to do |format|
      format.html { redirect_to quotes_url }
      format.json { head :no_content }
    end
  end

  def like
    if @quote.liked_by current_user
      respond_to do |format|
        format.html { redirect_to :back }
        format.js
      end
    end
  end

  #Gives a random quote


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_quote
      @quote = Quote.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def quote_params
      params.require(:quote).permit(:author, :quote, :image_url)
    end

    def get_flickr_image_url(quote)
      FlickRaw.api_key="f6825c919ed112e04e918353e711b02a"
      FlickRaw.shared_secret="1989755619d0054f"
      result = flickr.photos.search(:text => quote.most_significant_word, :per_page => 1, :safe_search => 1, :is_trending => true)

      #Image used in case no result is found
      biggest_url = "http://farm3.staticflickr.com/2862/10835118755_3757dab0a4_h.jpg"

      result.each do |p|
        info = flickr.photos.getInfo(:photo_id => p.id)
        sizes = flickr.photos.getSizes(:photo_id => p.id)
        photo_area = 0

        sizes.each do |size|
          calculate_area = size.width.to_f * size.height.to_f
          puts "size of #{size.label} is #{100} x #{100} : #{size.source}"
          if calculate_area > photo_area
            photo_area = calculate_area
            biggest_url = size.source
          end
        end


      end

      return biggest_url

    end  

  end

我工作的数据:

samochodziki %>% 
  mutate(dopasowane = model_classic$fitted.values) %>% 
  arrange(lp100k) %>%
  mutate(index = 1:nrow(samochodziki)) %>%
  ggplot(aes(x = index)) +
  geom_point(aes(y = lp100k), color = "red") +
  geom_point(aes(y = dopasowane), color = "blue")

Model_classic shape

structure(list(lp100k = c(9.8006076285327, 11.2006944326088, 
7.4671296217392, 12.1244630456075, 13.0674768380436, 8.11084769257879
), cylinders = c(4L, 6L, 4L, 6L, 6L, 4L), displacement = c(1474.8354, 
3277.412, 1458.44834, 3801.79792, 3687.0885, 1114.32008), horsepower = c(75L, 
85L, 71L, 90L, 105L, 49L), weight = c(956.17271596, 1173.44346119, 
902.6488163, 1456.0315077, 1415.66178677, 846.85695479), acceleration = c(15.5, 
16, 14.9, 17.2, 16.5, 19.5), year = c(74L, 70L, 78L, 78L, 73L, 
73L), origin = c(2L, 1L, 2L, 1L, 1L, 2L), name = c("fiat 128", 
"ford maverick", "volkswagen scirocco", "amc concord", "plymouth valiant", 
"fiat 128")), .Names = c("lp100k", "cylinders", "displacement", 
"horsepower", "weight", "acceleration", "year", "origin", "name"
), row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"
))

所以我想添加传说,红点得到“真实”,蓝点“适合”。

1 个答案:

答案 0 :(得分:3)

我简化了一些名称,但我从你的数据中重新创建了模型。我为数据框提供了一列拟合值,重新命名了测量值,只是为了让它在gather之后更整齐,然后gather编辑了两个lp列。

library(tidyverse)

model <- lm(lp100k ~ horsepower + weight + year, df)

df_long <- df %>% 
  mutate(lp_fitted = model$fitted.values) %>% 
  arrange(lp100k) %>%
  rename(lp_measured = lp100k) %>%
  mutate(index = 1:nrow(df)) %>%
  gather(key = type, value = lp100k, lp_measured, lp_fitted)

df_long
#> # A tibble: 12 x 11
#>    cylinders displacement horsepower weight acceleration  year origin
#>        <int>        <dbl>      <int>  <dbl>        <dbl> <int>  <int>
#>  1         4        1458.         71   903.         14.9    78      2
#>  2         4        1114.         49   847.         19.5    73      2
#>  3         4        1475.         75   956.         15.5    74      2
#>  4         6        3277.         85  1173.         16      70      1
#>  5         6        3802.         90  1456.         17.2    78      1
#>  6         6        3687.        105  1416.         16.5    73      1
#>  7         4        1458.         71   903.         14.9    78      2
#>  8         4        1114.         49   847.         19.5    73      2
#>  9         4        1475.         75   956.         15.5    74      2
#> 10         6        3277.         85  1173.         16      70      1
#> 11         6        3802.         90  1456.         17.2    78      1
#> 12         6        3687.        105  1416.         16.5    73      1
#> # ... with 4 more variables: name <chr>, index <int>, type <chr>,
#> #   lp100k <dbl>

现在数据采用这种格式,绘图很简单 - 你只需将type分配给颜色,这样lp_measured值就会得到一种颜色而lp_fitted值会得到另一种颜色

ggplot(df_long, aes(x = index, y = lp100k, color = type)) +
  geom_point() +
  scale_color_manual(values = c(lp_measured = "red", lp_fitted = "blue"))