I want to show two bar plots with geom points on it using subplots. my code -
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)
Now mean_df_delta
and mean_df_normalized
is my dataframe for bar plots and error bar. df_1
and df_2
is dataframe for geom points.
Now in above image legends are getting repeated. I only want legend 1 time. And I want that all the geom points are of colour black. Bar color should not get fill in geom point.
I had already seen this answer. But this is not applied in my case as I have different dataframes for bar and geom points.
dput(exp_files_con)
into your question? Please read through stackoverflow.com/questions/5963269/… to learn how to create reproducible examples in R. – hpesoj626aes()
useaes(show.legend = FALSE)
– anupdput(exp_files_con)
into your question. – hpesoj626