R:使用ggplot在单个图上的两个散点图 [英] R: two scatterplots on single graph using ggplot

查看:211
本文介绍了R:使用ggplot在单个图上的两个散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意我是初学者R.
我用merge()方法合并了两个数据框和一个公共列。我得到了如下数据框:

  x y1 y2 
1 3 5
2 2 4
1 2 2
3 5 5
...

等。
我想用ggplot绘制这样的数据框。我创建的(使用

  • / a>


  • 如何手动向ggplot对象添加图例
  • ggplot和R:随时间变化的两个变量 / b>

    (这些是搜索 [r] ggplot melt ,虽然您可能也通过 [r] ggplot legend ...)

    如果可以,请获取 ggplot book并从头开始阅读 - 不幸的是草稿的PDF不再可以在线获得,但这本书值得投资。


    1. 您实际上有一些点数 x y 值靠近你的阴谋的极端。只是很难看到它们,因为它们几乎是透明的(在白色背景下看它们会更容易一些,例如,尝试将 + theme_bw()添加到 ggplot call)。如果要限制图的范围,可以使用 xlim ylim 。 (对您的数据尝试 summary 并查看最大值...)


    2. 最好的方法绘制坐标轴的方法是遵循将数据融化为长格式数据集的 ggplot 成语,该数据集包含一列( y1 vs y2 ),另一个值如下:

    3. b
      $ b


        d < -  data.frame(x = c(1,2,1,3) ,
      y1 = c(3,2,2,5),
      y2 = c(5,4,2,5))
      library(ggplot2)##也会载入重塑包,我们需要熔化()
      dm < - 熔体(d,id.var = 1)
      ggplot(data = dm,aes(x,value,color = variable))+
      geom_point(alpha = 0.2)+
      scale_colour_manual(value = c(red,blue))+
      labs(x =games,y =variance)

      (对于略微奇怪的格式,抱歉)
      我设置了 alpha 值稍高一些,否则很难看到图中的点。我认为默认的颜色(reddish和blue-ish)是可以的,但是我使用 scale_colour_manual 来按照您指定的方式获取它们。




      • 3。我不确定你的意思。



      Please note I am beginner with R. I have merged two data frames with one common column with merge() method. I have obtained data frame like:

       x   y1   y2
       1   3    5
       2   2    4
       1   2    2
       3   5    5
       ...
      

      etc. i would like to plot such data frame with ggplot. What I have created (using documention of geom_point is

      ggplot(data = dat_c, aes(games, variance.x)) + 
           geom_point(aes(x = games, y = variance.x), legend=  TRUE,  xlab="X", ylab="Y", colour=alpha('red', 0.05)) + 
           geom_point(aes(x = games, y = variance.y), legend = TRUE, colour=alpha('blue', 0.05) )
      

      It works, NaNs do not disturb me because I get warning that they are ignored, which is fine. However I have two problems and I am not sure how to fix them:

      1. my actual plot is located at the bottom-left corner, I would like to set max values for X and Y axis (in a dynamic manner, for example with highest value from data + 100 or something like this)
      2. the legend is not displayed
      3. the axis are not described

      Here is how it looks hlike:

      解决方案

      See also:

      (these are the results of searching [r] ggplot melt, although you might also have gotten there via [r] ggplot legend ...)

      If you can, get a copy of the ggplot book and read it from the beginning -- unfortunately the PDF of the draft is no longer available online, but the book is worth the investment.

      1. You actually have some points with x and y values near the extremes of your plot. It's just hard to see them because they're nearly transparent (it will be a little easier to see them on a white background, i.e. try adding +theme_bw() to your ggplot call). You can use xlim and ylim if you want to restrict the range of the plot. (Try summary on your data and check out the Max values ...)

      2. the best way to get the axes drawn is to follow the ggplot idiom of "melting" your data into a long-format data set with one column for the category (y1 vs y2) and another for the value, as follows:


        d <- data.frame(x=c(1,2,1,3),
                      y1=c(3,2,2,5),
                      y2=c(5,4,2,5))
        library(ggplot2) ## loads reshape package too, which we need for melt()
        dm  <- melt(d,id.var=1)
        ggplot(data=dm,aes(x,value,colour=variable))+
        geom_point(alpha=0.2)+
        scale_colour_manual(value=c("red","blue"))+
        labs(x="games",y="variance")
      

      (sorry for the slightly odd formatting) I set the alpha value a little higher because otherwise it would have been hard to see the points in the figure. I think the default colours (reddish and blue-ish) are OK, but I used scale_colour_manual to get them the way you specified.

      • 3. I'm not sure what you mean.

      这篇关于R:使用ggplot在单个图上的两个散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆