-1

I’m trying to have a plot that contain 2 bar charts in the same plot with different scales. My data contains 3 columns: Year_climate_floods Year(from2016to22) Climate(maxtemp,mintemp,RH,rainfall levels, windspeed, windirection) Floods (have 2 station each have discharge and gauges levels)

I’m trying to add climate bar next to floods bar for each year..problem is they have different scale tried to add asc.axis but nothing shows up

Also all the variables are yearly averaged ..is it possible to add wind direction mode to this or remove it all together?

zephryl
  • 14,633
  • 3
  • 11
  • 30
Teema
  • 11
  • 2
  • Try to include example of your code and your data. It is likely that you have to adjust your data so they match first part (if the first part is in thousands, then multiply the second one to match its level). See https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – jyr Dec 22 '22 at 07:52
  • ggplot(data=ofile, mapping= aes(x=yearclimate, y=yearlyclimaticaverage, fill=yearlyclimaticfactors))+ geom_bar(stat='identity', position='dodge')+ geom_bar(stat='identity', position='dodge')+ scale_y_continuous('climatic factors', sec.axis=sec_axis(~.*100, name='flood levels'))+ geom_bar(stat='identity',position = 'dodge',aes(y=yearlyfloodingaverage, color=yearlyflooding)) – Teema Dec 22 '22 at 12:20
  • #im sorry if this seems duplicated but i already went through other questions mostly they use bar and line charts in same graph, whereas I'm using 2 bar charts and possibly later a line chart to link cases of malaria with climatic/flood factors using geom-line is easier i think cause i found the answer in already asked Qs but not for geom_bar , or blame it on my little knowledge anyway – Teema Dec 22 '22 at 12:26

1 Answers1

0

It's much easier to help (and also know what you are looking for) with a reproducible example but it seems to me if you have two station values that are the same each year (which don't differ between the climate factors for that year) a secondary line graph would be more appropriate. Here is an example with dummy data for three years. You can play with the rescaling factor to position the lines. You can then use facets to add the station gauges into another graph or just add additional lines. Also I can't tell if you meant to have different color outlines for each bar, but I don't think so, so I corrected that, and it looks like you have some NAs in your data that is causing the extra blank categories in your legend.

Dummy data:

df <- data.frame(Year_climate_floods  = c(2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018),
              yearlyclimaticfactors = c('maxtemp','mintemp','RH','rainfall levels','maxtemp','mintemp','RH','rainfall levels','maxtemp','mintemp','RH','rainfall levels'),
             ClimateValue= sample(0:15000, 12, replace = TRUE),
              station1 = c('Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge','Station1_discharge'),
              Station1Value= c(950000, 950000, 950000, 950000, 1300000, 1300000, 1300000, 1300000,1050000, 1050000, 1050000, 1050000),
             station2 = c('Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge','Station2_discharge'),
              Station2Value= c(1150000, 1150000, 1150000, 1150000, 1200000, 1200000, 1200000, 1200000,1350000, 1350000, 1350000, 1350000))

Rescaling factor for secondary y-axis

max_cv=max(df$ClimateValue)

max_sv=max(df$Station1Value,df$Station2Value)

rescale.factor=(max_sv/max_cv)

Plot

ggplot(data = df, aes(x = as.integer(Year_climate_floods), y = ClimateValue, fill = yearlyclimaticfactors)) + theme_classic() +  geom_bar(stat = 'identity', position="dodge", color="black", size=.5, width=.8) + geom_line(data = df, aes(y = Station1Value/rescale.factor, x=Year_climate_floods , group=station1),  linetype = "dashed")+ geom_line(data = df, aes(y = Station2Value/rescale.factor, x=Year_climate_floods , group=station2, size=1),  linetype = "solid", size=1) +scale_x_continuous(name="Year", breaks=c(2016,2017, 2018))+ scale_y_continuous(name = "Climate value", sec.axis = sec_axis(trans = ~.*rescale.factor, name = "Station value", breaks=c(0,500000,1000000,1500000),labels = scales::comma)) + theme(axis.text=element_text(size=14),axis.title=element_text(size=16, face="bold"), legend.position = "bottom") + scale_fill_manual(name="Climate factor", values=c("red", "green", "blue", "yellow"))

enter image description here