label x axis with one more categorical value

I would like to produce the graph as in this

Here is the data:



dat <- data.frame(Species = rep(rep(c("DC", "DP"), each = 2), 3),
                  ES = rep(c("CA", "CK", "HD"), each = 4),
                  W = rep(c("S", "D"), 6),
                  P = c(0.5, 0.77, 0.003, 0.43, 0.38, 0.68, 0.003, 0.26, 0.13, 0.05, 0.003, 0.04))

What I had achived is this...

dat %>% 
  ggplot(aes(x=Species, P*100, fill = Species)) + 
  scale_fill_manual(values = c("dodgerblue1", "springgreen3"), 
                    labels = c("Dreissena carinata", "Dreissena polymorpha"))+
  geom_col(position = position_dodge2(width = 0.9, reverse = TRUE))+
  facet_wrap(~ES, nrow = 1)+
  geom_text(aes(label = scales::percent(P, accuracy = 1)),
            position = position_dodge2(width = 1.9, reverse = F),
            vjust = - 0.5,
            hjust= 0.5,
            colour = "black",
            size = 2.5)+
  scale_y_continuous(limits = c(0, 100))+
  theme_gray()+
  labs(x = " ", 
       y = "Infection Prevalence %", 
       title = "Test #2020_001",
       subtitle = "Infection Prevalence at the Start (S) and During (D) the Test"
  ) +
  theme(plot.title = element_text(hjust = 0.5),
        axis.text.x = element_text(face="italic", size=0),
        axis.title.y = element_text(face="bold", colour="black", size = 10, family = "Helvetica"),
        strip.text.y = element_text(face = "bold"),
        strip.text = element_text(face= "italic", size = 9,  family = "Helvetica"))+
  theme(legend.position = "bottom",
        legend.title = element_text(size=0),
        legend.text = element_text(face= "italic", size = 10,  family = "Helvetica", margin = margin(r = 20, unit = "pt")),
        legend.spacing.x = unit(2, "mm"),
        legend.key.size = unit(5, "mm"),
        legend.box.spacing = unit(-5, "mm"),
        plot.title = element_text(face="bold", colour="black", size = 11, hjust = 0,  family = "Helvetica"),
        plot.subtitle = element_text(size = 9, hjust = 0,  family = "Helvetica"),
        axis.ticks = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.spacing = unit(5, "mm"))

What I would like to ask you is to place the labels "S" and "D" at the bottom of the bars and not to use photoshop anymore . :slight_smile:

Thank you in advance..

Hi, the first part of your code doesn't work. I suppose it should be this:

dat %>% 
  mutate(P = parse_number(P)) %>% # fixed this 
  ggplot(aes(W, P, fill = SP)) + # I assume Species is SP
  scale_fill_manual(values = c("dodgerblue1", "springgreen3"), 
                    labels = c("Dreissena carinata", "Dreissena polymorpha")) +
  geom_col(position = position_dodge2(width = 0.9, reverse = TRUE))+
  facet_wrap(~ES, nrow = 1)+
  geom_text(aes(label = scales::percent(P/100, accuracy = 1)), # divided here
            position = position_dodge2(width = 1.9, reverse = F),
            vjust = - 0.5,
            hjust= 0.5,
            colour = "black",
            size = 2.5)

Is this what you mean?

library(tidyverse)

dat <- data.frame(SP = rep(rep(c("DC", "DP"), each = 2), 3),
                  ES = rep(c("CA", "CK", "HD"), each = 4),
                  W = rep(c("S", "D"), 6),
                  P = c("50%", "77%", "0%", "43%", "38%", "68%", "0%", "26%", "13%", "5%", "0%", "4%"))

dat %>% 
    mutate(P = parse_number(P)) %>% # fixed this 
    ggplot(aes(SP, P, fill = SP)) + # I assume Species is SP
    scale_fill_manual(values = c("dodgerblue1", "springgreen3"), 
                      labels = c("Dreissena carinata", "Dreissena polymorpha")) +
    geom_col(position = position_dodge2(width = 0.9, reverse = TRUE))+
    facet_wrap(~ES, nrow = 1)+
    geom_text(aes(label = scales::percent(P/100, accuracy = 1)), # divided here
              position = position_dodge2(width = 1.9, reverse = T),
              vjust = - 0.5,
              hjust= 0.5,
              colour = "black",
              size = 2.5) +
    geom_text(aes(y = -3, label = W), position =  position_dodge2(width = 1.9, reverse = T) ) +
    theme(axis.text.x = element_blank(),
          axis.ticks.x = element_blank())

Created on 2022-02-09 by the reprex package (v2.0.1)

1 Like

Yes, I want. But I get this error using your instructions..Packages "readr"and "dplyr" have been installed.

<error/dplyr:::mutate_error>
Error in mutate_cols():
! Problem with mutate() column P.
:information_source: P = parse_number(P).
x could not find function "parse_number"
Caused by error in parse_number():
! could not find function "parse_number"
Backtrace:

  1. ├─dat %>% mutate(P = parse_number(P)) %>% ...
  2. ├─ggplot2::ggplot(., aes(SP, P, fill = SP))
  3. ├─dplyr::mutate(., P = parse_number(P))
  4. ├─dplyr:::mutate.data.frame(., P = parse_number(P))
  5. │ └─dplyr:::mutate_cols(.data, ..., caller_env = caller_env())
  6. │ ├─base::withCallingHandlers(...)
  7. │ └─mask$eval_all_mutate(quo)
  8. └─base::.handleSimpleError(...)
  9. └─dplyr h(simpleError(msg, call))
  10. └─rlang::abort(...)
    

Sorry, I had been used the old code. :confused: Now it is corrected. The value "P" is numerical..

You can use your own code and simply add the relevant part.

The examples we gave you had to deal with the malformed sample data you gave us (variable names doesn't even match those in your code) but you don't need those extra steps if your actual data is not malformed.

2 Likes

Thank you very much! YOu helped me a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.