使用g中的ggplot在y轴上绘制具有相同x轴的多个变量 [英] Plot multiple variables on y-axis with the same x-axis using ggplot in r

查看:674
本文介绍了使用g中的ggplot在y轴上绘制具有相同x轴的多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有一个简单的问题,

我有我的数据集的以下摘录:

 head(meanchangeafterdowngrade)
`降级后的天数``CDS债券基础``基础变化
< DBL> < DBL>
1 -30 -27.41164 0.02152773
2 -29 -27.13952 0.01377565
3 -28 -27.12704 0.02781917
4 -27 -27.10034 0.02570149
5 -26 -27.10408 0.02681551
6 -25 -27.10054 0.02795637

现在我想绘制降级后的天数使用ggplot在x轴上和y轴上的其他2个变量。我实现了将它们绘制在两个单独的图表中,但是我想让它们处于相同的状态。我想我应该使用 melt()函数,但是我还没有完全理解它。

解决方案

以下是 iris 数据集的示例:

  
dat < - reshape2 :: melt(iris,id.vars =Species,
measure.vars = c(Sepal.Length ,Sepal.Width))

ggplot(dat,aes(x = Species,y = value,color = variable))+
geom_point()


Hey there I got a quick question,

I have the following extract of my dataset:

head(meanchangeafterdowngrade)
    `Days after downgrade` `CDS-bond basis` `Basis change                                                 
                    <dbl>            <dbl>         <dbl>
1                    -30        -27.41164     0.02152773
2                    -29        -27.13952     0.01377565
3                    -28        -27.12704     0.02781917
4                    -27        -27.10034     0.02570149
5                    -26        -27.10408     0.02681551
6                    -25        -27.10054     0.02795637

Now I would like to plot the "Days after downgrade" on the x-axis and the other 2 variables on the y-axis using ggplot. I achieved to plot them in two separate graphs, however I would like to have them in the same. I guess I should use the melt() function, but I did not quite get it yet.

解决方案

Here is an example with the iris dataset:

library(ggplot2)

dat <- reshape2::melt(iris, id.vars="Species", 
                      measure.vars=c("Sepal.Length", "Sepal.Width"))

ggplot(dat, aes(x=Species, y=value, color=variable)) + 
  geom_point()

这篇关于使用g中的ggplot在y轴上绘制具有相同x轴的多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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