我想把y和y画在同一个图上。
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
plot(x, y1, type = "l", col = "red")
plot(x, y2, type = "l", col = "green")
但当我这样画的时候,它们就不在同一个图上了。
在Matlab中是可以的,但有人知道在R中怎么做吗?
我想把y和y画在同一个图上。
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
plot(x, y1, type = "l", col = "red")
plot(x, y2, type = "l", col = "green")
但当我这样画的时候,它们就不在同一个图上了。
在Matlab中是可以的,但有人知道在R中怎么做吗?
当前回答
您可以使用plotly包中的ggplotly()函数将这里的任何gggplot2示例转换为交互式图形,但我认为这种类型的图形没有ggplot2会更好:
# call Plotly and enter username and key
library(plotly)
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
plot_ly(x = x) %>%
add_lines(y = y1, color = I("red"), name = "Red") %>%
add_lines(y = y2, color = I("green"), name = "Green")
其他回答
如果您使用的是基础图形(即不是晶格/网格图形),那么您可以通过使用点/线/多边形函数来模拟MATLAB的保持特性,从而在不开始新的图形的情况下为图形添加额外的细节。在多图布局的情况下,您可以使用par(mfg=…)来选择将内容添加到哪个图中。
使用plotly(用主要和次要y轴从plotly中添加溶液-它似乎缺失了):
library(plotly)
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
df=cbind.data.frame(x,y1,y2)
plot_ly(df) %>%
add_trace(x=~x,y=~y1,name = 'Line 1',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE) %>%
add_trace(x=~x,y=~y2,name = 'Line 2',type = 'scatter',mode = 'lines+markers',connectgaps = TRUE,yaxis = "y2") %>%
layout(title = 'Title',
xaxis = list(title = "X-axis title"),
yaxis2 = list(side = 'right', overlaying = "y", title = 'secondary y axis', showgrid = FALSE, zeroline = FALSE))
工作演示截图:
也就是说,你可以在overplot中使用点。
plot(x1, y1,col='red')
points(x2,y2,col='blue')
数学函数使用曲线。 并使用add=TRUE来使用相同的绘图和轴。
curve( log2 , to=5 , col="black", ylab="log's(.)")
curve( log , add=TRUE , col="red" )
curve( log10, add=TRUE , col="blue" )
abline( h=0 )
我们也可以使用格库
library(lattice)
x <- seq(-2,2,0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
xyplot(y1 + y2 ~ x, ylab = "y1 and y2", type = "l", auto.key = list(points = FALSE,lines = TRUE))
对于特定的颜色
xyplot(y1 + y2 ~ x,ylab = "y1 and y2", type = "l", auto.key = list(points = F,lines = T), par.settings = list(superpose.line = list(col = c("red","green"))))