ggplot2 error : Discrete value supplied to continuous scale

前端 未结 3 765
囚心锁ツ
囚心锁ツ 2020-12-14 10:37

I have a dataset called "merged", which contains 3 numeric columns "pauseMedian" and "numTotalPauses" and "diff". I also have a splin

3条回答
  •  独厮守ぢ
    2020-12-14 11:14

    Now that we know the two color vars are of different types, there's the issue. You can try using a different scale for one (e.g. fill instead of color)

    set.seed(123)
    my_df1 <- data.frame(a=rnorm(100), b=runif(100), c=rep(1:10, 10))
    my_df2 <- data.frame(a=rnorm(100), b=runif(100), c=factor(rep(LETTERS[1:5], 20)))
     
    # this won't work. can't assign discrete and continuous to same scale
    ggplot() +
      geom_point(data=my_df1, aes(x=a, y=b, color=c)) +
      geom_polygon(data=my_df2, aes(x=a, y=b, color=c), alpha=0.5)
    

    Error: Discrete value supplied to continuous scale

    # but use fill for polygons, and that works:
    ggplot() +
      geom_point(data=my_df1, aes(x=a, y=b, color=c)) +
      geom_polygon(data=my_df2, aes(x=a, y=b, fill=c), alpha=0.5)
    

    plot output

    If you have to use the same scale (color), and can't convert the variables to the same type, see this for more info: Plotting continuous and discrete series in ggplot with facet

提交回复
热议问题