我有一个数据集,每行有两个值,我想互相绘图。
例如:
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()
上面的代码将产生:
答案 0 :(得分:4)
因为在一个图中你有几条线彼此非常接近,所以最好使用facets来获得更清晰的图。因此,数据应该重新整形为长格式。
使用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()
现在你可以制作刻面图(我已经添加了填充以区分阴影区域):
linetype
这导致:
如果你真的想要一个图中的所有内容,你也可以向aes
添加一个ggplot(data=d1) +
geom_smooth(aes(x=iterations, y=error, col=algorithm, linetype=variable), size=1) +
theme_bw()
,以便更好地区分几行:
open(originalFilename, "r")
结果: