同一张ggplot中的多条线图

3

我正在使用这些数据创建线性图。有人知道如何在同一图表中添加另一条线(具有第二组召回率和准确率点)吗?

recall11Point    = c(0.2, 0.2, 0.4, 0.4, 0.4, 0.6, 0.6, 0.6, 0.8, 1.0)
precision11Point = c(1.0, 0.5, 0.67, 0.5, 0.4, 0.5,0.43,0.38,0.44,0.5)
d = data.frame("Recall" = recall11Point, "Precision" = precision11Point);

# old fashioned plotting
#plot(y=precision11Point, x=recall11Point, type="l")

ggplot(data=d, aes(x=Recall, y=Precision)) +
  geom_line(size=2, colour="red") +
  geom_point(size=10)
2个回答

3
d = data.frame(  "Recall" = c(0.2, 0.2, 0.4, 0.4, 0.4, 0.6, 0.6, 0.6, 0.8, 1.0)
               , "Precision" = c(1.0, 0.5, 0.67, 0.5, 0.4, 0.5,0.43,0.38,0.44,0.5)
               ,  "Recall2" = seq(0,0.9, by = 0.1)
               ,  "Precision2" = seq(0,0.9, by = 0.1)
)

library(ggplot2)

ggplot(data=d) +
  geom_line(aes(x=Recall, y=Precision), size=1, colour="red") +
  geom_point(aes(x=Recall, y=Precision), size=5, color = "red") + 
  geom_line(aes(x=Recall2, y=Precision2), size=1, colour="blue") +
  geom_point(aes(x=Recall2, y=Precision2), size=5, colour="blue") +
  theme_bw() + 
  theme( panel.grid.minor = element_blank(), panel.grid.major = element_blank() ) 

enter image description here


1
你可以像这样创建第二个数据框dt2
dt2 <- data.frame(Recall=recall11Point+runif(10), Precision=precision11Point+runif(10))
ggplot(data=d, aes(x=Recall, y=Precision)) +
  geom_line(size=2, colour="red") +
      geom_point(size=10)+
          geom_line(data=dt2, size=2, aes(x=Recall, y=Precision), colour="red") +
              geom_point(data=dt2, aes(x=Recall, y=Precision), size=10)

或者您可以创建一个单一的数据框,并像这样添加group变量(在使用ggplot时更好)。
df <- rbind(d, dt2)
df$group <- gl(2, 10)

ggplot(data=df, aes(x=Recall, y=Precision, group=group))+
    geom_point(size=2)+
        geom_line(sise=10)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接