To avoid making a pie chart I want to create a barchart. I do however want to show the total for each catagory. Here's my dataset created with dput()
function:
df <- structure(list(Status = structure(c(4L, 6L, 1L, 5L, 2L, 3L), .Label = c("Compromise",
"Launched", "Not yet rated", "Promise broken", "Promise kept",
"Stuck"), class = "factor"), n = c(15L, 4L, 4L, 7L, 9L, 21L),
total = c("60", "60", "60", "60", "60", "60")), .Names = c("Status",
"n", "total"), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"))
Here's my ggplot2 code:
df %>%
ggplot(aes(x = Status, y = n, fill = Status)) +
geom_col() +
coord_flip() +
geom_text(aes(label = n),
hjust = 2,
colour = "white",
fontface = "bold",
size = 3) +
scale_x_discrete(limits = rev(order)) +
scale_fill_tableau() +
theme_minimal() +
theme(axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
legend.position = "null")
And here's an example from pew research center (obviously with totally different data) to get an idea of what I try to accomplish:
You've got the total
, you just need to convert it to numeric and add another geom_col
layer first.
df$total = as.numeric(as.character(df$total))
ggplot(df, aes(x = Status, y = n, fill = Status)) +
geom_col(aes(y = total), fill = "grey90") +
geom_col() +
coord_flip() +
geom_text(aes(label = n),
hjust = 2,
colour = "white",
fontface = "bold",
size = 3) +
scale_x_discrete(limits = rev(order)) +
scale_fill_tableau() +
theme_minimal() +
theme(axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
legend.position = "null")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.