ggplot2-如何在离散比例轴值中绘制连续值?

时间:2015-04-02 17:23:12

标签: r plot ggplot2

我有连续值(请参阅下面数据中的cols log),我想在ggplot2的行字符中绘图。我的代码实际上做了工作但我的问题是y轴的值。事实上,这些值并没有很好地分布,因此绘图会产生错误的比例视觉感知(例如,参见下图中出动图中绿线和蓝线之间的空间)

enter image description here

如何解决此问题?有没有办法强制y轴从离散值(如(0,1,2,3,4,5,6,7))周围的数据中组织出有价值的值?

以下是我的数据

motor;trace;rules;time;log
monetDBIE;1m;R2;1556;3,1
monetDBIE;1m;R1;1590;3,2
monetDBIE;1m;RDFS;15999;4,2
monetDBIE;2m;R2;3319;3,5
monetDBIE;2m;R1;3645;3,5
monetDBIE;2m;RDFS;31239;4,4
monetDBIE;5m;R2;9283;3,9
monetDBIE;5m;R1;10398;4
monetDBIE;5m;RDFS;85806;4,9
Jena;1m;R1;227;2,3
Jena;1m;R2;1203228;6
Jena;1m;RDFS;21;1,3
Jena;2m;R1;321;2,5
Jena;2m;R2;5099199;6,7
Jena;2m;RDFS;21;1,3
Jena;5m;R1;629;2,7
PGSQLIE;1m;R1;8149;3,9
PGSQLIE;1m;R2;6548;3,8
PGSQLIE;1m;RDFS;66116;4,8
PGSQLIE;2m;R1;17029;4,2
PGSQLIE;2m;R2;22523;4,3
PGSQLIE;2m;RDFS;81155;4,9
PGSQLIE;5m;R1;33483;4,5
PGSQLIE;5m;R2;1504944;6,1
PGSQLIE;5m;RDFS;891532;5,9

这是我的ggplot2代码

require("ggplot2")

w <- read.csv(file="saturation/saturation.csv", head=TRUE, sep=";")

p <- ggplot(data=w, aes(x=trace, y=log, colour=motor, shape=motor))

p <- p + geom_point(size=4)

p <- p + geom_line(size=1,aes(group=motor))

p <- p + geom_text(aes(label=time), hjust=-0.2, vjust=1)

p <- p + ggtitle("Insertion des triplets (R1, R2) et saturation RDFS dans triplestores")

p <- p + scale_fill_continuous(guide = guide_legend(title = NULL))

p <- p + facet_grid(rules~.)

p <- p + theme_bw()

postscript(file = 'saturation.vs.eps')

print (p)

1 个答案:

答案 0 :(得分:1)

在这种情况下,你真的想要一个离散的y尺度吗?如果您将w$log更改为数字,则图表将“正确”格式化:

# Convert log variable to numeric and substitute commas with decimals
w$log <- as.numeric(sub(",", ".", w$log, fixed=TRUE))

ggplot(data=w, aes(x=trace, y=log, colour=motor, shape=motor)) +
  geom_point(size=4) + 
  geom_line(size=1,aes(group=motor)) + 
  geom_text(aes(label=time), hjust=-0.2, vjust=1) + 
  ggtitle("Insertion des triplets (R1, R2) et saturation RDFS dans triplestores") + 
  scale_fill_continuous(guide = guide_legend(title = NULL)) + 
  facet_grid(rules~.) + 
  theme_bw()

如果你坚持使用scale_y_discrete(breaks=0:7, labels=0:7)

,那么你可以使用show_guide = FALSE

仅供参考 - 您可能希望在geom_text中添加a以从图例中删除geom_text(aes(label = time), hjust=-0.2, vjust=1, show_guide=FALSE) ...即{{1}}