绘制geom_point和geom_line

时间:2014-07-01 18:49:53

标签: r ggplot2

我正在尝试使用ggplot制作多线图。我还想在线上绘制一些点(这意味着重要性)。问题是当我使用geom_point时,这些点与我想要的线不一致。这些将是数据:

dat1: 
                1-4      2-5     3-6      4-7     5-8     6-9    7-10        id
 mod1      -0.035930 0.121970 0.34689 0.034345 0.35312 0.52048 0.58536     mod1
 mod2      -0.094121 0.297150 0.37262 0.512140 0.63918 0.42127 0.73890     mod2
 mod3       0.810550 0.876070 0.57120 0.472640 0.67341 0.79332 0.80882     mod3
 mod4      -0.121970 0.010009 0.49783 0.920100 0.76192 0.45662 0.45526     mod4

dat2:
             1-4     2-5 3-6    4-7     5-8     6-9    7-10        id
 mod1         NaN     NaN NaN    NaN     NaN     NaN     NaN     mod1
 mod2         NaN     NaN NaN    NaN 0.63918     NaN     NaN     mod2
 mod3      0.81055 0.87607 NaN    NaN 0.67341 0.79332 0.80882    mod3
 mod5          NaN     NaN NaN 0.9201 0.76192     NaN     NaN    mod4

该图将包含4行,并且在模型中包含一些值,我想用点绘制它们。

这是我的尝试:

#Start Plotting
library(ggplot2)
library(reshape2)
dat_r$id <- nam_model  #names of models
dat_r1$id <- nam_model
df <- melt(dat_r,id='id')
df2 <-melt(dat_r1,id='id')

p <-ggplot(df, aes(x=variable,y=value, group=id)) +
     geom_line(aes(color=id), lwd=1) + geom_point(aes(x=df2$variable,y=df2$value,      group=df$id),size = 4)

有什么建议吗?我很感激任何想法!

提前致谢

2 个答案:

答案 0 :(得分:2)

首先阅读数据:

dat1 <- read.table(header=TRUE, check.names=FALSE, text="1-4      2-5     3-6      4-7     5-8     6-9    7-10        id
-0.035930 0.121970 0.34689 0.034345 0.35312 0.52048 0.58536     mod1
-0.094121 0.297150 0.37262 0.512140 0.63918 0.42127 0.73890     mod2
 0.810550 0.876070 0.57120 0.472640 0.67341 0.79332 0.80882     mod3
-0.121970 0.010009 0.49783 0.920100 0.76192 0.45662 0.45526     mod4")

dat2 <- read.table(header=TRUE, check.names=FALSE, text="1-4     2-5 3-6    4-7     5-8     6-9    7-10        id
 NaN     NaN NaN    NaN     NaN     NaN     NaN     mod1
 NaN     NaN NaN    NaN 0.63918     NaN     NaN     mod2
 0.81055 0.87607 NaN    NaN 0.67341 0.79332 0.80882    mod3
 NaN     NaN NaN 0.9201 0.76192     NaN     NaN    mod4")

使用reshape2包将数据转换为长格式:

library(reshape2)
df1 <- melt(dat1, id="id")
df2 <- melt(dat2, id="id")

您还可以使用dplyrtidyr套餐的组合:

library(dplyr)
library(tidyr)

df1 <- dat1 %>% gather(var, value, 1:7)
df2 <- dat2 %>% gather(var, value, 1:7)

将数据绑定在一个数据帧中(不一定):

dat <- cbind(df1,df2[,3])
names(dat) <- c("id","var","value1","value2")

最后创建情节:

ggplot(data=dat, aes(x=var, y=value1, color=id, group=id)) +
  geom_line(lwd=1) + 
  geom_point(aes(y=value2), size=4) +
  scale_x_discrete("\nModels") +
  scale_y_continuous("Value", breaks=c(0,0.2,0.4,0.6,0.8)) +
  theme_bw()

给出: enter image description here

如果您不想将数据绑定在一个数据框中,可以使用:

ggplot(data=df1, aes(x=var, y=value, color=id, group=id)) +
  geom_line(lwd=1) + 
  geom_point(data=df2, size=4) +
  scale_x_discrete("\nModels") +
  scale_y_continuous("Value", breaks=c(0,0.2,0.4,0.6,0.8)) +
  theme_bw()

答案 1 :(得分:1)

这是一种可能性:

使用check.names = FALSE读取数据,因为您的变量名称在语法上无效。

dat1 <- read.table(text = "                1-4      2-5     3-6      4-7     5-8     6-9    7-10        id
 mod1      -0.035930 0.121970 0.34689 0.034345 0.35312 0.52048 0.58536     mod1
 mod2      -0.094121 0.297150 0.37262 0.512140 0.63918 0.42127 0.73890     mod2
 mod3       0.810550 0.876070 0.57120 0.472640 0.67341 0.79332 0.80882     mod3
 mod4      -0.121970 0.010009 0.49783 0.920100 0.76192 0.45662 0.45526     mod4",
                   header = TRUE, check.names = FALSE)

dat2 <- read.table(text = "             1-4     2-5 3-6    4-7     5-8     6-9    7-10        id
 mod1         NaN     NaN NaN    NaN     NaN     NaN     NaN     mod1
 mod2         NaN     NaN NaN    NaN 0.63918     NaN     NaN     mod2
 mod3      0.81055 0.87607 NaN    NaN 0.67341 0.79332 0.80882    mod3
 mod5          NaN     NaN NaN 0.9201 0.76192     NaN     NaN    mod4",
                   header = TRUE, check.names = FALSE)

melt数据格式为长格式:

library(reshape2)
dat1m <- melt(dat1, id.var = "id")
dat2m <- melt(dat2, id.var = "id")

使用数据集绘制线条,另一个绘制点:

library(ggplot2)
ggplot(data = df1m, aes(x = variable, y = value, colour = id, group = id)) +
  geom_line() +
  geom_point(data = df2m, size = 4)

enter image description here

小注意:在aes来电中,请避免使用dataset$variable之类的代码(例如df2$value)。它可能会导致不必要的行为