Я хочу показать два столбчатых графика с точками геом на нем с использованием вспомогательных участков.
мой код -
df_1 <- iris[,c("Species", "Petal.Length","Petal.Width")]
df_2 <- iris[,c("Species", "Sepal.Length","Sepal.Width")]
df_1["Condition_1"] <- "P condition"
df_1[10:20,"Condition_1"] <- "Q condition"
df_1[20:30,"Condition_1"] <- "R condition"
df_1[30:40,"Condition_1"] <- "S condition"
df_1[40:50,"Condition_1"] <- "T condition"
df_1[60:70,"Condition_1"] <- "Q condition"
df_1[70:80,"Condition_1"] <- "R condition"
df_1[80:90,"Condition_1"] <- "S condition"
df_1[90:100,"Condition_1"] <- "T condition"
df_1[110:120,"Condition_1"] <- "Q condition"
df_1[120:130,"Condition_1"] <- "R condition"
df_1[130:140,"Condition_1"] <- "S condition"
df_1[140:150,"Condition_1"] <- "T condition"
df_2["Condition_2"] <- "P condition"
df_2[10:20,"Condition_2"] <- "Q condition"
df_2[20:30,"Condition_2"] <- "R condition"
df_2[30:40,"Condition_2"] <- "S condition"
df_2[40:50,"Condition_2"] <- "T condition"
df_2[60:70,"Condition_2"] <- "Q condition"
df_2[70:80,"Condition_2"] <- "R condition"
df_2[80:90,"Condition_2"] <- "S condition"
df_2[90:100,"Condition_2"] <- "T condition"
df_2[110:120,"Condition_2"] <- "Q condition"
df_2[120:130,"Condition_2"] <- "R condition"
df_2[130:140,"Condition_2"] <- "S condition"
df_2[140:150,"Condition_2"] <- "T condition"
Condition_1 <- as.vector(unique(df_1$Condition))
Species_1 <- unique(df_1$Species)
mean_df_delta <- setNames(data.frame(matrix(ncol = 4, nrow = 0)), c("Species", "Condition", "Petal.Length", "Petal.Width"))
for(Species_name in Species_1){
mean_df_1 <- aggregate(.~Condition_1,data=df_1,function(x) c(M = mean(x)))
mean_df_1["Species"] <- Species_name
num <- assign(paste("sam_df", as.character(Species_name), sep = "_"), mean_df_1)
mean_df_delta <- rbind(mean_df_delta,num)
}
mean_df_normalized <- setNames(data.frame(matrix(ncol = 4, nrow = 0)), c("Species", "Condition", "Sepal.Length", "Sepal.Width"))
Species_2 <- unique(df_2$Species)
Condition_2 <- as.vector(unique(df_2$Condition))
for(Species_name in Species_2){
mean_df_2 <- aggregate(.~Condition_2,data=df_2,function(x) c(M = mean(x)))
mean_df_2["Species"] <- Species_name
num <- assign(paste("sam_df", as.character(Species_name), sep = "_"), mean_df_2)
mean_df_normalized <- rbind(mean_df_normalized,num)
}
delta_ct_plot <- ggplotly(ggplot(mean_df_delta, aes(fill=Condition_1, y=Petal.Length, x=Species)) + geom_bar(position=position_dodge(width=0.9), stat="identity") +
geom_errorbar(aes(ymax=Petal.Length+Petal.Width, ymin=Petal.Length-Petal.Width) , position=position_dodge(width=0.9), width=0.25) +
geom_point(data=df_1,
aes(Species,Petal.Length,color=Condition_1),position=position_dodge(width=0.9),
colour = "black")+theme(legend.title=element_blank()))
normalized_bar_plot <- ggplotly(ggplot(mean_df_normalized, aes(fill=Condition_2, y=Sepal.Length, x=Species)) + geom_bar(position=position_dodge(width=0.9), stat="identity") +
geom_errorbar(aes(ymax=Sepal.Length+Sepal.Width, ymin=Sepal.Length-Sepal.Width) , position=position_dodge(width=0.9), width=0.25) +
geom_point(data=df_2,
aes(Species,Sepal.Length,color=Condition_2),position=position_dodge(width=0.9),
colour = "black")+theme(legend.title=element_blank()))
subplot(delta_ct_plot, normalized_bar_plot)
Теперь mean_df_delta
и mean_df_normalized
- это мои данные для гистограмм и панели ошибок. df_1
и df_2
- это датафрейм для точек геом.
введите описание изображения здесь
Теперь на изображении выше легенды повторяются. Я хочу только легенду 1 раз.
И я хочу, чтобы все геометрические точки были черного цвета. Цвет полосы не должен заполнять точку geom.
Я уже видел этот ответ . Но это не применяется в моем случае, так как у меня есть разные кадры данных для точек бара и геом.