Error in stacked bar graph

ggplot(matchedsaltfixed, aes(fill=P, y=DAMAGE, x=LineHPA )) +
geom_bar(position="fill", stat="identity") + scale_colour_viridis() + scale_y_continuous(labels = percent)+labs(y="% Leaves", x="Lines - Hpa")

I have made the above graph using the above code but I'm not sure as to why there is firstly an NA column and secondly errors in the middle part of Lines Pst-I3 and Pst-I4

any help would be greatly appreciated

I think one has to see your data to answer your question. The code seems fine to me, with the exception that you're category P seems to be of class numeric and not factor (judging from the legend).

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi everyone, sorted it out now, I just made the mistake of having an image on my excel sheet which produced an NA column and altering my data, and secondly making an error when creating tidy data.

However, I do wonder how I can dissect the graph into 4 sections (I presume using facet wrap but I am unsure how?), grouping by f1 and LineHPA. My dataframe is below:

F1 LineHPA P DAMAGE
f1mock m-1 1 2
f1mock m-1 2 21
f1mock m-1 3 151
f1mock m-1 4 49
f1mock m-2 1 0
f1mock m-2 2 16
f1mock m-2 3 151
f1mock m-2 4 62
f1mock m-3 1 6
f1mock m-3 2 30
f1mock m-3 3 163
f1mock m-3 4 42
f1mock m-4 1 8
f1mock m-4 2 14
f1mock m-4 3 147
f1mock m-4 4 40
f1PSTLOW Pst-I1 1 10
f1PSTLOW Pst-I1 2 49
f1PSTLOW Pst-I1 3 184
f1PSTLOW Pst-I1 4 40
f1PSTLOW Pst-I2 1 12
f1PSTLOW Pst-I2 2 50
f1PSTLOW Pst-I2 3 150
f1PSTLOW Pst-I2 4 29
f1PSTLOW Pst-I3 1 4
f1PSTLOW Pst-I3 2 33
f1PSTLOW Pst-I3 3 139
f1PSTLOW Pst-I3 4 32
f1PSTLOW Pst-I4 1 13
f1PSTLOW Pst-I4 2 36
f1PSTLOW Pst-I4 3 139
f1PSTLOW Pst-I4 4 41
f1PSTMED Pst-II1 1 10
f1PSTMED Pst-II1 2 29
f1PSTMED Pst-II1 3 156
f1PSTMED Pst-II1 4 37
f1PSTMED Pst-II2 1 0
f1PSTMED Pst-II2 2 11
f1PSTMED Pst-II2 3 40
f1PSTMED Pst-II2 4 14
f1PSTMED Pst-II3 1 2
f1PSTMED Pst-II3 2 21
f1PSTMED Pst-II3 3 61
f1PSTMED Pst-II3 4 17
f1PSTMED Pst-II4 1 2
f1PSTMED Pst-II4 2 41
f1PSTMED Pst-II4 3 132
f1PSTMED Pst-II4 4 43
f1PSTHIGH Pst-III1 1 20
f1PSTHIGH Pst-III1 2 40
f1PSTHIGH Pst-III1 3 147
f1PSTHIGH Pst-III1 4 22
f1PSTHIGH Pst-III2 1 21
f1PSTHIGH Pst-III2 2 62
f1PSTHIGH Pst-III2 3 124
f1PSTHIGH Pst-III2 4 38
f1PSTHIGH Pst-III3 1 5
f1PSTHIGH Pst-III3 2 50
f1PSTHIGH Pst-III3 3 189
f1PSTHIGH Pst-III3 4 40
f1PSTHIGH Pst-III4 1 16
f1PSTHIGH Pst-III4 2 52
f1PSTHIGH Pst-III4 3 158
f1PSTHIGH Pst-III4 4 30

Is this what you mean?

library(ggplot2)
library(viridis)
library(scales)

# Sample data on a copy/paste friendly format, replace with your actual data frame.
matchedsaltfixed <- data.frame(
  stringsAsFactors = FALSE,
                F1 = c("f1mock","f1mock","f1mock",
                       "f1mock","f1mock","f1mock","f1mock","f1mock","f1mock",
                       "f1mock","f1mock","f1mock","f1mock","f1mock",
                       "f1mock","f1mock","f1PSTLOW","f1PSTLOW","f1PSTLOW",
                       "f1PSTLOW","f1PSTLOW","f1PSTLOW","f1PSTLOW","f1PSTLOW",
                       "f1PSTLOW","f1PSTLOW","f1PSTLOW","f1PSTLOW","f1PSTLOW",
                       "f1PSTLOW","f1PSTLOW","f1PSTLOW","f1PSTMED",
                       "f1PSTMED","f1PSTMED","f1PSTMED","f1PSTMED","f1PSTMED",
                       "f1PSTMED","f1PSTMED","f1PSTMED","f1PSTMED","f1PSTMED",
                       "f1PSTMED","f1PSTMED","f1PSTMED","f1PSTMED","f1PSTMED",
                       "f1PSTHIGH","f1PSTHIGH","f1PSTHIGH","f1PSTHIGH",
                       "f1PSTHIGH","f1PSTHIGH","f1PSTHIGH","f1PSTHIGH",
                       "f1PSTHIGH","f1PSTHIGH","f1PSTHIGH","f1PSTHIGH","f1PSTHIGH",
                       "f1PSTHIGH","f1PSTHIGH","f1PSTHIGH"),
           LineHPA = c("m-1","m-1","m-1","m-1",
                       "m-2","m-2","m-2","m-2","m-3","m-3","m-3","m-3",
                       "m-4","m-4","m-4","m-4","Pst-I1","Pst-I1","Pst-I1",
                       "Pst-I1","Pst-I2","Pst-I2","Pst-I2","Pst-I2","Pst-I3",
                       "Pst-I3","Pst-I3","Pst-I3","Pst-I4","Pst-I4",
                       "Pst-I4","Pst-I4","Pst-II1","Pst-II1","Pst-II1","Pst-II1",
                       "Pst-II2","Pst-II2","Pst-II2","Pst-II2","Pst-I3",
                       "Pst-I3","Pst-I3","Pst-I3","Pst-II4","Pst-II4",
                       "Pst-II4","Pst-II4","Pst-III1","Pst-III1","Pst-III1",
                       "Pst-III1","Pst-III2","Pst-III2","Pst-III2","Pst-III2",
                       "Pst-III3","Pst-III3","Pst-III3","Pst-III3","Pst-III4",
                       "Pst-III4","Pst-III4","Pst-III4"),
                 P = c(1L,2L,3L,4L,1L,2L,3L,4L,
                       1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,
                       3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,
                       1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,
                       4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L),
            DAMAGE = c(2L,21L,151L,49L,0L,16L,
                       151L,62L,6L,30L,163L,42L,8L,14L,147L,40L,10L,49L,
                       184L,40L,12L,50L,150L,29L,4L,33L,139L,32L,13L,
                       36L,139L,41L,10L,29L,156L,37L,0L,11L,40L,14L,
                       2L,21L,61L,17L,2L,41L,132L,43L,20L,40L,147L,
                       22L,21L,62L,124L,38L,5L,50L,189L,40L,16L,52L,
                       158L,30L)
)

ggplot(matchedsaltfixed, aes(x = LineHPA, y = DAMAGE, fill = P)) +
    geom_col(position = "fill") +
    scale_colour_viridis() +
    scale_y_continuous(labels = percent) +
    labs(x = "Lines - Hpa", 
         y = "% Leaves") +
    facet_wrap(facets = vars(F1), scales = "free_x")

Created on 2021-02-20 by the reprex package (v1.0.0)

BTW, please notice the way I'm sharing the example with you, that would be a proper reproducible example as explained on the guide I linked for you before. Next time please make your questions providing a reprex like this one.

1 Like

Thanks this is great!

Just one thing, is there any way you can format it so they're all on the same line?

and yes I will remember to provide a reprex next time

Use facet_wrap's nrow parameter , setting it to one.