同一图表中的2个独立的独立图

时间:2017-05-04 11:51:10

标签: r ggplot2

我有这样的数据:

mydata(
var0 = 4,4,4,4.5,4.5,4.6,4.8,5.5,5.7,7,7.5,7.6,8,8.5,8.6,9,9,9.5,9.6,10
var1 = 10,9.6,9.5,9.3,8,8.5,8.3,7.5,7,7,6.8,6.7,6.3,6,5.5,5.3,4.8,4.5,4.3,4
con0 = 0,5,10,15,50,80,100,150,180,200,220,250,300,350,400,450,480,500,550,600
con1= 600,580,550,530,480,460,450,350,330,300,200,170,165,120,75,65,25,15,10,0
)

我想要y轴上的con0和con1以及x轴上的相应值var0和var1。有没有办法在同一图表中绘制2个独立的独立图。我尝试使用以下代码只使用con0来做,但问题是如果我切换到con1形状更改。有什么建议?

ggplot(mydata, aes(y = con0)) + 
geom_line(aes(x = var0, colour = "ascent")) +   
geom_line(aes(x = var1, colour = "descent"))

1 个答案:

答案 0 :(得分:3)

这是什么意思?

library(ggplot)
df <- data.frame(
  var0 = c(4,4,4,4.5,4.5,4.6,4.8,5.5,5.7,7,7.5,7.6,8,8.5,8.6,9,9,9.5,9.6, 10),
  var1 = c(10,9.6,9.5,9.3,8,8.5,8.3,7.5,7,7,6.8,6.7,6.3,6,5.5,5.3,4.8,4.5,4.3,4),
  con0 = c(0,5,10,15,50,80,100,150,180,200,220,250,300,350,400,450,480,500,550,600),
  con1= c(600,580,550,530,480,460,450,350,330,300,200,170,165,120,75,65,25,15,10,0)
)

ggplot(df) +
  geom_line(aes(x = var0, y = con0), color = 'red') + 
  geom_line(aes(x = var1, y = con1), color = 'blue')