I am trying to plot some lines on R, but it exchange the lines. Pehaps it is something I am doing wrong. If someone can help me with this.
The dataframes I have are:
df_sel2 <- read.table(text="knn_sel2 tam_tab
V11 3.500000 8
V12 3.740000 64
V13 7.111667 512
V14 25.361667 4096
V15 195.018333 32768", header=TRUE)
df_sel4 <- read.table(text="knn_sel4 tam_tab
V11 3.535000 8
V12 3.811667 64
V13 7.193333 512
V14 26.151667 4096
V15 203.636667 32768", header=TRUE)
df_sel8 <- read.table(text="knn_sel8 tam_tab
V11 3.961667 8
V12 4.055000 64
V13 7.538333 512
V14 27.288333 4096
V15 209.646667 32768", header=TRUE)
df_sel32 <- read.table(text="knn_sel32 tam_tab
V11 3.750000 8
V12 5.040000 64
V13 8.581667 512
V14 30.103333 4096
V15 225.441667 32768", header=TRUE)
The graph is being plot through this code:
qplot(tam_tab,knn_sel2,data=df_sel2, linetype="2",geom="line") +
geom_line(mapping=aes(tam_tab,knn_sel4,linetype="4"),data=df_sel4) +
geom_line(mapping=aes(tam_tab,knn_sel8,linetype="8"),data=df_sel8) +
geom_line(mapping=aes(tam_tab,knn_sel32,linetype="32"),data=df_sel32) +
scale_linetype_manual("Valor da Seletividade",
values=c("solid","dotted","dashed","dotdash"),
labels=c("2","4", "8","32")) +
labs( x ="Tamanho da tabela interna", y = "tempo (s)")
The result I get is this:
As can be seen, the label "4" is in the place of the "32". Why is it happening?
Thanks in advance!
The problem is with your labels=
arguemnt. This is for renaming the levels. I fyou want to specify the order, you need to specify the breaks=
. Use
scale_linetype_manual("Valor da Seletividade",
values=c("solid","dotted","dashed","dotdash"),
breaks=c("2","4", "8","32"))
Of course, the more friendly way to do this with ggplot is to merge all your data into one data frame. Here's one way to do that
library(dplyr)
sels <- c(2,4,8,32)
mydata <- mget(paste0("df_sel", sels)) %>%
Map(data.frame, ., sel=sels) %>%
Map(function(x) setNames(x, c("knn","tam_tab", "sel")), .) %>%
bind_rows() %>%
mutate(sel=factor(sel, sels))
Then your plotting code becomes more simple
ggplot(mydata, aes(tam_tab , knn, linetype=sel)) + geom_line()