2016-03-06 161 views
-1

ggplot2中是否有更高效的方式显示这些数据? 理想情况下,我希望他们都在一个情节。我知道这可以通过pythonmatlibplot实现,但我更喜欢ggplot2的视觉效果。用于生成地块在ggplot2中显示两条曲线

Rplot

R代码:

#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.) 
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) 
+1

看到这个吗? http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph – JasonWang

+0

我有不同的情况。我的y轴彼此不同,即不同的比例。 –

+2

ggplot2不支持二级缩放。对于这种故意的设计选择,有[良好的,有效的理由](http://stackoverflow.com/a/3101876/1412059)。如果您绝对必须通过使用辅助轴将这两个图组合成一个图,则可以使用一些非常复杂的hack或使用R基本图形。 – Roland

回答

1

你仍然可以使用其他层添加一层相同的概念。

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

或者结合在一起的两个数据帧,并为彩色另一个变量。

# 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

1

我想提出同样的理念为JasonWang,但他速度更快。我认为这是要走的路(因此我自己提出)。

GGPLOT2不允许两个Y轴,是有原因的:Plot with 2 y axes, one y axis on the left, and another y axis on the right

这是一种误导。

但是,如果你仍然想这样做。您可以使用基地plotdygraphs(例如)做到这一点:

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

但是,这会给你点var_acc,因为它少了很多意见。

您可以填写它。

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

注:这近似值,这可能不是你想要做的事。 enter image description here

+1

尽管Hadley严重反对使用两个y轴,但实际上有人在'ggplot2'中找到了一种方法:https://rpubs.com/kohske/dual_axis_in_ggplot2 – ytk

+0

感谢您的有用链接! –