Так что мне удалось создать два графика ggplot и построить их идеально совмещенные с grid.arrange
. Теперь я хотел бы подключить min, 1Q, 3Q и max boxplot с распределением на левом графике. Я построил горизонтальные (красные полосатые) линии с geom_hline
на обоих графиках, но мне хотелось бы, чтобы эти линии были связаны, так что пройдите через пробел между графиками. Какие-либо предложения? Вот график: Нарисуйте линии между двумя разными («grid.arranged») графиками
О да, усложняющий фактор: на правильных графах его координаты перевернуты!
Вот воспроизводимый пример:
library(ggplot2)
library(scales)
library(gridExtra)
# create some data
df_ahn <- data.frame(yh=rnorm(1000,0.23,0.05))
df_peil <- data.frame(hoogte = rnorm(1000,0,1))
# vector met de hoogtes
Summary_df <- summary(df_ahn$yh)
p_peil <- ggplot(df_peil,aes(x=hoogte))+
geom_histogram(aes(y=cumsum((..count..)/sum(..count..))), binwidth = 0.01,fill="gray")+
stat_bin(aes(y=cumsum((..count..)/sum(..count..))),binwidth = 0.01,geom="line",color="black") +
geom_vline(aes(xintercept = as.vector(Summary_df[1])), lty = 2,color =2)+
geom_vline(aes(xintercept = as.vector(Summary_df[2])), lty = 2,color =2)+
geom_vline(aes(xintercept = as.vector(Summary_df[5])), lty = 2,color =2)+
geom_vline(aes(xintercept = as.vector(Summary_df[6])), lty = 2,color =2)+
coord_flip() +
ggtitle("Onderschrijdingsfrequentie\n waterstand in kreek") +
xlab("Hoogte in meter NAP") +
ylab("Onderschrijdingsfrequentie in % (10% = 36,5 dagen/jaar)") +
scale_y_continuous(limits = c(0, 1),labels = percent, breaks=seq(0,1,by=0.1)) +
scale_x_continuous(limits = c(-0.5, 0.6), breaks=seq(-0.5,0.6,by=0.1)) +
theme(panel.background = element_rect(fill = "transparent",colour = "black"),
panel.grid.major = element_line(colour = "darkgray"),
panel.grid.minor = element_line(colour = "gray"),
strip.background = element_rect(fill="gray"),
strip.text = element_text(size=14, color="black"),
axis.ticks.y = element_line(colour = "black"),
axis.ticks.x = element_line(colour = "black"),
axis.text.x = element_text(size=14, color="black"),
axis.text.y = element_text(size=14, color="black"),
axis.title = element_text(size=14, color="black")
)
p_ahn <- ggplot(df_ahn, aes(x=1, y=yh)) +
geom_boxplot(outlier.size=3, outlier.shape=1) +
geom_hline(aes(yintercept = as.vector(Summary_df[1])), lty = 2,color =2)+
geom_hline(aes(yintercept = as.vector(Summary_df[2])), lty = 2,color =2)+
geom_hline(aes(yintercept = as.vector(Summary_df[5])), lty = 2,color =2)+
geom_hline(aes(yintercept = as.vector(Summary_df[6])), lty = 2,color =2)+
scale_y_continuous(limits = c(-0.5,0.6), breaks=seq(-0.5,0.6,by=0.1)) +
ggtitle("Hoogte groeiplaatsen\nKruipend moerascherm") +
ylab("") +
xlab("") +
theme(panel.background = element_rect(fill = "transparent",colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_rect(fill="gray"),
strip.text = element_text(size=14, color="black"),
axis.ticks.y = element_line(colour = "black"),
axis.ticks.x = element_line(colour = "white"),
axis.text.x = element_text(size=14, color="white"),
axis.text.y = element_text(size=14, color="black"),
axis.title = element_text(size=14, color="black", face="bold")
)
grid.arrange(p_peil,p_ahn, layout_matrix = matrix(c(1,1,1,2), nrow=1, byrow=TRUE), ncol = 4)
[Этот вопрос и ответы] (http://stackoverflow.com/questions/17492230/how-to-place-grobs-with-annotation-custom-at-precise-areas-of-the-plot-region) могут иметь значение. – Henrik
или [этот] (http://stackoverflow.com/questions/31690007/ggplot-drawing-line-between-points-across-facets/31691313#31691313). Мне кажется, что фацетинг будет лучшим вариантом. – baptiste