Mona Jalal Mona Jalal - 2 months ago 15
R Question

legend not showing, grid doesn't become finer

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

Answer

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

Comments