1

Overview

I would like to produce a secondary y-axis showing percentages between 0-100 % by 5 % intervals (i.e. 0, 5, 10, 15, etc).

So far, I managed to produce the secondary y-axis, but I cannot figure out how to change the scale in intervals of 5 %.

Problem

I successfully changed the scale of secondary y-axis but my code also changed the first y-axis to the same scale, thus affecting the visual presentation of the boxes and squashing them vertically together.

Desired Result

The scale in the first y-axis scale denotes latitude coordinates, so this scale cannot be changed.

Does anyone know how I can custom the scale of the secondary y-axis (percentages 0-100 %, by 5 %) in R without affecting the scale of first y-axis using the following R-code and data frame below?

If anyone can help, I would be deeply appreciative.

R-Code

        ##New Plot Window 
          dev.new()
        ##Leave space for z axis
          par(mar = c(5, 4, 4, 4) + 0.3)

        ##Potentially set the second data set for Canopy Cover index onto a different scale 0-100%
        ##I am not sure if this object is useful to produce the scale for the secondary y-axis
             Canopy_Scale_ylabel <- seq(0, 100, by = 5)

        ##First Plot for the key parameters and latitude 

        QuercusParameterLat1<-ggplot(MeltedParameterLatitude1, aes(x = Key_Parameters, y = Latitude, fill=Key_Parameter_Category_Values)) + 
                                     geom_boxplot() +
                                     theme(axis.text.x = element_text(angle = 15, hjust = 1), text = element_text(size=10)) + 
                                     scale_x_discrete(labels=c("Stand Density Index", "Urbansiation Index", "Phenological Index"))+
                                     theme(panel.background = element_blank(), 
                                     panel.grid.major = element_blank(), 
                                     panel.grid.minor = element_blank(),
                                     panel.border = element_blank()) + 
                                     theme(axis.line.x = element_line(color="black", size = 0.8),
                                     axis.line.y = element_line(color="black", size = 0.8)) + 
                                     labs(x = "Key Parameters", y = "Latitude", size = 0.5) +
                                     theme(legend.position="right")
        ##Rename the legend title
        p11 <- QuercusParameterLat1 + guides(fill=guide_legend(title="Key Parameter Categories"))

# now adding the secondary axis, following the example in the help file ?scale_y_continuous
        # and, very important, reverting the above transformation
        p_yaxis_scale1 <- p11 + scale_y_continuous(sec.axis = sec_axis(~.+1, name = "Canopy Index %"), limit=c(0, 100))

Following dww's suggestion of inserting limits and breaks to solve the issue:

Attempt 1

 # now adding the secondary axis, following the example in the help file ?scale_y_continuous
 # and, very important, reverting the above transformation

p_yaxis_scale <- p11 + scale_y_continuous(sec.axis = sec_axis(~.+1, name = "Canopy Index %"), breaks=round(seq(min(MeltedParameterLatitude1$Canopy_Index), max(MeltedParameterLatitude1$Canopy_Index), by=5), 2))

##Error message

Error in seq.default(min(MeltedParameterLatitude1$Canopy_Index), max(MeltedParameterLatitude1$Canopy_Index),  : 
  'from' must be a finite number
In addition: Warning messages:
1: In min(MeltedParameterLatitude1$Canopy_Index) :
  no non-missing arguments to min; returning Inf
2: In max(MeltedParameterLatitude1$Canopy_Index) :
  no non-missing arguments to max; returning -Inf

Attempt 2

##There are 20 tick marks for 0-100 by 5
##n=20

p_yaxis_scale <- p11 + scale_y_continuous(sec.axis = sec_axis(~.+1, name = "Canopy Index %"), breaks=scales::pretty_breaks(MeltedParameterLatitude1$Category_Index, n=20))

   ##Error message

     Error in pretty.default(x, n, ...) : invalid 'min.n' argument

Plot produced from R-code

enter image description here

Dataframe

structure(list(Key_Parameters = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1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, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Stand_density_index", 
"Urbanisation_index", "Phenological_Index"), class = "factor"), 
    Latitude = c(51.4175, 52.12087, 52.0269, 52.0269, 52.0269, 
    52.0269, 52.947709, 52.947709, 51.491811, 51.491811, 52.59925, 
    52.59925, 52.59925, 52.59925, 51.60157, 51.60157, 52.6888, 
    52.6888, 52.6888, 52.6888, 50.697802, 50.697802, 50.697802, 
    50.697802, 53.62417, 50.446841, 50.446841, 53.959679, 53.959679, 
    53.959679, 53.959679, 51.78375, 51.78375, 51.78375, 51.78375, 
    51.456965, 51.456965, 51.456965, 51.456965, 51.3651, 51.3651, 
    51.3651, 51.3651, 52.01182, 52.01182, 52.01182, 52.01182, 
    50.114277, 50.114277, 51.43474, 51.43474, 51.10676, 51.10676, 
    51.10676, 51.10676, 50.435984, 50.435984, 50.435984, 50.435984, 
    51.78666, 51.78666, 52.441088, 52.441088, 52.552344, 49.259471, 
    49.259471, 49.259471, 49.259471, 51.746642, 51.746642, 51.746642, 
    51.746642, 52.2501, 52.2501, 52.2501, 52.2501, 52.423336, 
    52.423336, 52.423336, 52.423336, 53.615575, 53.615575, 53.615575, 
    53.615575, 51.08474, 51.08474, 51.08474, 53.19329, 53.19329, 
    53.19329, 53.19329, 55.96785, 55.96785, 56.52664, 56.52664, 
    56.52664, 56.52664, 51.8113, 51.8113, 51.8113, 51.8113, 52.580157, 
    52.580157, 52.580157, 52.580157, 50.52008, 50.52008, 50.52008, 
    50.52008, 51.48417, 51.48417, 51.48417, 51.48417, 54.58243, 
    54.58243, 54.58243, 54.58243, 52.58839, 52.58839, 52.58839, 
    52.58839, 52.717283, 52.717283, 52.717283, 52.717283, 50.740764, 
    50.740764, 50.740764, 50.740764, 52.57937, 52.57937, 52.57937, 
    52.57937, 50.736531, 50.736531, 50.79926, 50.79926, 50.79926, 
    53.675996, 53.675996, 51.36445, 51.36445, 51.36445, 51.36445, 
    52.122402, 52.122402, 52.122402, 52.16104, 52.16104, 55.91913, 
    51.6528, 51.6528, 51.6528, 51.6528, 51.88485, 51.88485, 51.88485, 
    51.88485, 52.34015, 52.34015, 52.34015, 52.026042, 52.026042, 
    52.026042, 52.026042, 51.319032, 51.319032, 51.319032, 51.319032, 
    51.51357, 51.51357, 51.51357, 51.51357, 53.43202, 53.43202, 
    53.43202, 53.43202, 51.50823, 51.50823, 51.50823, 51.50823, 
    51.4175, 52.12087, 52.0269, 52.0269, 52.0269, 52.0269, 52.947709, 
    52.947709, 51.491811, 51.491811, 52.59925, 52.59925, 52.59925, 
    52.59925, 51.60157, 51.60157, 52.6888, 52.6888, 52.6888, 
    52.6888, 50.697802, 50.697802, 50.697802, 50.697802, 53.62417, 
    50.446841, 50.446841, 53.959679, 53.959679, 53.959679, 53.959679, 
    51.78375, 51.78375, 51.78375, 51.78375, 51.456965, 51.456965, 
    51.456965, 51.456965, 51.3651, 51.3651, 51.3651, 51.3651, 
    52.01182, 52.01182, 52.01182, 52.01182, 50.114277, 50.114277, 
    51.43474, 51.43474, 51.10676, 51.10676, 51.10676, 51.10676, 
    50.435984, 50.435984, 50.435984, 50.435984, 51.78666, 51.78666, 
    52.441088, 52.441088, 52.552344, 49.259471, 49.259471, 49.259471, 
    49.259471, 51.746642, 51.746642, 51.746642, 51.746642, 52.2501, 
    52.2501, 52.2501, 52.2501, 52.423336, 52.423336, 52.423336, 
    52.423336, 53.615575, 53.615575, 53.615575, 53.615575, 51.08474, 
    51.08474, 51.08474, 53.19329, 53.19329, 53.19329, 53.19329, 
    55.96785, 55.96785, 56.52664, 56.52664, 56.52664, 56.52664, 
    51.8113, 51.8113, 51.8113, 51.8113, 52.580157, 52.580157, 
    52.580157, 52.580157, 50.52008, 50.52008, 50.52008, 50.52008, 
    51.48417, 51.48417, 51.48417, 51.48417, 54.58243, 54.58243, 
    54.58243, 54.58243, 52.58839, 52.58839, 52.58839, 52.58839, 
    52.717283, 52.717283, 52.717283, 52.717283, 50.740764, 50.740764, 
    50.740764, 50.740764, 52.57937, 52.57937, 52.57937, 52.57937, 
    50.736531, 50.736531, 50.79926, 50.79926, 50.79926, 53.675996, 
    53.675996, 51.36445, 51.36445, 51.36445, 51.36445, 52.122402, 
    52.122402, 52.122402, 52.16104, 52.16104, 55.91913, 51.6528, 
    51.6528, 51.6528, 51.6528, 51.88485, 51.88485, 51.88485, 
    51.88485, 52.34015, 52.34015, 52.34015, 52.026042, 52.026042, 
    52.026042, 52.026042, 51.319032, 51.319032, 51.319032, 51.319032, 
    51.51357, 51.51357, 51.51357, 51.51357, 53.43202, 53.43202, 
    53.43202, 53.43202, 51.50823, 51.50823, 51.50823, 51.50823, 
    51.4175, 52.12087, 52.0269, 52.0269, 52.0269, 52.0269, 52.947709, 
    52.947709, 51.491811, 51.491811, 52.59925, 52.59925, 52.59925, 
    52.59925, 51.60157, 51.60157, 52.6888, 52.6888, 52.6888, 
    52.6888, 50.697802, 50.697802, 50.697802, 50.697802, 53.62417, 
    50.446841, 50.446841, 53.959679, 53.959679, 53.959679, 53.959679, 
    51.78375, 51.78375, 51.78375, 51.78375, 51.456965, 51.456965, 
    51.456965, 51.456965, 51.3651, 51.3651, 51.3651, 51.3651, 
    52.01182, 52.01182, 52.01182, 52.01182, 50.114277, 50.114277, 
    51.43474, 51.43474, 51.10676, 51.10676, 51.10676, 51.10676, 
    50.435984, 50.435984, 50.435984, 50.435984, 51.78666, 51.78666, 
    52.441088, 52.441088, 52.552344, 49.259471, 49.259471, 49.259471, 
    49.259471, 51.746642, 51.746642, 51.746642, 51.746642, 52.2501, 
    52.2501, 52.2501, 52.2501, 52.423336, 52.423336, 52.423336, 
    52.423336, 53.615575, 53.615575, 53.615575, 53.615575, 51.08474, 
    51.08474, 51.08474, 53.19329, 53.19329, 53.19329, 53.19329, 
    55.96785, 55.96785, 56.52664, 56.52664, 56.52664, 56.52664, 
    51.8113, 51.8113, 51.8113, 51.8113, 52.580157, 52.580157, 
    52.580157, 52.580157, 50.52008, 50.52008, 50.52008, 50.52008, 
    51.48417, 51.48417, 51.48417, 51.48417, 54.58243, 54.58243, 
    54.58243, 54.58243, 52.58839, 52.58839, 52.58839, 52.58839, 
    52.717283, 52.717283, 52.717283, 52.717283, 50.740764, 50.740764, 
    50.740764, 50.740764, 52.57937, 52.57937, 52.57937, 52.57937, 
    50.736531, 50.736531, 50.79926, 50.79926, 50.79926, 53.675996, 
    53.675996, 51.36445, 51.36445, 51.36445, 51.36445, 52.122402, 
    52.122402, 52.122402, 52.16104, 52.16104, 55.91913, 51.6528, 
    51.6528, 51.6528, 51.6528, 51.88485, 51.88485, 51.88485, 
    51.88485, 52.34015, 52.34015, 52.34015, 52.026042, 52.026042, 
    52.026042, 52.026042, 51.319032, 51.319032, 51.319032, 51.319032, 
    51.51357, 51.51357, 51.51357, 51.51357, 53.43202, 53.43202, 
    53.43202, 53.43202, 51.50823, 51.50823, 51.50823, 51.50823
    ), Category_Index = c(85L, 85L, 85L, 75L, 45L, 25L, 75L, 
    65L, 75L, 75L, 95L, 95L, 95L, 95L, 95L, 65L, 85L, 65L, 95L, 
    85L, 85L, 85L, 75L, 75L, 65L, 85L, 85L, 75L, 75L, 85L, 65L, 
    95L, 85L, 95L, 95L, 75L, 75L, 85L, 85L, 85L, 85L, 85L, 75L, 
    85L, 85L, 85L, 85L, 75L, 75L, 85L, 85L, 65L, 75L, 85L, 75L, 
    95L, 95L, 95L, 95L, 75L, 65L, 95L, 95L, 55L, 75L, 65L, 75L, 
    65L, 95L, 75L, 95L, 65L, 75L, 75L, 85L, 85L, 65L, 95L, 65L, 
    65L, 65L, 65L, 65L, 65L, 85L, 85L, 75L, 95L, 85L, 85L, 75L, 
    45L, 55L, 35L, 35L, 25L, 25L, 95L, 85L, 75L, 85L, 85L, 75L, 
    75L, 65L, 75L, 85L, 65L, 45L, 95L, 95L, 95L, 95L, 65L, 75L, 
    45L, 35L, 75L, 95L, 95L, 85L, 75L, 65L, 85L, 95L, 75L, 85L, 
    85L, 95L, 65L, 65L, 45L, 65L, 85L, 35L, 95L, 85L, 85L, 85L, 
    85L, 65L, 55L, 75L, 85L, 85L, 95L, 85L, 75L, 75L, 85L, 65L, 
    45L, 75L, 75L, 65L, 65L, 75L, 65L, 95L, 95L, 95L, 85L, 65L, 
    75L, 75L, 75L, 65L, 75L, 35L, 75L, 75L, 75L, 75L, 25L, 45L, 
    45L, 35L, 85L, 95L, 85L, 95L, 85L, 85L, 85L, 75L, 45L, 25L, 
    75L, 65L, 75L, 75L, 95L, 95L, 95L, 95L, 95L, 65L, 85L, 65L, 
    95L, 85L, 85L, 85L, 75L, 75L, 65L, 85L, 85L, 75L, 75L, 85L, 
    65L, 95L, 85L, 95L, 95L, 75L, 75L, 85L, 85L, 85L, 85L, 85L, 
    75L, 85L, 85L, 85L, 85L, 75L, 75L, 85L, 85L, 65L, 75L, 85L, 
    75L, 95L, 95L, 95L, 95L, 75L, 65L, 95L, 95L, 55L, 75L, 65L, 
    75L, 65L, 95L, 75L, 95L, 65L, 75L, 75L, 85L, 85L, 65L, 95L, 
    65L, 65L, 65L, 65L, 65L, 65L, 85L, 85L, 75L, 95L, 85L, 85L, 
    75L, 45L, 55L, 35L, 35L, 25L, 25L, 95L, 85L, 75L, 85L, 85L, 
    75L, 75L, 65L, 75L, 85L, 65L, 45L, 95L, 95L, 95L, 95L, 65L, 
    75L, 45L, 35L, 75L, 95L, 95L, 85L, 75L, 65L, 85L, 95L, 75L, 
    85L, 85L, 95L, 65L, 65L, 45L, 65L, 85L, 35L, 95L, 85L, 85L, 
    85L, 85L, 65L, 55L, 75L, 85L, 85L, 95L, 85L, 75L, 75L, 85L, 
    65L, 45L, 75L, 75L, 65L, 65L, 75L, 65L, 95L, 95L, 95L, 85L, 
    65L, 75L, 75L, 75L, 65L, 75L, 35L, 75L, 75L, 75L, 75L, 25L, 
    45L, 45L, 35L, 85L, 95L, 85L, 95L, 85L, 85L, 85L, 75L, 45L, 
    25L, 75L, 65L, 75L, 75L, 95L, 95L, 95L, 95L, 95L, 65L, 85L, 
    65L, 95L, 85L, 85L, 85L, 75L, 75L, 65L, 85L, 85L, 75L, 75L, 
    85L, 65L, 95L, 85L, 95L, 95L, 75L, 75L, 85L, 85L, 85L, 85L, 
    85L, 75L, 85L, 85L, 85L, 85L, 75L, 75L, 85L, 85L, 65L, 75L, 
    85L, 75L, 95L, 95L, 95L, 95L, 75L, 65L, 95L, 95L, 55L, 75L, 
    65L, 75L, 65L, 95L, 75L, 95L, 65L, 75L, 75L, 85L, 85L, 65L, 
    95L, 65L, 65L, 65L, 65L, 65L, 65L, 85L, 85L, 75L, 95L, 85L, 
    85L, 75L, 45L, 55L, 35L, 35L, 25L, 25L, 95L, 85L, 75L, 85L, 
    85L, 75L, 75L, 65L, 75L, 85L, 65L, 45L, 95L, 95L, 95L, 95L, 
    65L, 75L, 45L, 35L, 75L, 95L, 95L, 85L, 75L, 65L, 85L, 95L, 
    75L, 85L, 85L, 95L, 65L, 65L, 45L, 65L, 85L, 35L, 95L, 85L, 
    85L, 85L, 85L, 65L, 55L, 75L, 85L, 85L, 95L, 85L, 75L, 75L, 
    85L, 65L, 45L, 75L, 75L, 65L, 65L, 75L, 65L, 95L, 95L, 95L, 
    85L, 65L, 75L, 75L, 75L, 65L, 75L, 35L, 75L, 75L, 75L, 75L, 
    25L, 45L, 45L, 35L, 85L, 95L, 85L, 95L), Key_Parameter_Category_Values = structure(c(3L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 2L, 2L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 
    4L, 4L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
    2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 4L, 
    4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 3L, 
    3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L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 4L, 4L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 4L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L, 3L, 3L, 
    4L, 3L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 1L, 4L, 2L, 4L, 3L, 3L, 
    3L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 3L, 4L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L, 2L), .Label = c("1", "2", "3", "4"), class = "factor")), class = "data.frame", row.names = c(NA, 
-543L))
Alice Hobbs
  • 1,021
  • 1
  • 15
  • 31
  • you need to set `breaks` not `limits` to go up in 5's in `scale_y_continuous` – dww Mar 10 '19 at 15:12
  • 1
    Possible duplicate of [Increase number of axis ticks](https://stackoverflow.com/questions/11335836/increase-number-of-axis-ticks) – dww Mar 10 '19 at 15:14
  • Hi dww. Thank you for answering my post once again, it is deeply appreciated. I am fairly new to producing complex plots so this is a steep learning curve but good. My deadline is in 2 days, and I have been working on these plots for 4-5 days. I re-edited the code above like you suggested (adding breaks and limits). Unfortunately, I was not successful. I also created an object called Canopy_Scale_ylabel' with the correct scale. If this is possible, I would be especially grateful for any suggestions. Many thanks in advance. – Alice Hobbs Mar 10 '19 at 16:20
  • I produced some lovely error messages, which I incorporated into the question above. Am I on the right track? Many thanks if you can advise. – Alice Hobbs Mar 10 '19 at 16:36
  • This really is a duplicate. E.g. see [here](https://stackoverflow.com/questions/46039176/ggplot2-adding-secondary-y-axis-with-different-breaks-and-labels) and use the advice there. In addition to checking for duplicates before you post (the above link is the first hit if you google "ggplot secondary axis breaks"), you should construct minimal examples for your questions here. Lots of data and code that are irrelevant to the actual problem should be eliminated before you post, otherwise it wastes people's time to wade through all the extra stuff. – dww Mar 10 '19 at 18:35
  • Thanks for the hint. I followed the advice on the page you suggested and this time, I manually produced a secondary y-axis (wrong scale 0.5) using the breaks=seq() function; however, the scale starts at 54.0 and ends at 61.5. I am having trouble figuring out the correct transformation equation and break sequence values to produce a y-axis scale from 0-100,k by 5.0. I have done a lot of research on Google throughout this whole process and I did not know I needed to use breaks=seq() until I received your advice. – Alice Hobbs Mar 10 '19 at 20:30
  • secondary axes can only be a linear transformation of the primary axis in ggplot. So you set the limits on the primary axis and the breaks in the secondary axis. As in the linked answer. – dww Mar 10 '19 at 20:32
  • For example `p11 + scale_y_continuous(sec.axis = sec_axis(~.+1, name = "Canopy Index %", breaks = seq(0,100,5)), limits = c(0,100))` does what you asked for. But I'm not convinced it makes any sense. I find it hard to believe that canopy structure is a linear function of latitude, so it probably should not be presented as a secondary y axis – dww Mar 10 '19 at 20:40
  • Thank you dww.. Please be patient with me, I am relatively new to R, Funnily enough, I have already written that code, which is great because I am on the right track. The reason why I continued with my plight is because the scale for the lefthand y-axis for latitude has changed in conjunction with the righthand secondary y-axis, when it's important that the lefthand y-axis stays the same as the original. scale My aim is to upload a secondary data set to plot against the secondary y-axis. Now the boxes are squashed again and you cannot see them. Thank you again – Alice Hobbs Mar 10 '19 at 20:45
  • You can't do that in ggplot. The primary and secondary axes are inextricably linked, so you can't make them fully independent and have different data relate to different axes. This was an explicit decision by the package author, on the basis that it can be is misleading if people read the wrong points against the wrong axis, and should therefore be discouraged. If you are nonetheless determined, you should probably use something other than ggplot. Although I would encourage you to rethink your plot and put the separate data on two side-by-side panels. – dww Mar 10 '19 at 20:55
  • Or - if the point of the plot is to show that there is a relationship between latitude and canopy structure, then you should plot latitude vs canopy – dww Mar 10 '19 at 20:57
  • My goodness. So, I have been going crazy for days over a problem that cannot be done. I am really pleased to know this now. Do you mean like these panel plots produced with facet_grid() https://ggplot2.tidyverse.org/reference/facet_grid.html – Alice Hobbs Mar 10 '19 at 21:03
  • Problem: we have to link Canopy Cover with Latitude and the categories of the key parameters into a plot. That's why I chose side by side boxplots. – Alice Hobbs Mar 10 '19 at 21:04
  • https://blog.datawrapper.de/dualaxis/ – dww Mar 10 '19 at 21:20
  • Thank you for the tips. Back to the drawing board. Panel plots here I come. Best wishes and take care – Alice Hobbs Mar 11 '19 at 06:02

0 Answers0