本文介绍了在ggplot2中的两个面之间画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我如何在两个方面之间画几条线? 我试图通过在顶部图的最小值处绘制点,但它们不在两个方面之间。见下图。 这是我到目前为止的代码: t y1 y2 - cumsum(y1)z z [ (t = t,值= c(y2,y1),type = rep(c(字节,变化), ),each = 1000)) points vline.data< - data.frame(type = c(Bytes,Bytes,Changes,Changes),v1 = c(1,5,20,5)) g geom_line(color = I(black))+ facet_grid(type〜 ,scale =free)+ scale_y_continuous(trans =log10)+ ylab(Log values)+ theme(axis.text.x = element_text(angle = 90 ,hjust = 1),panel.margin = unit(0,lines))+ geom_point(data = points,aes(x = x,y = y),color =green) g 解决方案为了实现这一点,您必须将图内边距设置为零。你可以用 expand = c(0,0)来实现。我对你的代码所作的修改: 当你使用 scale_y_continuous 时,你可以定义该部件中的轴标签,并且不需要分离 ylab 。 更改 color =在 geom_line 中,我(黑色)到 color =black li> 添加 expand = c(0,0)至 scale_x_continuous 和 scale_y_continuous 。 完整的代码: ggplot(data = df,aes(x = t,y = values))+ geom_line(color =black)+ geom_point data = points,aes(x = x,y = y),color =green)+ facet_grid(type〜。,scales =free)+ scale_x_continuous(t = c(0,0))+ scale_y_continuous(Log values,trans =log10,expand = c(0,0))+ theme(axis.text.x = element_text( angle = 90,vjust = 0.5),panel.margin = unit(0,lines)) 给出: 添加行也可以用 geom_segment 。通常线条(线段)将出现在两个面上。如果你希望它们出现在两个方面之间,你必须在 data 参数中限制它: ggplot(data = df,aes(x = t,y = values))+ geom_line(color =black)+ geom_segment(data = df [df $ type ==Bytes,],aes(x = 10,y = 0,xend = 200,yend = 0),color =green,size = 2)+ geom_segment(data = df [ df $ type ==Bytes,],aes(x = 300,y = 0,xend = 350,yend = 0),color =green,size = 1)+ facet_grid(type〜 ,scale =free)+ scale_x_continuous(t,expand = c(0,0))+ scale_y_continuous(Log values,trans =log10,expand = c(0 ,0))+ theme(axis.text.x = element_text(angle = 90,vjust = 0.5),panel.margin = unit(0,lines)) 给出: How can I draw several lines between two facets?I attempted this by plotting points at the min value of the top graph but they are not between the two facets. See picture below.This is my code so far:t <- seq(1:1000)y1 <- rexp(1000)y2 <- cumsum(y1)z <- rep(NA, length(t))z[100:200] <- 1df <- data.frame(t=t, values=c(y2,y1), type=rep(c("Bytes","Changes"), each=1000))points <- data.frame(x=c(10:200,300:350), y=min(y2), type=rep("Bytes",242))vline.data <- data.frame(type = c("Bytes","Bytes","Changes","Changes"), vl=c(1,5,20,5))g <- ggplot(data=df, aes(x=t, y=values)) + geom_line(colour=I("black")) + facet_grid(type ~ ., scales="free") + scale_y_continuous(trans="log10") + ylab("Log values") + theme(axis.text.x = element_text(angle = 90, hjust = 1), panel.margin = unit(0, "lines"))+ geom_point(data=points, aes(x = x, y = y), colour="green")g 解决方案 In order to achieve that, you have to set the margins inside the plot to zero. You can do that with expand=c(0,0). The changes I made to your code:When you use scale_y_continuous, you can define the axis label inside that part and you don't need a seperarate ylab.Changed colour=I("black") to colour="black" inside geom_line.Added expand=c(0,0) to scale_x_continuous and scale_y_continuous.The complete code:ggplot(data=df, aes(x=t, y=values)) + geom_line(colour="black") + geom_point(data=points, aes(x = x, y = y), colour="green") + facet_grid(type ~ ., scales="free") + scale_x_continuous("t", expand=c(0,0)) + scale_y_continuous("Log values", trans="log10", expand=c(0,0)) + theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))which gives:Adding lines can also be done with geom_segment. Normally the lines (segments) will appear in both facets. If you want them to appear between the two facets, you will have to restrict that in data parameter:ggplot(data=df, aes(x=t, y=values)) + geom_line(colour="black") + geom_segment(data=df[df$type=="Bytes",], aes(x=10, y=0, xend=200, yend=0), colour="green", size=2) + geom_segment(data=df[df$type=="Bytes",], aes(x=300, y=0, xend=350, yend=0), colour="green", size=1) + facet_grid(type ~ ., scales="free") + scale_x_continuous("t", expand=c(0,0)) + scale_y_continuous("Log values", trans="log10", expand=c(0,0)) + theme(axis.text.x=element_text(angle=90, vjust=0.5), panel.margin=unit(0, "lines"))which gives: 这篇关于在ggplot2中的两个面之间画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 23:28
查看更多