
uj5u.com熱心網友回復:
問題是在最后geom_text添加組標簽時,您將長度為 4 的向量傳遞給hjust. lapply但是,由于您有 8 個組,您必須傳遞一個長度為 8 的向量。此外,我通過使用添加分段層簡化了您的代碼:
ggplot(data, aes(x = as.factor(id), y = value, fill = group)) # Note that id is a factor. If x is numeric, there is some space between the first bar
geom_bar(aes(x = as.factor(id), y = value, fill = group), stat = "identity", alpha = 0.5)
# Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
lapply(seq(8), function(x) {
geom_segment(data = grid_data, aes(x = end, y = 10 * x, xend = start, yend = 10 * x), colour = "grey", alpha = 1, size = 0.3, inherit.aes = FALSE)
})
# Add text showing the value of each 100/75/50/25 lines
annotate("text", x = rep(max(data$id), 8), y = c(10, 20, 30, 40, 50, 60, 70, 80), label = c("1", "2", "3", "4", "5", "6", "7", "8"), color = "grey", size = 3, angle = 0, fontface = "bold", hjust = 1)
geom_bar(aes(x = as.factor(id), y = value, fill = group), stat = "identity", alpha = 0.5)
ylim(-100, 120)
theme_minimal()
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1, 4), "cm")
)
coord_polar()
geom_text(data = label_data, aes(x = id, y = value 10, label = individual, hjust = hjust), color = "black", fontface = "bold", alpha = 0.6, size = 2.5, angle = label_data$angle, inherit.aes = FALSE)
# Add base line information
geom_segment(data = base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha = 0.8, size = 0.6, inherit.aes = FALSE)
geom_text(data = base_data, aes(x = title, y = -18, label = group), hjust = c(1, 1, 1, 0, 0, 0, 0, 0),
colour = "black", alpha = 0.8, size = 4, fontface = "bold", inherit.aes = FALSE)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/482171.html
