【问题标题】:ggplot2: manual modify legend in scatter plot after mulitple layers using subset [duplicate]ggplot2:使用子集在多层后手动修改散点图中的图例[重复]
【发布时间】:2023-03-28 21:28:01
【问题描述】:

有一个数据集 df,行数 >10000。前 30 行是:

>df
      ms_estimate moso_estimate         sig
1554     6.518196     0.8782018          NS
825      6.170754     5.1146423 ms and moso
709      4.164373            NA        <NA>
13025    4.269822     5.7502859 ms and moso
2269     2.905754     0.7512660          NS
6714     3.401530     3.3315667          NS
14984    2.713234            NA        <NA>
7423     1.935319    -0.5283304          NS
8453     2.123371     0.1680088          NS
906            NA     0.0382903        <NA>
14196          NA     0.0382903        <NA>
10033    2.280660     3.1261748          ms
16397    2.280660     3.1261748          ms
4647     2.159354     1.5308502          NS
5121     1.847211     0.1912870          NS
4245     1.478000     0.5877055          NS
4732     1.973196     3.0805554        moso
4733     1.973196     3.0805554        moso
14411    1.776247     0.9723628          ms
9760     1.740305    -2.3284208 ms and moso
12158    1.720102     0.9989511          NS
7741     1.758581     0.2117089          ms
14883    1.788952            NA        <NA>
2315     1.832134     0.3518875          NS
4849     1.779664    -0.2311154          NS
7266     1.226592     0.5295427          NS
7189     1.716813     0.3342551          NS
253      1.667899     0.1715527          ms
13456    1.687443     0.4861952          ms
13518    1.542558     0.5361044 ms and moso

想要使用“moso_estimate”与“ms_estimate”制作散点图,并根据在 ms_estimate、moso_estimate、两者中是否显着(由“sig”变量编码)是否显着来着色点。为了避免过度绘制('sig' == "NS"),我需要根据 'sig' 变量(使用 plyr 包中的子集()和 .())在图层中添加数据,并且 alpha = 0.2。第一层是“NS”,最后一层应该是“ms and moso”。使用下面的代码可以正常工作,除了我无法控制以这种方式执行的图例。有没有办法手动设置图例颜色,最好使用 alpha = 1。代码如下:

g <- ggplot(data = df)
g +
     aes(x = ms_estimate, y = moso_estimate) +
     geom_point(color = "grey", shape = 20, alpha=1, aes(fill = "NS")) +
     geom_point(subset = .(sig == "ms"), color = "green", shape = 20, alpha = 0.2, aes(fill = "ms")) +
     geom_point(subset = .(sig == "moso"), color = "blue", shape = 20, alpha = 0.2, aes(fill = "moso")) +
     geom_point(subset = .(sig == "ms and moso"), color = "red", shape = 20, alpha = 1, aes(fill = "ms and moso")) +
     xlim(-5, 5) + ylim(-5,5)

【问题讨论】:

    标签: r ggplot2 subset layer legend


    【解决方案1】:

    你想做这样的事情:

    ggplot(data=df[complete.cases(df),]) +
        aes(x=ms_estimate, y=moso_estimate, color=sig, alpha=sig) +
        geom_point(shape=20) +
        scale_colour_manual(values=c(NS='grey', ms='green', moso='blue', `ms and moso`='red')) +
        scale_alpha_manual(values=c(NS=1, ms=.2, moso=.2, `ms and moso`=1))
    

    【讨论】:

    • 建议的代码不会处理过度绘图 - 使用建议的代码,请参见上图以及整个数据集(>10000 个点)。这就是为什么我需要将点放在图层中,请参见上图所有数据但没有图例(这是我需要工作的内容)。我看不到建议的线程如何回答我的问题,抱歉。
    • 只需按照您希望它们绘制的顺序对您的行进行排序...
    • 例如,df &lt;- df[order(match(df$sig, c('&lt;NA&gt;', 'NS', 'ms', 'moso', 'ms and moso'))), ]
    • @user3375672 如果此答案对您有用,请考虑支持并接受它。
    • 完美,效果很好!
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2013-11-23
    • 2017-11-16
    相关资源
    最近更新 更多