0

I have the following code. How can I make the grid much finer scale? Also not sure why the legend is not showing? Like I want to have 4 o 8 squares between 75 and 100.

#Select the colors from http://colorbrewer2.org/#type=sequential&scheme=RdPu&n=9

if( !is.element("ggplot2", installed.packages()[,1]) )
  install.packages("ggplot2")

if( !is.element("grid", installed.packages()[,1]) )
  install.packages("grid")

if( !is.element("plyr", installed.packages()[,1]) )
  install.packages("plyr")

if( !is.element("data.table", installed.packages()[,1]) )
  install.packages("data.table")

if( !is.element("igraph", installed.packages()[,1]) )
  install.packages("igraph")

if( !is.element("ggthemes", installed.packages()[,1]) )
  install.packages("ggthemes")


library(ggplot2)
library(grid)
library(plyr)
library(data.table)
library(igraph)
library(ggthemes)

#setwd(dir = "/home/sathya/Documents/coreset/rplots/fig/svm")
svmdata <- fread("ionosphere-smooth-ls-optim.dat")
#svmdata <- read.table("ionosphere-smooth-ls-optim.dat", header=TRUE)
svmdata <- rename(svmdata, c("# k"="k"))
svmdata <- head(svmdata, 100)

svmdata_ls <- fread("ionosphere-ls-optim.dat")
svmdata_ls <- head(svmdata_ls,100)
svmdata_ls <- rename(svmdata_ls, c("# k"="k"))

y_max <- max(max(svmdata$gap), max(svmdata_ls$gap))
base <- ggplot(data=svmdata_ls,aes(x=k, y=gap, group=2)) + 
       geom_line(colour="#dd3497", size=1.5) + #geom_point(size=4, shape=21) +
       geom_line(data=svmdata,aes(x=k, y=gap, group=1 ), colour="#54278f", size=1.5) +
       #ggtitle("svmdata_ls gap and svmdata gap vs k") +
       geom_smooth(alpha=.2, size=1) +
       geom_abline(colour = "grey50", size = 2) +
       xlim(0, max(svmdata$k)) +
       ylim(0, y_max) +
       scale_colour_manual("", 
                      breaks = c("svmdata_ls", "svmdata"),
                      values = c("#dd3497", "#54278f")) 


labelled <- base +
  labs(
    x = "K",
    y = "Gap",
    colour = "Cylinders",
    title = "svmdata_ls gap and svmdata gap vs k"
  ) 
labelled


styled <- labelled +
  theme_bw() + 
  theme(
    plot.title = element_text(face = "bold", size = 25, colour = "purple"),
    axis.title=element_text(size= 20, face= "bold", colour="blue"),
    legend.background = element_rect(fill = "white", size = 4, colour = "white"),
    legend.justification = c(0, 1),
    legend.key = element_rect(fill = "yellow"),
    #legend.position = "bottom",
    legend.position = c(0, 1),
    axis.ticks = element_line(colour = "grey70", size = 0.25),
    panel.grid.major = element_line(colour = "grey70", size = 0.5),
    panel.grid.minor = element_line(colour= "grey70", size=0.5)
  )
styled

enter image description here

Data: http://pastebin.com/0wb6S4m8 and http://pastebin.com/L4sdEyYZ

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • Can you please include data that will provide us with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? – Ben Bolker Sep 07 '16 at 21:38
  • should I upload the data to pastebin? – Mona Jalal Sep 07 '16 at 21:39
  • that would be OK. Anywhere it's easily accessible. – Ben Bolker Sep 07 '16 at 21:40
  • @BenBolker please see the update – Mona Jalal Sep 07 '16 at 21:42
  • 2
    There's no legend because you didn't map any variables to color (you would do this inside `aes`, not outside). You can change the number of gridlines by setting the `breaks` in `scale_x_continuous` and `scale_y_continuous` to whatever you'd like. – aosmith Sep 07 '16 at 21:52

2 Answers2

2

These are just the basics; I didn't bother with all the special colours etc..

  • to get a legend, combine your data into one long data frame and use aes(colour=grp), where grp is a variable that identifies the group.
  • to get finer-spaced tick marks, use scale_x_continuous() with breaks or minor_breaks set.

Get data:

rr <- function(x) plyr::rename(as.data.frame(data.table::fread(x)),
                               c("# k"="k"))
svmdata <- rr("ionosphere-smooth-ls-optim.dat")
svmdata_ls <- rr("ionosphere-ls-optim.dat")

Combine data into a single labeled data frame:

get_vars <- function(d) d[c("k","gap")]
svmcomb <- plyr::ldply(list(svmdata=svmdata,svmdata_ls=svmdata_ls),
        get_vars)

Plot:

library(ggplot2)
ggplot(svmcomb,aes(k,gap,colour=.id))+
       geom_line()+
       scale_x_continuous(minor_breaks=seq(0,100,by=2.5))

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

Typically with ggplot you would approach this type of thing more like this:

svmdata$grp <- "svmdata"
svmdata_ls$grp <- "svmdata_ls"
dat <- rbind(svmdata,svmdata_ls)
base <- ggplot(data=dat,aes(x=k, y=gap, colour = grp,group=grp)) + 
    geom_line(size=1.5) + #geom_point(size=4, shape=21) +
    geom_line(size=1.5) +
    #ggtitle("svmdata_ls gap and svmdata gap vs k") +
    geom_smooth(alpha=.2, size=1) +
    geom_abline(colour = "grey50", size = 2) +
    xlim(0, max(svmdata$k)) +
    ylim(0, y_max) +
    scale_colour_manual("", 
                        breaks = c("svmdata_ls", "svmdata"),
                        values = c("#dd3497", "#54278f")) 
joran
  • 169,992
  • 32
  • 429
  • 468