Search code examples
rggplot2scale

Scale adjustments of sec.axis with ggplot


I'm trying to create a two y-axis plot. Individually when I plot my bar and line plots they seem to work fine but I'm having difficulties combining the two.

For my bar plot, here's a sample of what my data looks like

treatment month fruit_origin avg_weight
F400 4 1 33.669
F400 4 2 26.200

And here's my code for the plot itself:

f400plot_fw <- ggplot(data= f400weight, aes(x = factor(month), y = avg_weight, fill = factor(fruit_origin) , color = factor(fruit_origin))) +
  geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
  labs(x="", y="Aplot verage Fruit Weight (g)") + 
  geom_errorbar(aes(ymin = avg_weight - error, ymax = avg_weight + error), position = position_dodge(0.9), width = 0.25, show.legend = FALSE) +
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.position = "none", axis.title.y = element_text(size=9), axis.text = element_text(size = 8)) + ylim(0,50) +
  scale_fill_discrete(name = "Fruit Origin") + scale_color_discrete(name = "Fruit Origin") +
  scale_fill_manual(values=c('deepskyblue1','forestgreen', 'navyblue', 'blueviolet')) + scale_color_manual(values=c('grey4', 'grey4', 'grey4', 'grey4'))

Then similarly for my line plot the data and code for the plot look like the following

treatment month yield
F400 4 0.11
F400 5 2.02
lineup <- ggplot(data=montgomery, aes(x = month, y = yieldypoo, group =1)) +
  geom_line(size=0.5) +
  labs(x="Month of Production", y="Total Weekly Yield (kg)") + 
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())
lineup

barplot

line

When I combine the two this is what I code and the third image is what I get

ggplot() + geom_col(data= f400weight, aes(x = factor(month), y = avg_weight, fill = factor(fruit_origin) , color = factor(fruit_origin)), position = "dodge", alpha = 0.7) +
  labs(x="Month of Production", y="Average Fruit Weight (g)") + 
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_fill_discrete(name = "Fruit Origin") + scale_color_discrete(name = "Fruit Origin") +
  scale_fill_manual(values=c('deepskyblue1','forestgreen', 'navyblue', 'blueviolet')) + scale_color_manual(values=c('grey4', 'grey4', 'grey4', 'grey4')) +
  geom_line(data = montgomery, aes(x = month, y = yield), size = 0.5, group =1) +
  scale_y_continuous(sec.axis = sec_axis(~.*0.2, name = "Total Weekly Yield (kg)"))

image im trying to fix Not sure how to adjust the shift on the x-axis or how to get the line plot data to correlate with the second y-axis. Thanks in advance :)


Solution

  • First, make sure your x values are consistent. You use month in one case but factor(month) in another; below, I use factor(month) for both. Second, for cases like this, you need to transform the values for the second axis with the inverse transformation as the axis itself. ie, since the y-axis is transformed using ~.*0.2, transform the y values using yield/0.2.

    library(ggplot2)
    
    ggplot() + 
      geom_col(
        data = f400weight, 
        aes(factor(month), avg_weight, fill = factor(fruit_origin)), 
        color = "grey4", 
        position = "dodge", 
        alpha = 0.7
      ) +
      geom_line(data = montgomery, aes(factor(month), yield/0.2), size = 0.5, group = 1) +
      labs(x = "Month of Production", y = "Average Fruit Weight (g)") + 
      theme_bw() +
      theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
      scale_fill_manual("Fruit Origin", values = c('deepskyblue1','forestgreen', 'navyblue', 'blueviolet')) +
      scale_y_continuous(sec.axis = sec_axis(~.*0.2, name = "Total Weekly Yield (kg)"))
    

    Sample data:

    f400weight <- readr::read_tsv(
    'treatment  month   fruit_origin    avg_weight
    F400    4   1   33.669
    F400    4   2   26.200
    F400    5   1   30
    F400    5   2   25
    F400    6   1   22
    F400    6   2   30'
    )
    
    montgomery <- readr::read_tsv(
    'treatment  month   yield
    F400    4   0.11
    F400    5   2.02
    F400    6   2.5'
    )