我正在用图表绘制一些份额。我想将标签放在每个分数的中间,但是即使我使用position_stack
,它也只能为我提供第一张图(Alberta)所需的标签,而其余部分则没有。
df$label <- scales::percent(df$fuel.share)
df$label[df$fuel.share < 0.05 ] <- NA
ggplot(data = df) +
geom_bar( aes(factor(1),y=fuel.share,fill=Fuel),stat="identity") +
geom_text(aes(factor(1) ,y=fuel.share, label =label )
, size =2
,position = position_stack(vjust = 0.5)) +
coord_polar(theta="y") +
facet_wrap(prov~.) +
scale_x_discrete(name=" ", breaks = NULL) +
scale_y_continuous(name=" ", breaks = NULL)
这是数据集:
df<- structure(list(Fuel = c("Natural.Gas", "Gasoline", "Diesel",
"Light.Fuel.Oil", "Kerosene", "Heavy.Fuel.Oil", "Propane", "Natural.Gas",
"Gasoline", "Diesel", "Light.Fuel.Oil", "Kerosene", "Heavy.Fuel.Oil",
"Propane", "Natural.Gas", "Gasoline", "Diesel", "Light.Fuel.Oil",
"Kerosene", "Heavy.Fuel.Oil", "Propane", "Natural.Gas", "Gasoline",
"Diesel", "Light.Fuel.Oil", "Kerosene", "Heavy.Fuel.Oil", "Propane"
), prov = c("Quebec", "Quebec", "Quebec", "Quebec", "Quebec",
"Quebec", "Quebec", "Ontario", "Ontario", "Ontario", "Ontario",
"Ontario", "Ontario", "Ontario", "Alberta", "Alberta", "Alberta",
"Alberta", "Alberta", "Alberta", "Alberta", "Saskatchewan", "Saskatchewan",
"Saskatchewan", "Saskatchewan", "Saskatchewan", "Saskatchewan",
"Saskatchewan"), year = c(2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016,
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016
), energy.fuel = c(1.149, 4.68, 15.58, 0.357, 0.105, 0.183, 5.058,
22.103, 8.883, 15.142, 1.76, 0.402, 0.019, 4.339, 4.411, 17.441,
34.206, 0.015, 0.001, 0, 0.285, 2.624, 16.933, 49.532, 0.026,
0.016, 8.718, 0.065), fuel.share = c(0.04237975804072, 0.172617291236353,
0.574653290056064, 0.0131676010622603, 0.00387282384184125, 0.00674977869578047,
0.186559457066981, 0.419826014283544, 0.168724358000304, 0.287608266220939,
0.0334295699741681, 0.00763561768728157, 0.000360887403130223,
0.0824152864306336, 0.078266115438528, 0.309462552564808, 0.606930570095282,
0.00026615092531805, 1.77433950212034e-05, 0, 0.00505686758104296,
0.0336781579690428, 0.217329363143979, 0.635726570321123, 0.000333701260363991,
0.000205354621762456, 0.111892599532818, 0.000834253150909978
), label = c(NA, "17.3%", "57.5%", NA, NA, NA, "18.7%", "42.0%",
"16.9%", "28.8%", NA, NA, NA, "8.2%", "7.8%", "30.9%", "60.7%",
NA, NA, NA, NA, NA, "21.7%", "63.6%", NA, NA, "11.2%", NA)), row.names = c(NA,
-28L), vars = c("prov", "year"), drop = TRUE, indices = list(
14:20, 7:13, 0:6, 21:27), group_sizes = c(7L, 7L, 7L, 7L), biggest_group_size = 7L,
labels = structure(list(
prov = c("Alberta", "Ontario", "Quebec", "Saskatchewan"),
year = c(2016, 2016, 2016, 2016)), class = "data.frame", row.names = c(NA,
-4L), vars = c("prov", "year"), drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
答案 0 :(得分:2)
我认为问题在于geom_text
层不太了解要堆叠的内容,尤其是不知道要堆叠的 order 。这是因为Fuel
列确定了条形图的堆叠顺序,但在geom_text
层上根本没有映射。
最简单的解决方法是将美观贴图移动到ggplot()
而不是单个图层中。这样,它将被后续层继承(也避免了在每一层中重复x
和y
美学)。借助ggplot
的魔力,即使geom_text
不使用fill
映射进行填充,它仍然会知道根据该位置的审美分组/排序。
我已经进行了更改,将文本层的position_stack
更改为position_fill
,并用geom_bar(stat = "identity")
替换了geom_col
,这是该成语的首选方法。
ggplot(data = df, aes(factor(1), y = fuel.share, fill = Fuel)) +
geom_col(position = "fill") +
geom_text(aes(label = label)
, size =2
,position = position_fill(vjust = 0.5)) +
coord_polar(theta = "y") +
facet_wrap(prov ~ .) +
scale_x_discrete(name = "", breaks = NULL) +
scale_y_continuous(name = "", breaks = NULL)