小编War*_*ght的帖子

桑基网络手动换色

我正在尝试使用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)

r sankey-diagram networkd3

5
推荐指数
1
解决办法
2292
查看次数

如何在ggplot中为数字x轴的条形图添加直接标签

我正在尝试在 ggplot 中创建一个条形图,其中条形的宽度与变量相关联Cost$Sum.of.FS_P_Reduction_Kg。我正在使用该参数width=Sum.of.FS_P_Reduction_Kg根据变量设置条形的宽度。

我想向图表添加直接标签以标记每个条形,类似于下面记录的图像。我也在寻求添加与参数相对应的 x 轴标签width=Sum.of.FS_P_Reduction_Kg.任何帮助将不胜感激。我知道ggrepel但到目前为止还没有达到预期的效果。

带有直接标签和数字 x 轴的图形示例

我使用了以下代码:

# 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)

r bar-chart ggplot2 direct-labels ggrepel

3
推荐指数
1
解决办法
948
查看次数