绘制x对y,y中有两个值

时间:2017-04-25 00:05:17

标签: r plot ggplot2

我拟合了一个模型,想要在x轴上绘制一个观测数字图,并在y轴上拟合y轴的y值,使用ggplot2,类似于this。我不能使用颜色然后我需要不同的符号用于观察和预测。

observed <- c(0.1,0.32,0.42,0.27,0.9,0.8)
fitted <- c(0.19,0.31,0.41,0.26,0.81,0.77)
x <- c(1,2,3,4,5,6)

所以基本上我想要做的是将每个x放在y轴(观察和拟合)中的两个值用不同的符号。

2 个答案:

答案 0 :(得分:2)

您可以使用简单的基本图形来获得两个不同的绘图字符

plot(x, observed, pch=15)
points(x, fitted, pch=17)

答案 1 :(得分:2)

使用ggplot,首先创建一个data.frame:

library(tidyverse)

df <- data.frame(observed = c(0.1,0.32,0.42,0.27,0.9,0.8),
                 fitted = c(0.19,0.31,0.41,0.26,0.81,0.77),
                 x = c(1,2,3,4,5,6))

然后,您可以单独绘制每组值,并使用一些额外的规范来获取形状:

ggplot(df) + 
    geom_point(aes(x, observed, shape = 'observed')) + 
    geom_point(aes(x, fitted, shape = 'fitted'))

...或者采用可以更好地扩展的方法,首先使用tidyr::gather重塑为长形式:

df %>% gather(value, val, -x) %>% 
    ggplot(aes(x, val, shape = value)) + 
    geom_point()

相关问题