0
    > dput(figure4)
structure(list(Days = c(1.3, 3, 4, 6, 7.7, 1, 4, 7.7, 1, 4, 6, 
7.7), type = c("Indirect", "Indirect", "Indirect", "Indirect", 
"Indirect", "Direct_control", "Direct_control", "Direct_control", 
"Temp_control", "Temp_control", "Temp_control", "Temp_control"
), fraction = c(100, 75.9, 63.8, 39.2, 32.4, 100, 96.35, 98.1, 
100, 107.05, 102.45, 96.26), ratio2 = c(2.87, 2.86, 2.94, 3.55, 
3.8, 3.99, 3.638, 4.189, 1.088, 1.112, 1.07, 1.12), se2 = c(15, 
11.38, 9.57, 5.88, 4.86, 15, 14.88, 14.7, 15, 16.0571, 15.368, 
14.439)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L))
> 

figure

I would like to have legend with the lines. They belong to the second y axis, but I am struggling to achieve that. Any suggestion?

ylim.prim <- c(0, 120)   # in this example, percentage
ylim.sec <- c(0, 5)    # in this example, ratio
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
ggplot(figure4, aes(x=Days, y=fraction, shape = type, colour = type)) +
  geom_point(size=4) +
  geom_errorbar(aes(ymax = fraction + se2, ymin = fraction - se2, group= type),  width = 0.25)+
  geom_line(aes(x=Days, y=a + ratio2*b,colour = type, linetype = Ratio), size= 1)+
  scale_y_continuous("Fraction remainig(%)", sec.axis = sec_axis(~ (. - a)/b, name = "Z/E ratio"))+ theme(text = element_text(size = 15))+ 
  theme(axis.text.x = element_text(angle = 0, vjust = 1, hjust= 1,size =12 ))+
  theme(axis.text.y = element_text(size =12 ))+
   theme(text = element_text(size = 15)) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))`
  • 3
    Welcome to SO! At a first glance your code looks fine and should give a separate legend for the `linetype`s. But without any data to run you code one can only guess what's the issue. To this end have a look at how to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and especially the section on how to provide a snippet of your data. – stefan Oct 21 '22 at 13:28
  • 1
    Thank you Stefan for the feedback, i have added the input data on top – Tetyana Gilevska Oct 21 '22 at 14:30
  • 1
    Still easier for you and us would be to use the `dput()` function to share your data, i.e. type `dput(dat)` into the console and copy the output into your post. But anyway: Your data is lacking a `Ratio` column which according to your code is mapped on the `linetype` aes. Maybe you could check that too. – stefan Oct 21 '22 at 14:35
  • In `geom_line(...)` `Ratio` is not defined. Could you please resolve this so as to make the question reproducible? – Peter Oct 21 '22 at 15:59

1 Answers1

0

I hope understand it right. You want to exchange the legend you have for the primary y-axis by one for the secondary y-axis. You could do this by using show.legend = F for the geoms which should not be seen in the legend.

The Ratio value is missing, so linetype = type was used for styling the lines.

figure4  <- 
  structure(list(Days = c(1.3, 3, 4, 6, 7.7, 1, 4, 7.7, 1, 4, 6, 
                          7.7), type = c("Indirect", "Indirect", "Indirect", "Indirect", 
                                         "Indirect", "Direct_control", "Direct_control", "Direct_control", 
                                         "Temp_control", "Temp_control", "Temp_control", "Temp_control"
                          ), fraction = c(100, 75.9, 63.8, 39.2, 32.4, 100, 96.35, 98.1, 
                                          100, 107.05, 102.45, 96.26), ratio2 = c(2.87, 2.86, 2.94, 3.55, 
                                                                                  3.8, 3.99, 3.638, 4.189, 1.088, 1.112, 1.07, 1.12), se2 = c(15, 
                                                                                                                                              11.38, 9.57, 5.88, 4.86, 15, 14.88, 14.7, 15, 16.0571, 15.368, 
                                                                                                                                              14.439)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,-12L))

library(ggplot2)

ylim.prim <- c(0, 120)   # in this example, percentage
ylim.sec <- c(0, 5)    # in this example, ratio
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
ggplot(figure4, aes(x = Days, y = fraction, shape = type, colour = type)) +
  geom_point(size = 4, show.legend = F) +
  geom_errorbar(aes(ymax = fraction + se2, ymin = fraction - se2,
                    group = type), show.legend = F, width = 0.25) +
  geom_line(aes(x = Days, y = a + ratio2 * b,
                colour = type, linetype = type), size = 1) +
  scale_y_continuous("Fraction remainig(%)",
                     sec.axis = sec_axis(~ (. - a) / b, name = "Z/E ratio")) +
  theme(axis.text.x = element_text(angle = 0, vjust = 1, hjust= 1,size =12 ))+
  theme(axis.text.y = element_text(size =12 ))+
  theme(text = element_text(size = 15)) +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black")) +
  theme_bw()

MarBlo
  • 4,195
  • 1
  • 13
  • 27