How I can change the place of the variables that I want to analyze in my barplots?

Hello everyone, I hope you are very well. Does anyone know how I can change the place of the variables that I want to analyze in my barplots?

I would like to obtain something like the next picture

But Im obtaining this:


You don't see this result in my script because, I don't know how to put the row.names = 1, in my playable data.

In the example I initially added, they analyzed 4 months, that's why 4 colors are visible in the bars. In my case it is only 3 months.

Family <- tibble::tribble(
            ~index, ~Asimilatoria, ~Disimilatoria, ~Desnitrificacion,   ~Fijacion,   ~Comammox,    ~Anammox,
              "1A",   2723.254821,    38183.45592,       53573.76288,  13018.7107, 13277.83386, 77.08191835,
              "1B",   1388.970964,    12164.50113,       29296.87011, 3601.982925, 2418.416111, 219.5781374,
              "1C",   1697.256794,    25014.27221,        17943.5721, 5565.764586,  2378.42444, 98.45689751,
              "1D",    1845.90785,    30953.96123,       18818.37437, 6883.659478, 11078.79831,  150.176288,
              "1E",   7343.594562,    27610.96905,        20161.0688, 16017.18979, 5505.664447, 173.7012284,
              "1F",   12601.21572,    36581.21871,       62929.11297, 44124.90253, 9866.798625, 376.1351159,
              "1G",   1367.992019,     5390.31907,        22359.7753, 7475.004442, 1328.437404,  24.4173847,
              "1H",   3414.112887,    31348.28055,       61648.85288, 17314.86411, 9878.842193, 403.2418896,
              "2A",   10898.10015,    36601.98456,       104532.5823, 46718.93958, 7952.580015, 681.4778672,
              "2B",   4862.008643,    27000.10627,       76538.20808, 35129.54079, 6535.788905, 65.14895218,
              "2C",   4545.964001,    16558.75163,       96002.82159, 18598.65187, 4243.693325, 328.7336149,
              "2D",   2979.024445,    18341.55805,       22249.28264, 13289.69271, 5741.753271, 149.6220436,
              "2E",   7178.050624,    37096.56213,       47869.39277, 10838.61865, 7013.400444, 956.4160795,
              "2F",    2302.42041,    11167.73345,       29339.71993, 7972.507868, 2458.425863, 288.4324224,
              "2G",   4005.809027,    46495.49203,       71800.05373, 15902.22339, 7017.612085,  199.604044,
              "2H",   8273.885093,    24424.33344,       74871.48992, 20621.01368, 6034.751409, 232.5177404
            )


data<- Family

attach(Family)
rwnames <- index
data <- as.data.frame(data[,-1])
rownames(data) <- rwnames

Metadata<- read.csv("~/RSTUDIOPICRUST/Metadata.csv", row.names=1)

#this is the metadata but I don't know how to apply the fuction row.names=1 here. 
Metadata<- data.table::data.table(
                         index = c("1A",
                                   "1B","1C","1D","1E","1F","1G","1H","2A",
                                   "2B","2C","2D","2E","2F","2G","2H"),
                  Asimilatoria = c(2723.254821,1388.970964,1697.256794,1845.90785,
                                   7343.594562,12601.21572,1367.992019,
                                   3414.112887,10898.10015,4862.008643,4545.964001,
                                   2979.024445,7178.050624,2302.42041,
                                   4005.809027,8273.885093),
                 Disimilatoria = c(38183.45592,12164.50113,25014.27221,30953.96123,
                                   27610.96905,36581.21871,5390.31907,
                                   31348.28055,36601.98456,27000.10627,16558.75163,
                                   18341.55805,37096.56213,11167.73345,
                                   46495.49203,24424.33344),
              Desnitrificacion = c(53573.76288,29296.87011,17943.5721,18818.37437,
                                   20161.0688,62929.11297,22359.7753,
                                   61648.85288,104532.5823,76538.20808,96002.82159,
                                   22249.28264,47869.39277,29339.71993,
                                   71800.05373,74871.48992),
                      Fijacion = c(13018.7107,3601.982925,5565.764586,6883.659478,
                                   16017.18979,44124.90253,7475.004442,
                                   17314.86411,46718.93958,35129.54079,18598.65187,
                                   13289.69271,10838.61865,7972.507868,
                                   15902.22339,20621.01368),
                      Comammox = c(13277.83386,2418.416111,2378.42444,11078.79831,
                                   5505.664447,9866.798625,1328.437404,
                                   9878.842193,7952.580015,6535.788905,4243.693325,
                                   5741.753271,7013.400444,2458.425863,
                                   7017.612085,6034.751409),
                       Anammox = c(77.08191835,219.5781374,98.45689751,150.176288,
                                   173.7012284,376.1351159,24.4173847,
                                   403.2418896,681.4778672,65.14895218,328.7336149,
                                   149.6220436,956.4160795,288.4324224,
                                   199.604044,232.5177404)
            )


#SUBSET DE MONTH
Jul <- subset(data, Metadata$Month == "July", select = c(`Asimilatoria`:`Anammox`))
Aug <- subset(data, Metadata$Month == "August", select = c(`Asimilatoria`:`Anammox`))
Sep <- subset(data, Metadata$Month == "September", select = c(`Asimilatoria`:`Anammox`))

Metadata$Month <- factor(Metadata$Month,
                         levels = c("Jul", "Aug", "Sep"))

#July
Jul <- data.frame(Jul)
Jul_counts <- colSums(Jul)
Counts <- unname(Jul_counts)
Jul_counts <- data.frame(Jul_counts)
Jul_counts <- t(Jul_counts)
total <- sum(Counts)
rel_ab <- Jul_counts/total
Others <- rel_ab[,colMeans(rel_ab)<.00]
Others <- sum(Others)
rel_ab <- rel_ab[,colMeans(rel_ab)>=.00]
rel_ab <- data.frame(t(rel_ab), Others)
rel_ab_P <- t(rel_ab)
abundance <- c("abundance")
rel_ab_P <- data.frame(rel_ab_P)
write.csv(rel_ab_P, file = "~/RSTUDIOPICRUST/JUL.csv")
Jul <- read.csv("~/RSTUDIOPICRUST/JUL.csv")

#August
Aug <- data.frame(Aug)
Aug_counts <- colSums(Aug)
Counts <- unname(Aug_counts)
Aug_counts <- data.frame(Aug_counts)
Aug_counts <- t(Aug_counts)
total <- sum(Counts)
rel_ab <- Aug_counts/total
Others <- rel_ab[,colMeans(rel_ab)<.00]
Others <- sum(Others)
rel_ab <- rel_ab[,colMeans(rel_ab)>=.00]
rel_ab <- data.frame(t(rel_ab), Others)
rel_ab_P <- t(rel_ab)
abundance <- c("abundance")
rel_ab_P <- data.frame(rel_ab_P)
write.csv(rel_ab_P, file = "~/RSTUDIOPICRUST/AUG.csv")
Aug <- read.csv("~/RSTUDIOPICRUST/AUG.csv")

#September
Sep <- data.frame(Sep)
Sep_counts <- colSums(Sep)
Counts <- unname(Sep_counts)
Sep_counts <- data.frame(Sep_counts)
Sep_counts <- t(Sep_counts)
total <- sum(Counts)
rel_ab <- Sep_counts/total
Others <- rel_ab[,colMeans(rel_ab)<.00]
Others <- sum(Others)
rel_ab <- rel_ab[,colMeans(rel_ab)>=.00]
rel_ab <- data.frame(t(rel_ab), Others)
rel_ab_P <- t(rel_ab)
abundance <- c("abundance")
rel_ab_P <- data.frame(rel_ab_P)
write.csv(rel_ab_P, file = "~/RSTUDIOPICRUST/SEP.csv")
Sep <- read.csv("~/RSTUDIOPICRUST/SEP.csv")

Family_colors <- c(
  "#b988d5","#cbd588", "#88a5d5",
  "#673770","#D14285", "#652926", "#C84248", 
  "#8569D5", "#5E738F","#D1A33D", "#8A7C64", "#599861"
)


library(ggplot2)
library(scales)
ggplot() +geom_bar(aes(y = rel_ab_P*100, x= "Jul", fill = X), data = Jul,
                   stat="identity", width = .5)+ geom_bar(aes(y = rel_ab_P*100, x= "Aug", fill = X), data = Aug,
                                                          stat="identity",width=.5)+
  scale_x_discrete(
    labels = c("Jul", "Aug", "Sep"), 
    drop = FALSE
  ) +
  geom_bar(aes(y = rel_ab_P*100, x= "Sep", fill = X), data = Sep,
           stat="identity", width = .5)+
  theme_classic()+
  theme(legend.title = element_blank())+
  ylab("Relative Abundance >.01% \n")+
  xlab("Month")+
  scale_fill_manual(values = Family_colors)
#> Warning: Removed 7 rows containing missing values (position_stack).

#> Warning: Removed 7 rows containing missing values (position_stack).

#> Warning: Removed 7 rows containing missing values (position_stack).

Created on 2021-06-03 by the reprex package (v0.3.0)

Hi,

I had some trouble understanding your code and it seems that the data is not complete, i.e. I could not make the subsets due to lacking month-info. However, looking at your desired output and adding a dummy month column to your data, is this something you want to accomplish?

Family$month  <- rep(c("Jun", "Jul", "Aug", "Sep"), 4)   #add month col to df                              
#str(Family)                                                                                               
Family_colors <- c("#b988d5","#cbd588", "#88a5d5","#673770")  #when months are coloured                    
                                                                                                           
family_long  <- Family %>% pivot_longer(2:7, names_to = "treatment", values_to = "value") %>%  #make long  
        select(index, month, treatment, value)  #reorder columns                                           
                                                                                                           
family_long %>% group_by(month, treatment) %>%                                                             
        summarize(month_tot = sum(value, na.rm = TRUE)) %>%                                                
        ggplot(aes(x = as.factor(treatment), y = month_tot, fill = month)) + geom_col(position = "dodge") +
        xlab("Month")+                                                                                     
        scale_fill_manual(values = Family_colors) +                                                        
        theme_classic()                                                                                    
                                                                                                           
#plotting the treatment on the x-axis                                                                      
Family_colors <- c(                                                                                        
  "#b988d5","#cbd588", "#88a5d5",                                                                          
  "#673770","#D14285", "#652926", "#C84248",                                                               
  "#8569D5", "#5E738F","#D1A33D", "#8A7C64", "#599861"                                                     
)  #treatment colouring                                                                                    
                                                                                                           
family_long %>% group_by(month, treatment) %>%                                                             
        summarize(month_tot = sum(value, na.rm = TRUE)) %>%                                                
        ggplot(aes(x = as.factor(month), y = month_tot, fill = treatment)) + geom_col(position = "dodge") +
        xlab("treatment")+                                                                                 
        scale_fill_manual(values = Family_colors) +                                                        
        theme_classic()                                                                                    
                                                                                                           

Hope this helps you out in some way.

JW

This topic was automatically closed 21 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.