我正在尝试使用networkD3库构建桑基图。我有以下代码和数据:
# Plotting the Sankey network diagram
nodes = data.frame("name" =
c("Mamalian", # Node 0
"Avian", # Node 1
"Critical", # Node 2
"Critical-maintained", # Node 3
"Endangerd", #Node 4
"Endangered-maintained", #Node 5
"Not at risk")) # Node 6
links = as.data.frame(matrix(c(
0, 2, 16, # Note the values in the final column refer to the % of breeds in each risk category
0, 3, 2.2,
0, 4, 17.6,
0, 5, 7.0,
0, 6, 57.2,
1, 2, 23.9,
1, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 ggplot 中创建一个条形图,其中条形的宽度与变量相关联Cost$Sum.of.FS_P_Reduction_Kg。我正在使用该参数width=Sum.of.FS_P_Reduction_Kg根据变量设置条形的宽度。
我想向图表添加直接标签以标记每个条形,类似于下面记录的图像。我也在寻求添加与参数相对应的 x 轴标签width=Sum.of.FS_P_Reduction_Kg.任何帮助将不胜感激。我知道ggrepel但到目前为止还没有达到预期的效果。
我使用了以下代码:
# Plot the data
P1 <- ggplot(Cost,
aes(x = Row.Labels,
y = Average.of.Cost_Per_Kg_P_Removal.undiscounted..LOW_Oncost,
width = Average.of.FS_Annual_P_Reduction_Kg, label = Row.Labels)) +
geom_col(fill = "grey", colour = "black") +
geom_label_repel(
arrow = arrow(length = unit(0.03, "npc"), type = "closed", ends = "first"),
force = 10,
xlim = NA) +
facet_grid(~reorder(Row.Labels,
Average.of.Cost_Per_Kg_P_Removal.undiscounted..LOW_Oncost),
scales = "free_x", space = "free_x") +
labs(x = "Measure code and average P reduction (kg/P/yr)", …Run Code Online (Sandbox Code Playgroud)