更新时间:2023-09-23 19:52:46
为了实现这一点,您必须将图内边距设置为零。你可以用 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] <- 1
df <- 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:
scale_y_continuous
, you can define the axis label inside that part and you don't need a seperarate ylab
.colour=I("black")
to colour="black"
inside geom_line
.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: