How to get rid of that NA column? Is there a better way to deal with missing values in this case, than drop_na
that I used?
My code:
plot2 <- plot2_longer %>%
mutate(
variable = recode(
variable,
"item28" = "item28",
"item29" = "item29",
"item30" = "item30",
"item31" = "item31",
"item32" = "item32",
"item33" = "item33",
"item34" = "item34")) %>%
#drop_na() %>% #writing this out produces the NA colum
mutate(value = recode(value, "1" = "option 1", "2" = "option 2", "3" = "option 3",
"4" = "option 4", "5" = "option 5", "6" = "option 6",
"7" = "option 7")) %>%
count(variable, value) %>% # tai |>
group_by(variable) %>% # tai |>
mutate(pct = n / sum(n))
plot2$value <- factor(plot2$value,
levels = plot2$value[order(plot2$pct, decreasing = FALSE)])
ggplot(plot2, mapping = aes(x = value, y = pct))
geom_col(aes(fill = value),
width = 0.30
)
scale_y_continuous(
breaks = seq(from = 0, to = 1, by = 0.2),
minor_breaks = seq(from = 0, to = 1, by = 0.1),
labels = seq(from = 0, to = 100, by = 20),
limits = c(0, 1),
expand = c(0, 0)
)
scale_fill_manual(
values = c('option 1' = "blue",
'option 2' = "blue",
'option 3' = "blue",
'option 4' = "blue",
'option 5' = "blue",
'option 6' = "blue",
'option 7' = "blue"),
drop = FALSE
)
labs(x = "", y = "%")
guides(
fill = "none"
)
theme(
plot.margin = margin(t = 50, r = 30, b = 20, l = 5.5),
plot.title = element_text(size = 20, face = "bold"),
panel.background = element_blank(),
panel.grid.major = element_line(colour = "grey"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_line(colour = "lightgrey"),
axis.ticks.x = element_blank(),
axis.ticks.y = element_line(colour = "grey"),
axis.text.y = element_text(
size = 13,
face = "bold",
hjust = 0
),
axis.text.x = element_text(
size = 13,
face = "bold",
hjust = 0
),
axis.title.x = element_text(size = 20),
)
coord_flip()
ggtitle("Title")
My data:
structure(list(variable = c("item28", "item28", "item29", "item29",
"item30", "item30", "item31", "item31", "item32", "item32", "item33",
"item33", "item34", "item34"), value = structure(c(2L, NA, 5L,
NA, 6L, NA, 7L, NA, 3L, NA, 4L, NA, 1L, NA), .Label = c("option 7",
"option 1", "option 5", "option 6", "option 2", "option 3", "option 4"
), class = c("ordered", "factor")), n = c(38L, 109L, 110L, 37L,
111L, 36L, 121L, 26L, 45L, 102L, 70L, 77L, 19L, 128L), pct = c(0.258503401360544,
0.741496598639456, 0.748299319727891, 0.251700680272109, 0.755102040816326,
0.244897959183673, 0.82312925170068, 0.17687074829932, 0.306122448979592,
0.693877551020408, 0.476190476190476, 0.523809523809524, 0.129251700680272,
0.870748299319728)), row.names = c(NA, -14L), groups = structure(list(
variable = c("item28", "item29", "item30", "item31", "item32",
"item33", "item34"), .rows = structure(list(1:2, 3:4, 5:6,
7:8, 9:10, 11:12, 13:14), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Longer data:
structure(list(variable = c("item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34", "item28", "item29", "item30", "item31",
"item32", "item33", "item34"), value = structure(c(NA, 2L, 3L,
4L, NA, NA, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, NA, NA, 4L, NA,
NA, NA, NA, NA, NA, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
NA, 2L, 3L, 4L, NA, NA, NA, NA, 2L, 3L, 4L, NA, 6L, NA, NA, 2L,
NA, 4L, NA, 6L, NA, NA, NA, 3L, NA, NA, NA, NA, NA, NA, 3L, 4L,
NA, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, 1L, 2L, 3L, NA, NA, 6L,
7L, NA, NA, 3L, 4L, NA, 6L, NA, 1L, NA, 3L, NA, NA, NA, NA, NA,
2L, 3L, 4L, 5L, NA, 7L, NA, 2L, 3L, NA, NA, NA, NA, NA, NA, 3L,
4L, NA, NA, NA, NA, 2L, 3L, NA, NA, NA, NA, 1L, 2L, 3L, 4L, 5L,
6L, 7L, NA, 2L, NA, NA, NA, 6L, NA, NA, NA, NA, 4L, 5L, NA, NA,
NA, NA, NA, 4L, NA, NA, NA, NA, 2L, 3L, NA, NA, NA, NA, NA, 2L,
NA, 4L, NA, 6L, NA, NA, NA, 3L, 4L, NA, NA, NA, NA, 2L, 3L, 4L,
5L, NA, 7L, 1L, 2L, 3L, 4L, NA, 6L, NA, NA, NA, NA, 4L, NA, NA,
NA, NA, NA, 3L, 4L, NA, NA, NA, NA, 2L, 3L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 2L, 3L, 4L, NA, 6L, 7L, NA, NA, 3L,
4L, NA, NA, 7L, NA, 2L, NA, NA, NA, 6L, NA, 1L, 2L, 3L, 4L, 5L,
6L, NA, NA, 2L, 3L, 4L, NA, 6L, NA, NA, NA, NA, 4L, NA, 6L, NA,
1L, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, NA,
3L, NA, NA, NA, NA, NA, 2L, NA, 4L, 5L, NA, NA, NA, NA, 3L, 4L,
5L, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, 2L, 3L, NA, NA, NA,
NA, 1L, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA,
2L, 3L, 4L, 5L, 6L, NA, 1L, 2L, 3L, 4L, 5L, 6L, NA, NA, 2L, NA,
4L, NA, 6L, NA, 1L, NA, 3L, 4L, 5L, 6L, NA, NA, 2L, 3L, 4L, 5L,
6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, 2L, 3L, 4L, NA, NA, NA,
1L, 2L, 3L, 4L, NA, NA, 7L, NA, 2L, 3L, 4L, NA, NA, NA, NA, 2L,
NA, 4L, NA, 6L, NA, 1L, 2L, 3L, 4L, 5L, NA, 7L, 1L, 2L, 3L, 4L,
NA, NA, NA, NA, 2L, NA, 4L, 5L, NA, NA, NA, 2L, 3L, NA, 5L, 6L,
NA, NA, 2L, 3L, 4L, 5L, NA, NA, NA, 2L, 3L, 4L, NA, 6L, NA, NA,
2L, 3L, NA, NA, NA, 7L, NA, NA, NA, 4L, 5L, 6L, NA, NA, 2L, 3L,
NA, NA, NA, NA, NA, 2L, NA, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, NA,
6L, 7L, 1L, 2L, 3L, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, NA, NA, 7L,
NA, NA, 3L, 4L, NA, 6L, NA, 1L, NA, 3L, 4L, 5L, 6L, NA, NA, NA,
NA, 4L, NA, NA, NA, NA, 2L, NA, 4L, 5L, 6L, NA, 1L, NA, 3L, 4L,
NA, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, 2L, 3L, NA, 5L, 6L,
NA, NA, NA, 3L, NA, NA, NA, NA, NA, 2L, 3L, 4L, NA, 6L, NA, 1L,
2L, 3L, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, NA, NA, 2L, 3L,
4L, NA, 6L, NA, NA, NA, 3L, 4L, NA, 6L, NA, 1L, 2L, 3L, 4L, 5L,
6L, NA, NA, NA, 3L, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, NA,
NA, 2L, 3L, 4L, 5L, 6L, 7L, NA, 2L, NA, 4L, NA, NA, NA, NA, 2L,
NA, 4L, NA, NA, NA, NA, 2L, 3L, 4L, 5L, NA, NA, NA, NA, 3L, NA,
NA, 6L, NA, NA, 2L, 3L, 4L, NA, NA, 7L, 1L, 2L, NA, NA, NA, 6L,
NA, NA, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, 3L, 4L, 5L, 6L, 7L, NA,
2L, 3L, 4L, 5L, 6L, NA, NA, 2L, 3L, 4L, NA, 6L, NA, 1L, NA, 3L,
4L, NA, NA, NA, 1L, 2L, 3L, NA, 5L, NA, NA, 1L, 2L, 3L, 4L, 5L,
6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA,
1L, 2L, 3L, 4L, NA, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, NA, NA, NA,
NA, 4L, NA, NA, NA, NA, 2L, 3L, 4L, 5L, NA, NA, NA, 2L, 3L, 4L,
NA, NA, NA, NA, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, NA, 4L, 5L, NA,
NA, NA, NA, NA, 4L, 5L, 6L, NA, NA, 2L, 3L, 4L, NA, 6L, NA, NA,
2L, 3L, 4L, 5L, 6L, NA, NA, 2L, NA, 4L, NA, NA, NA, NA, NA, NA,
4L, NA, 6L, NA, NA, 2L, NA, 4L, NA, NA, NA, NA, 2L, 3L, 4L, 5L,
NA, NA, NA, 2L, 3L, 4L, NA, NA, NA, NA, 2L, 3L, NA, 5L, 6L, NA,
NA, 2L, 3L, 4L, NA, NA, NA, 1L, 2L, 3L, NA, NA, NA, NA, NA, 2L,
3L, 4L, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1L, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, 3L, 4L, NA, 6L,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 3L, NA, 5L, NA, NA, 1L,
2L, NA, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, NA,
4L, NA, 6L, NA, 1L, 2L, 3L, 4L, 5L, 6L, NA, 1L, 2L, 3L, 4L, NA,
6L, NA, NA, 2L, 3L, 4L, 5L, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA,
NA, NA, 3L, 4L, 5L, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, 1L, 2L,
NA, 4L, 5L, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, NA, NA, 2L, NA, 4L,
NA, 6L, NA, NA, NA, 3L, 4L, 5L, NA, 7L, 1L, 2L, 3L, 4L, NA, 6L,
7L, NA, NA, 3L, 4L, NA, NA, NA, NA, 2L, 3L, 4L, 5L, 6L, NA, NA,
2L, 3L, 4L, NA, NA, 7L, NA, 2L, 3L, 4L, NA, 6L, NA, NA, 2L, 3L,
4L, NA, 6L, NA, NA, 2L, 3L, 4L, NA, NA, NA, 1L, 2L, 3L, 4L, 5L,
6L, NA), .Label = c("1", "2", "3", "4", "5", "6", "7"), class = c("ordered",
"factor"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-1029L))
CodePudding user response:
Just pass remove_missing(plot2)
instead of plot2
to ggplot
ggplot(remove_missing(plot2), mapping = aes(pct, value))
geom_col(fill = '#97afc7', width = 0.30)
scale_x_continuous(breaks = 0:5/5, minor_breaks = 0:10/10,
labels = ~.x*100, limits = c(0, 1), expand = c(0, 0))
labs(x = "%", y = NULL, title = "Title")
theme(plot.margin = margin(t = 50, r = 30, b = 20, l = 5.5),
plot.title = element_text(size = 20, face = "bold"),
panel.background = element_blank(),
panel.grid.major = element_line(colour = "grey"),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_line(colour = "lightgrey"),
axis.ticks.y = element_blank(),
axis.ticks.x = element_line(colour = "grey"),
axis.text.x = element_text(size = 13, face = "bold", hjust = 0),
axis.text.y = element_text(size = 13, face = "bold", hjust = 0),
axis.title.y = element_text(size = 20))