一个图中的几个分布

huangapple go评论122阅读模式
英文:

several distributions in one graph

问题

假设有几个小数据集:df1、df2和df3。你想要将它们都绘制在同一张图上,以便比较它们的分布情况。

你可以使用ggplot2包来实现这个目标。首先,将这三个数据集合并成一个数据框,添加一个表示数据集来源的变量。然后,使用geom_line函数绘制线图,根据来源变量进行分组。

以下是实现这个过程的代码:

library(ggplot2)

# 合并数据集并添加来源变量
df <- rbind(data.frame(df1, source = "df1"),
            data.frame(df2, source = "df2"),
            data.frame(df3, source = "df3"))

# 绘制线图
plot <- ggplot(df, aes(x = x, y = y, color = source)) +
  geom_line() +
  xlab(expression(beta[1])) +
  ylab("Probability density")

# 显示图形
plot

这样,你就可以在同一张图上绘制出三个数据集的分布情况,并通过颜色区分它们。

英文:

Suppose there are several small datasets:

df1 &lt;- structure(c(-0.141538212318325, -0.116634451818482, -0.0879234771580484, 
        -0.0533107066643022, -0.0368054146633136, -0.0226497125575905, 
        -0.0063678612056457, 0.00459906451609109, 0.0133063655282195, 
        0.0207716060184669, 0.0274726820594204, 0.0336799878510329, 0.0366555724475935, 
        0.0395689665873547, 0.04243489055802, 0.0452659577454999, 0.0463915601927606, 
        0.0475140060188625, 0.0480744765110037, 0.0486345817926834, 0.0497538615794434, 
        0.0508721740725647, 0.0519904873781105, 0.0531097710495058, 0.0536698794089345, 
        0.054230353728014, 0.0553528085525862, 0.0564784224639779, 0.059309530735538, 
        0.0621755132337771, 0.0650889826150985, 0.0680646612262933, 0.0742722155557264, 
        0.0809736316248002, 0.088439323853557, 0.0971472210537636, 0.108114939336891, 
        0.124397829043938, 0.138553979141888, 0.155058746350865, 0.189663362295615, 
        0.218355318832042, 0.243229444420235, 0.000990367062930731, 0.00859600082404186, 
        0.073791896372616, 0.590432799361081, 1.30023297379888, 2.30162743948732, 
        3.92795335912153, 5.22667830522455, 6.28214376100352, 7.13561630354229, 
        7.81121422958062, 8.32417165491041, 8.52293194377732, 8.68444915588508, 
        8.80940455484161, 8.89828918186656, 8.92383408803673, 8.94367846649687, 
        8.95147098864248, 8.95784497923834, 8.96633776478538, 8.96916022491114, 
        8.96631670437497, 8.95780289880137, 8.95141842778464, 8.94361545280756, 
        8.92375028884104, 8.89818477962728, 8.8092497383716, 8.68424607129549, 
        8.52268357491506, 8.32388168593728, 7.81085549214868, 7.13521427519757, 
        6.28173240599271, 5.22630207998337, 3.92766975181398, 2.30150974790506, 
        1.30022339309761, 0.590480184551787, 0.0738236966137148, 0.00860441443526146, 
        0.000991525984262303), dim = c(43L, 2L), dimnames = list(NULL, 
                                                                 c(&quot;x&quot;, &quot;y&quot;)))


df2 &lt;- structure(c(-0.0413827934629693, -0.0276579999170032, -0.0117995507020511, 
        0.00739192105963645, 0.0165693398018205, 0.0244514120357447, 
        0.0335278704497392, 0.0396465367545758, 0.0445067538961215, 0.0486748895819256, 
        0.0524170661872075, 0.0558839094952304, 0.0575458943494636, 0.0591731772486053, 
        0.0607739585292183, 0.0623552691134339, 0.0629839775957659, 0.0636109183806704, 
        0.063923967935041, 0.0642368122285105, 0.0648619756351439, 0.0654865892656883, 
        0.066111200385378, 0.0667363438129962, 0.0670491716028731, 0.0673622003291286, 
        0.0679890942306494, 0.0686177432580686, 0.0701988367343767, 0.0717993065611313, 
        0.0734261885327203, 0.0750876661445659, 0.0785531357573142, 0.0822933560246597, 
        0.0864587313377159, 0.0913149464811867, 0.0974273586147934, 0.106491977047703, 
        0.114361163786173, 0.123520369948199, 0.142660802024068, 0.15846155043312, 
        0.172125897628315, 0.00179349669403844, 0.0155912772122874, 0.133362234541623, 
        1.06279223402205, 2.33662799903533, 4.13125205460057, 7.04250247788188, 
        9.36557106339002, 11.2528029021117, 12.7785874377162, 13.9863446824503, 
        14.9034770017163, 15.2589350003385, 15.5478729238094, 15.7715144175066, 
        15.9307387833, 15.9765541923651, 16.0121892005073, 16.0262034622988, 
        16.037684390603, 16.0530468546798, 16.0582825497975, 16.0533991624541, 
        16.0383886439602, 16.0270834354433, 16.013244660632, 15.9779593096853, 
        15.9324917734991, 15.7741269284714, 15.5513247801031, 15.2631967626917, 
        14.9085126983238, 13.9927850910296, 12.7861706127143, 11.2611579416905, 
        9.37416982809025, 7.05055176208662, 4.13739896889687, 2.34084167446738, 
        1.06512254864988, 0.133783705520388, 0.0156574074081598, 0.0018001543533275), dim = c(43L, 2L), dimnames = list(NULL, c(&quot;x&quot;, &quot;y&quot;)))


df3 &lt;- structure(c(-0.082233843587677, -0.0597005169090702, -0.0336543510525615, 
        -0.0021407296289717, 0.0129241680990817, 0.0258599447774349, 
        0.040752975875112, 0.050791213645155, 0.0587641290079624, 0.0656013126708051, 
        0.0717395216059292, 0.0774259454349767, 0.0801519435472434, 0.0828210043008093, 
        0.0854465855512746, 0.0880402233156813, 0.0890714186615819, 0.0900997149006481, 
        0.0906131726566816, 0.0911262937560994, 0.0921516750344531, 0.0931761559371701, 
        0.094200633022996, 0.0952259845878202, 0.0957390811763558, 0.0962525080131071, 
        0.0972807345356578, 0.098311841471253, 0.1009051565638, 0.103530275018208, 
        0.106198739920532, 0.108923984363453, 0.114608368010273, 0.120743673471371, 
        0.127576764724515, 0.135543757352368, 0.145572768134293, 0.160448449448319, 
        0.173365565126249, 0.188404548861935, 0.219849321906425, 0.245825250021071, 
        0.268296640023723, 0.00109286334225922, 0.00949382560065067, 
        0.0812018552428404, 0.647366021737669, 1.42361648649301, 2.51750038311475, 
        4.29240801962455, 5.70895277808992, 6.85984529873148, 7.79037084939203, 
        8.52696704546091, 9.08632075317194, 9.30311086502383, 9.47932790692175, 
        9.61571711280238, 9.71281416041876, 9.74075017657795, 9.76247645204087, 
        9.77101971565848, 9.77801771543369, 9.78737829672544, 9.79056182441988, 
        9.78757297897308, 9.77840687698152, 9.77150597459025, 9.76305967730968, 
        9.74152660117476, 9.71378278600689, 9.61716054596157, 9.48123484214343, 
        9.30546483604875, 9.0891016124177, 8.53052146395146, 7.79455204494685, 
        6.86444543401569, 5.71367583716456, 4.29681015602118, 2.5208271639189, 
        1.42586548859089, 0.648581249471226, 0.0814032409867103, 0.00952131477190934, 
        0.00109507452646605), dim = c(43L, 2L), dimnames = list(NULL, 
                                                                c(&quot;x&quot;, &quot;y&quot;)))

I can plot one dataset in a graph as a distribution like this:

plot &lt;- ggplot(as.data.frame(
df1)) + geom_line(aes(x = x, y = y)) + xlab(expression(beta[1])) + ylab(&quot;Probability density&quot;)

But how can I plot all three datasets on a one graph? They all use the same scale, so that sounds feasible.

The final results has to be similar to the one above, but with allthree distributions plotted in one graph

答案1

得分: 2

library(ggplot2)
ggplot() + 
      geom_line(data = as.data.frame(df1), aes(x = x, y = y), color = "红色") + 
      geom_line(data = as.data.frame(df2), aes(x = x, y = y), color = "蓝色") +
      geom_line(data = as.data.frame(df3), aes(x = x, y = y), color = "绿色")

一个图中的几个分布


<details>
<summary>英文:</summary>

library(ggplot2)
ggplot() +
geom_line(data = as.data.frame(df1), aes(x = x, y = y), color = "red") +
geom_line(data = as.data.frame(df2), aes(x = x, y = y), color = "blue") +
geom_line(data = as.data.frame(df3), aes(x = x, y = y), color = "green")

[![plot][1]][1]


  [1]: https://i.stack.imgur.com/RMOVD.png

</details>



huangapple
  • 本文由 发表于 2023年8月9日 17:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定