-1

Is there a more efficient way to present these data in ggplot2? Ideally, I would like them both in one plot. I know this can be achieved in python with matlibplot, but I like the visuals of ggplot2 better.

Rplot

R code used to generate the plots:

#load libraries
library(ggplot2)
library (gridExtra)
library(scales)   

#generate some data plot 1
var_iter <- c(seq(0, 4000, 20))
x <- runif(201,0.877813, 2.283210)
var_loss <- c(sort(x, decreasing = TRUE))
rndm1 <- data.frame(var_iter, var_loss)



#generate some data plot 2
var_iter2 <- c(seq(0, 3500, 500))
x2 <- runif(8,0.1821, 0.6675)
var_acc  <- c(sort(x2, decreasing = FALSE))
rndm2 <- data.frame(var_iter2, var_acc)



#plot loss
c <- ggplot(data=rndm1, aes(x=var_iter, y=var_loss)) + geom_line(aes(colour="Log Loss"))  +
  scale_colour_manual(name='', values=c('Log Loss'='#00BFC4'))  + #theme_bw() +
  xlab("iterations") + ylab("log loss") + theme(legend.position=c(1,1),legend.justification=c(1,1),
                                                legend.direction="horizontal",
                                                legend.box="horizontal",
                                                legend.box.just = c("top"), 
                                                legend.background = element_rect(fill=alpha('white', 0.3)))



#plot accuracy
d <- ggplot(data=rndm2, aes(x=var_iter2, y=var_acc))  + geom_line(aes(colour="Accuracy"))  +
  scale_colour_manual(name='', values=c('Accuracy'='#F8766D')) + #theme_bw() +
  xlab("iterations") + ylab("accuracy") + theme(legend.position=c(0.80, 1),legend.justification=c(1,1),
                                                legend.direction="horizontal",
                                                legend.box="horizontal",
                                                legend.box.just = c("top"), 
                                                legend.background = element_rect(fill=alpha('white', 0.3)))



grid.arrange(c, d, ncol=2)
apples-oranges
  • 959
  • 2
  • 9
  • 21
  • 1
    See this one? http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph – JasonWang Mar 06 '16 at 16:57
  • I have a different case. My y-axes are different from each other i.e. different scales. – apples-oranges Mar 06 '16 at 17:08
  • 2
    ggplot2 doesn't support secondary scales. There are [good, valid reasons](http://stackoverflow.com/a/3101876/1412059) for this deliberate design choice. If you absolutely must combine these two plots into one by use of a secondary axis, you can use some very intricate hack or use R base graphics. – Roland Mar 06 '16 at 17:22
  • Thanks for that comment, it is clear now. – apples-oranges Mar 06 '16 at 17:35

2 Answers2

1

You still can use the same concept of adding a layer on another layer.

ggplot(rndm1, aes(x=var_iter)) + 
  geom_line(aes(y=var_loss, color="var_loss")) + 
  geom_line(data=rndm2, aes(x=var_iter2, y=var_acc, color="var_acc"))

enter image description here

Or combine two data frame together and create another variable for color.

# Change the column name, so they can combine together
names(rndm1) <- c("x", "y")
names(rndm2) <- c("x", "y")
rndm <- rbind(rndm1, rndm2)

# Create a variable for color
rndm$group <- rep(c("Log Loss", "Accuracy"), c(dim(rndm1)[1], dim(rndm2)[1]))
ggplot(rndm, aes(x=x, y=y, color=group)) + geom_line()

enter image description here

JasonWang
  • 2,414
  • 11
  • 12
1

I wanted to suggest the same idea as the JasonWang, but he was faster. I think it is the way to go (hence I upvoted it myself).

ggplot2 doesn't allow two y axis, for a reason: Plot with 2 y axes, one y axis on the left, and another y axis on the right

It is misleading.

But if you still want to do it. You can do it with base plot or dygraphs (for example):

rndm2$var_iter <- rndm2$var_iter2
rndm2$var_iter2 <- NULL
merged.rndm <- merge(rndm1, rndm2, all = TRUE)

dygraph(merged.rndm) %>% dySeries("var_acc", axis = "y2")

enter image description here

But this will give you points for var_acc, as it has a lot less observations.

You could fill it.

merged.rndm1 <- as.data.frame(zoo::na.approx(merged.rndm))
dygraph(merged.rndm1) %>% dySeries("var_acc", axis = "y2")

Note: this has approximated values, which might not be something you want to do. enter image description here

Community
  • 1
  • 1
Jav
  • 2,203
  • 13
  • 22
  • 1
    While Hadley is heavily against using two y-axes, someone actually found a way to do it in `ggplot2`: https://rpubs.com/kohske/dual_axis_in_ggplot2 – ytk Mar 06 '16 at 17:58