Home Articles

geom_smooth为每行两个值着色

Asked
Viewed 1581 times
1

我有一个数据集,每行有两个值,我想互相绘制 .

例如:

RHC,1,0.370,0.287,0.003,0.063
SA,1,0.352,0.258,0.003,0.057
GA,1,0.121,0.091,0.430,0.008

我想绘制每列的单独行,按第一列分组 . 例如 . 对于RHC行,我分别绘制{1,0.370}和{1,0.287}的{x,y1}和{x,y2} .

以下ggplot / geom_smooth完成了这个:

ggplot(data=d) + 
  geom_smooth(aes(x=iterations, y=training.error, col=algorithm)) + 
  geom_smooth(aes(x=iterations, y=testing.error, col=algorithm))

但是,两行都以一个图例条目和一种颜色结束......这使得它们无法区分 .

如何为每个 geom_smooth 呼叫生成的每一行应用不同的颜色和相应的图例条目?

重现:

library(ggplot2)
d <- read.csv("https://gist.githubusercontent.com/jameskyle/8d233dcbd0ad0b66bfdd/raw/9c975ac9d9bbcb633e44cfd70b66f7ab89dc1517/results.csv")

p1 <- ggplot(data=d) +
    geom_smooth(aes(x=iterations, y=training.error, col=algorithm)) +
    geom_smooth(aes(x=iterations, y=testing.error, col=algorithm))

pdf("graph.pdf")
print(p1)
dev.off()

上面的代码将产生:

ggplot graph

1 Answer

  • 4

    因为在一个图中你有几条线彼此非常接近,所以最好使用facet来获得更清晰的图 . 因此,数据应该重新整形为长格式 .

    使用 data.table 包,您可以将多个列同时重新整形为长格式:

    library(data.table)
    
    # melting operation for the error & time columns simultaneously
    # and setting the appropriate labels for the variable column 
    d1 <- melt(setDT(d),
               measure.vars = patterns('.error','.time'),
               value.name = c('error','time'))[, variable := c('train','test')[variable]]
    

    现在你可以制作刻面图(我已添加填充以区分阴影区域):

    ggplot(data=d1) +
      geom_smooth(aes(x=iterations, y=error, col=variable, fill=variable), size=1) +
      facet_grid(. ~ algorithm) +
      theme_bw()
    

    这导致:

    enter image description here

    如果你真的想要一个图中的所有内容,你也可以将 linetype 添加到 aes 中,以便更好地区分几行:

    ggplot(data=d1) +
      geom_smooth(aes(x=iterations, y=error, col=algorithm, linetype=variable), size=1) +
      theme_bw()
    

    结果:

    enter image description here

Related