I have a quartodocument containing several ggplot-plots. When I render the document all but one works just fine.
The one that does not render looks fine in RStudio, no error messages, but the html-file only contains the background colour, title and xlab-text. The actual bars are not showing.
I created a repex for this question but failed, because with my example data the plot renders as it should! The repex dataset had the same two variables, same possible values, the same class, and included random NAs. So now my question is in two parts: why does this particular plot not work, and if someone could give me a suggestion how to produce a repex?
This is the code for my plot:
n_aktiviteter <- LHU_act %>%
drop_na() %>%
count(KM7_8, Aktivitet_sum) %>%
group_by(KM7_8) %>%
mutate(prop = prop.table(n)) %>%
ungroup() %>%
mutate(KM7_8 = case_when(
KM7_8 == 0 ~ "Does not reach limit",
KM7_8 == 1 ~ "Reaches limit"))
ggplot(n_aktiviteter,
aes(group = KM7_8, y = prop, x = as.factor(Aktivitet_sum),
fill = KM7_8)) +
geom_col(position = "dodge") +
scale_fill_manual(values = c("#339D94", "#670F3B")) +
scale_y_continuous(labels = scales::percent) +
ggtitle("Number of activities grouped by level of PA") +
xlab("Number of activities") +
ylab("") +
theme(legend.title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
I have tried to save the ggplot and use print(plot1)
and show(plot1)
but that did not work. The file is originally imported from SPSS with attributes, I removed them using
LHU_act <- LHU_act %>% mutate(across(everything(), as.integer))`
, but that did not do any difference either.
This is the ouput from my original dataset using str(LHU_act):
tibble [6,848 × 2] (S3: tbl_df/tbl/data.frame)
$ KM7_8 : int [1:6848] 0 1 0 0 NA 0 0 NA 0 1 …
$ Aktivitet_sum: num [1:6848] 4 5 4 3 NA 2 1 NA 3 4 …
And the one I created as repex looks like this:
tibble [6,800 × 2] (S3: tbl_df/tbl/data.frame)
$ Aktivitet_sum: num [1:6800] 5 2 5 0 0 4 4 1 0 5 …
$ KM7_8 : int [1:6800] 0 1 0 1 0 1 0 1 0 1 …
Lastly I tried not removing the NA’s, and then the plot semi-works, it shows the NA-bar but nothing else!