Fopa Léon Constantin
Fopa Léon Constantin

Reputation: 12363

ggplot2- How to plot continues values in a discrete scale axis values?

I have continous value (see cols log in data below) that I want to plot in a line char with ggplot2. My code actually do the job But my problem is the values of the y-axis. In fact these values are not well distributed and thus the plot creates wrong visual perception of the scale (see for example the space between the green and the blue lines in the thirth plot of the picture below)

enter image description here

How can I fix this issue ? Is there a way to force y-axis to organize the countinous values from data around discrete values like (0,1,2,3,4,5,6,7) ?

Here are my data

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

And here is my ggplot2 code

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)

Upvotes: 3

Views: 2163

Answers (1)

JasonAizkalns
JasonAizkalns

Reputation: 20463

Do you really want a discrete y scale in this case? If you change w$log to numeric, the graph will format "correctly":

# 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()

If you insist on a discrete y scale, you could use scale_y_discrete(breaks=0:7, labels=0:7).

FYI - you probably want to add show_guide = FALSE within geom_text to remove the a from the legend...that is, geom_text(aes(label = time), hjust=-0.2, vjust=1, show_guide=FALSE)

Upvotes: 1

Related Questions