0
votes

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.

result i am getting

enter image description here

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.

1
Can you paste the output of dput(exp_files_con) into your question? Please read through stackoverflow.com/questions/5963269/… to learn how to create reproducible examples in R.hpesoj626
In aes() use aes(show.legend = FALSE)anup
@anup already tried that.Also tried other likes hidelegend(), guides(fill=FALSE), scale_fill_discrete(guide=FALSE), theme(legend.position="none").Only hidelegend() and theme(legend.position="none") work and it removes all the legend if i put these in second plot. i.e. in second argument of subplotprateekeshwar singh
@hpesoj626 it is dataframe with 4 column Condition,Value,Gene,Sd_Error and there respective valueprateekeshwar singh
@prateekeshwarsingh, again, please read through this link: stackoverflow.com/questions/5963269/…. We cannot help you if we can't reproduce your problem. We can only reproduce your problem if you provide us your data set. The easiest way to do that is to paste the output of dput(exp_files_con) into your question.hpesoj626

1 Answers

0
votes

It seems like ggplotly is rendering each of the group labels from each of the subplots. Use facet_wrap() instead to generate only 1 set of labels. To do this, I have changed the loops you did into an entirely "tidy" approach.

library(tidyverse)
library(plotly)
df <- iris

df["Condition"] <- "P condition"
df[10:20,"Condition"] <- "Q condition"
df[20:30,"Condition"] <- "R condition"
df[30:40,"Condition"] <- "S condition"
df[40:50,"Condition"] <- "T condition"

df[60:70,"Condition"] <- "Q condition"
df[70:80,"Condition"] <- "R condition"
df[80:90,"Condition"] <- "S condition"
df[90:100,"Condition"] <- "T condition"

df[110:120,"Condition"] <- "Q condition"
df[120:130,"Condition"] <- "R condition"
df[130:140,"Condition"] <- "S condition"
df[140:150,"Condition"] <- "T condition"

df1 <- df %>%
  gather(var, value, -Condition, -Species) %>%
  ungroup() %>%
  separate(var, into = c("var", "measure")) %>%
  group_by(measure) %>%
  mutate(id = row_number()) %>%
  spread(measure, value) %>%
  select(-id)


df2 <- df1 %>%
  group_by(Condition) %>%
  mutate_at(vars(Length:Width), mean) %>%
  ungroup() %>%
  mutate(ymax = Length + Width,
         ymin = Length - Width) %>%
  unique()



p <- df2 %>% ggplot(aes(fill=Condition, y=Length, x=Species)) + geom_bar(position=position_dodge(width=0.9), stat="identity") +
  geom_errorbar(aes(ymax=ymax, ymin=ymin) , position=position_dodge(width=0.9), width=0.25) +
  geom_point(data=df1,
             aes(Species,Length,color=Condition),position=position_dodge(width=0.9),
             colour = "black")+theme(legend.title=element_blank()) + 
  facet_wrap(~var, scales = "free_y") + xlab(NULL) + ylab(NULL)

ggplotly(p)  

enter image description here

I also have to note that averaging by Condition only doesn't make sense to me. But I think you just have to adjust to your use case.