0
votes

I already plot chart with plotly. You can see how is look like code

 library(plotly)  
                      fig <- plot_ly(
                        type='histogram',
                        x=~rnorm(100, 5))
                        fig <- fig %>% add_trace(
                        type='histogram',
                        x=~rnorm(20, 5))
                      
                      fig <- fig %>% layout(
                        barmode="stack",
                        bargap=0.1)
                      
                      fig

enter image description here

This plot have two colors orange and blue. So now I want to plot other plot but now with ggplot2 but with same colors.You can see code and pic below

library(ggplot2)
                      
                      specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
                      condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
                      value <- abs(rnorm(12 , 0 , 15))
                      data <- data.frame(specie,condition,value)
                      ggplot(data, aes(fill=condition, y=value, x=specie)) + 
                        geom_bar(position="stack", stat="identity")+scale_color_manual(values=c("royalblue", "dark orange1"
                        ))

enter image description here

But here I have different colors.So can anybody help me how to get same colors (blue and orange) from plotly and make graph with ggplot2?

1

1 Answers

1
votes

Two problems. You want scale_fill_manual instead of scale_color_manual. And you only provide 2 colors but you have three levels of condition.

You didn't provide data for the histogram - think more about what you are trying to accomplish with this pair of plots (i.e., how does that data relate to the data for the bar plot?).

Here I've added a third color to get the plot to render.

ggplot(data, aes(fill=condition, y=value, x=specie)) + 
geom_bar(position="stack", stat="identity")+     
scale_fill_manual(values=c("royalblue", "dark orange1", "red"))

barchart with three colors