如何在没有单独的颜色键的情况下调整宽度并将标签添加到 X 轴
How to adjust width and add labels to X axis without a separete color key
我想使用 ggplot2 绘制分组条形图,并且我有多个方面,每个方面都有不同的测量值:
> dput(df)
structure(list(X = c("Low birthweight", "Low birthweight", "Low birthweight",
"Exclusive breast-feeding", "Exclusive breast-feeding", "Exclusive breast-feeding",
"Stunting (under 5 years)", "Stunting (under 5 years)", "Stunting (under 5 years)",
"Wasting (under 5 years)", "Wasting (under 5 years)", "Wasting (under 5 years)",
"Overweight (under 5 years)", "Overweight (under 5 years)", "Overweight (under 5 years)",
"Anaemia (Women of reproductive age)", "Anaemia (Women of reproductive age)",
"Anaemia (Women of reproductive age)", "Obesity (adults)", "Obesity (adults)"
), Year = c(2015, 2025, 2030, 2019, 2025, 2030, 2020, 2025, 2030,
2020, 2025, 2030, 2020, 2025, 2030, 2019, 2025, 2030, 2016, 2025
), Percentage = c(14.6, 10.5, 10.5, 44, 50, 70, 22, 15.4, 12.8,
6.7, 5, 3, 5.7, 5.6, 3, 29.9, 14.3, 14.3, 13.1, 11.8)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), spec = structure(list(
cols = list(X = structure(list(), class = c("collector_character",
"collector")), Year = structure(list(), class = c("collector_double",
"collector")), Percentage = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
我只想在数据可用的年份使用 X 轴,所以我将其保留为 scales="free"
但是我仍然无法为缺少数据的方面平均调整每个条形的宽度(年份)
我如何调整它并分别向 X 轴添加标签(对于每个带有年份的条形标签,而不是在单独的颜色键中)
p <- ggplot(data = df, aes(x = factor(X), y = Percentage,
fill = factor(Year)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
facet_grid(. ~ X, scales="free")
通过将 X
替换为 Year
,
df %>%
ggplot( aes(x = factor(Year), y = Percentage,
fill = factor(Year))) +
geom_bar(stat = "identity", position = position_dodge(0.9)) +
facet_grid(. ~ X, scales = "free_x", space = "free_x")
我想使用 ggplot2 绘制分组条形图,并且我有多个方面,每个方面都有不同的测量值:
> dput(df)
structure(list(X = c("Low birthweight", "Low birthweight", "Low birthweight",
"Exclusive breast-feeding", "Exclusive breast-feeding", "Exclusive breast-feeding",
"Stunting (under 5 years)", "Stunting (under 5 years)", "Stunting (under 5 years)",
"Wasting (under 5 years)", "Wasting (under 5 years)", "Wasting (under 5 years)",
"Overweight (under 5 years)", "Overweight (under 5 years)", "Overweight (under 5 years)",
"Anaemia (Women of reproductive age)", "Anaemia (Women of reproductive age)",
"Anaemia (Women of reproductive age)", "Obesity (adults)", "Obesity (adults)"
), Year = c(2015, 2025, 2030, 2019, 2025, 2030, 2020, 2025, 2030,
2020, 2025, 2030, 2020, 2025, 2030, 2019, 2025, 2030, 2016, 2025
), Percentage = c(14.6, 10.5, 10.5, 44, 50, 70, 22, 15.4, 12.8,
6.7, 5, 3, 5.7, 5.6, 3, 29.9, 14.3, 14.3, 13.1, 11.8)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L), spec = structure(list(
cols = list(X = structure(list(), class = c("collector_character",
"collector")), Year = structure(list(), class = c("collector_double",
"collector")), Percentage = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))
我只想在数据可用的年份使用 X 轴,所以我将其保留为 scales="free"
但是我仍然无法为缺少数据的方面平均调整每个条形的宽度(年份)
我如何调整它并分别向 X 轴添加标签(对于每个带有年份的条形标签,而不是在单独的颜色键中)
p <- ggplot(data = df, aes(x = factor(X), y = Percentage,
fill = factor(Year)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
facet_grid(. ~ X, scales="free")
通过将 X
替换为 Year
,
df %>%
ggplot( aes(x = factor(Year), y = Percentage,
fill = factor(Year))) +
geom_bar(stat = "identity", position = position_dodge(0.9)) +
facet_grid(. ~ X, scales = "free_x", space = "free_x")