0

There are a couple of other posts on the same topic, but the solutions proposed do not fit my case. Given a data frame like this

    ddff <- structure(
  list(
    SampleID = structure(
      20:16,
      .Label = c(
        "S39",
        "S30",
        "S35",
        "S22",
        "S23",
        "S26",
        "S29",
        "S24",
        "S27",
        "S32",
        "S37",
        "S36",
        "S38",
        "S34",
        "S33",
        "S40",
        "S25",
        "S28",
        "S31",
        "S21"
      ),
      class = "factor"
    ),
    Counts = c(12177, 14367, 15118, 15312,
               16622),
    sampleName = structure(
      20:16,
      .Label = c(
        "2Dr",
        "2Es",
        "1Er",
        "1Bs",
        "1Cs",
        "2As",
        "2Ds",
        "1Ds",
        "2Bs",
        "1Br",
        "2Br",
        "2Ar",
        "2Cr",
        "1Dr",
        "1Cr",
        "2Er",
        "1Es",
        "2Cs",
        "1Ar",
        "1As"
      ),
      class = "factor"
    ),
    compartment = c("soil", "root", "soil",
                    "soil", "root")
  ),
  row.names = c(NA, 5L),
  class = "data.frame"
)

and the following code

library(tidyverse)    
ddff %>%
      ggplot(aes(x = Counts, y = SampleID)) +
      geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
      geom_text(
        aes(label = paste0("   ", Counts)),
        size = 3,
        hjust = 0,
        nudge_x = -0.1,
        check_overlap = TRUE,
        color = "blue"
      ) +
      xlim(NA, 33000) +
      theme_light() +
      theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1) +
      theme(# remove the vertical grid lines
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
      labs(title = "Library Size Overview",
           x = "Read Counts",
           color = "Compartment") +
      theme(
        legend.position = c(.95, .95),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.box.background = element_rect(color = "black", size = 1)
      )

I get this plot Rplot I'm interested in adding second labels on the right Y axis, according to the content of the ddff column 'sampleName'. Of course the solution seems to be the usage of scale_y_discrete along with dup_axis, but I'm not able to figure out how...any idea based on recent ggplot evolutions?

SteoG5972
  • 45
  • 4
  • Not `dup_axis`, more so `sec_axis`, which is slightly different ... but I don't think `sec_axis` works natively with non-continuous data as easily, you need to fake it. See https://stackoverflow.com/q/45361904/3358272 for some one approach. – r2evans Oct 06 '21 at 22:45

2 Answers2

1

Discrete scales in ggplot2 don't support secondary scales (see related issue). The ggh4x package has a manual axis that can work around this limitation. (Disclaimer: I'm the author of ggh4x).

For you example, you could use it like this:

library(ggplot2)

# ddff <- structure(...) # omitted for brevity

ggplot(ddff, aes(x = Counts, y = SampleID)) +
  geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
  geom_text(
    aes(label = paste0("   ", Counts)),
    size = 3,
    hjust = 0,
    nudge_x = -0.1,
    check_overlap = TRUE,
    color = "blue"
  ) +
  xlim(NA, 33000) +
  guides(y.sec = ggh4x::guide_axis_manual(
    breaks = ddff$SampleID, labels = ddff$sampleName
  ))

However, you may need to deduplicate data in the axis if there are multiple observations per y-axis category (which isn't the case in the example).

teunbrand
  • 33,645
  • 4
  • 37
  • 63
1

This is a dupe of the ..., and adapting its solution to this is merely adding the scale_y_continuous and setting the y axis label. Unlike teunbrand's excellent answer (I'm a fan of teunbrand's packages/work), this is base ggplot2.

(This also alters the order, since we're now "counting" along the samples.)

ddff %>%
      ggplot(aes(x = Counts, y = seq_along(SampleID))) +
      geom_point(aes(col = compartment), size = 4, alpha = 0.75) +
      geom_text(
        aes(label = paste0("   ", Counts)),
        size = 3,
        hjust = 0,
        nudge_x = -0.1,
        check_overlap = TRUE,
        color = "blue"
      ) +
      xlim(NA, 33000) +
      theme_light() +
      theme(plot.title = element_text(hjust = 0.5), aspect.ratio = 1) +
      theme(# remove the vertical grid lines
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
      labs(title = "Library Size Overview",
           x = "Read Counts",
           y = "SampleID",                        # <-- new
           color = "Compartment") +
      theme(
        legend.position = c(.95, .95),
        legend.justification = c("right", "top"),
        legend.box.just = "right",
        legend.box.background = element_rect(color = "black", size = 1)
      ) +
      # new
      scale_y_continuous(
        breaks = seq_len(nrow(ddff)), labels = ddff$SampleID,
        sec.axis = sec_axis(~., breaks = seq_len(nrow(ddff)),
                            labels = ddff$sampleName)
      )

second axis with categorical data

r2evans
  • 141,215
  • 6
  • 77
  • 149