简体   繁体   English

ggplot2中的两条曲线的表示

[英]Presentation of two curves in ggplot2

Is there a more efficient way to present these data in ggplot2 ? 有没有更有效的方法来在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. 我知道这可以在python使用matlibplot实现,但我更喜欢ggplot2的视觉效果。

绘图

R code used to generate the plots: 用于生成图的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.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)

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"))

在此处输入图片说明

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()

在此处输入图片说明

I wanted to suggest the same idea as the JasonWang, but he was faster. 我想提出与JasonWang相同的想法,但他更快。 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 ggplot2不允许使用两个y轴,原因如下: 绘制有2个y轴,一个y轴在左侧,另一个y轴在右侧

It is misleading. 这是误导。

But if you still want to do it. 但是,如果您仍然想要这样做。 You can do it with base plot or dygraphs (for example): 您可以使用基本plot或有dygraphs plot来进行此dygraphs (例如):

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")

在此处输入图片说明

But this will give you points for var_acc, as it has a lot less observations. 但这将为您提供var_acc的积分,因为它的观察值要少得多。

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. 注意:这是近似值 ,您可能不想这样做。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM