Try to search your question here, if you can't find : Ask Any Question Now ?

Not able to merge two legends

HomeCategory: stackoverflowNot able to merge two legends
Avatargaurav asked 1 month ago

I have plotted stacked bar plot and line graph in one which have two different data sets. I am getting two separate legends for both of them. I have tried all the possible things.
Please find the attached code.

alldata = data.frame(x, aircargo, autototal, govtreceipts, 
iipconsumer,nongimports, railfreight)
linedata = data.frame(x,ceii)
melteddata = melt(alldata,id.vars="x")
plotS1 <- ggplot(melteddata)
plotS1 +  geom_bar(aes(x=ordered_x,y=value,factor=variable,fill=variable, 
                       order=-as.numeric(variable)), stat="identity") +
  geom_line(data=linedata, aes(x=as.numeric(ordered_x),y=ceii, color = "CEII"), lwd=1.5) + 
  scale_color_manual( values = c("#000000")) + 
  scale_fill_manual(name = "Components", values = c("#0000FF", "#FFC0CB", "#00FFFF", "#00FF00", "#FF00FF", "#20B2AA", "#000000")) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + theme(plot.background = element_rect(fill = "#BFD5E3")) + 
  ggtitle("Monthly Contribution by Components (3 month MA)") + 
  theme( panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + labs( y = "", x = "") + 
  scale_y_continuous(labels = c("-0.30","-0.25","-0.2","-0.15","-0.10","-0.05", "0.00", "0.05", "0.10", "0.15", "0.20", "0.25", "0.30"), breaks = c(-0.30, -0.25, -0.20, -0.15, -0.10, -0.05, 0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30)) 

Dataset –

aircargo <- c(-0.027, 0.028, 0.044, 0.011, 0.041, 0.030, -0.028, 0.017, 0.001, 0.060, -0.040, 0.016, 0.006, -0.040, -0.003, 0)
autototal <- c(0.061, -0.004, 0.009, 0.024, -0.026, 0.025, -0.029, 0.000, -0.015, -0.016, 0.026, -0.062, 0.034, 0.002, -0.081, -0.005)
govtreceipts <- c(-0.001, 0.001, -0.005, 0.031, -0.023, 0.000, -0.009, 0.005, 0.002, -0.005, 0.004, 0.000, 0.004, -0.003, 0, 0)
iipconsumer <- c(0.043, -0.014, 0.041, -0.035, 0.001, 0.001, 0.040, 0.010, -0.006, 0.013, 0.001, -0.006, -0.002, -0.011, -0.033, 0)
nongimports <- c(0.018, -0.008, 0.015, -0.004, 0.019, -0.010, 0.008, 0.007, -0.021, 0.006, -0.002, -0.007, 0.009, -0.017, 0.005, 0)
railfreight <- c(0.014, -0.015, 0.031, 0.103, -0.041, 0.025, -0.044, 0.061, -0.050, 0.092, -0.045, 0.011, -0.007, 0.050, 0.100, -0.015)
x <- c(“Jan-18”, “Feb-18”, “Mar-18”, “Apr-18”, “May-18”, “Jun-18”, “Jul-18”, “Aug-18”, “Sep-18”, “Oct-18”, “Nov-18”, “Dec-18”, “Jan-19”, “Feb-19”, “Mar-19”, “Apr-19”)
ceii <- c(0.108, -0.012, 0.134, 0.131, -0.030, 0.072, -0.062, 0.100, -0.089, 0.149, -0.070, -0.047, 0.043, -0.019, -0.012, -0.020)

Please help in combining the legend. Thanks in advance.

1 Answers
Best Answer
AvatarJyoti answered 1 month ago
Your Answer

17 + 20 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com