我正在尝试在 ggplot 中创建一个条形图,其中条形的宽度与变量(Cost$Sum.of.FS_P_Reduction_Kg)相关联。我使用参数 width=Sum.of.FS_P_Reduction_Kg
根据变量设置条形的宽度。
当我尝试此操作时,数据两侧的 x 轴上似乎显示了残留条宽度。任何帮助将不胜感激。
我使用了以下代码:
# Plotting a bar plot
P <- ggplot(data=Cost,
aes(x = reorder(Row.Labels,
Average.of.Cost_Per_Kg_P_Removal.undiscounted.),
y = Average.of.Cost_Per_Kg_P_Removal.undiscounted.))+
geom_bar(aes(width = Sum.of.FS_P_Reduction_Kg),
stat="identity", position="stack") +
labs(x="Breed",
y="Hypothetical budget allocation (million)",
fill="") +
theme_bw() +
theme(axis.text.x = element_text(size=12, angle=50, vjust=0.9, hjust=0.9),
axis.text.y = element_text(size=12, angle=0, vjust=0.9, hjust=0.9),
axis.title = element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
P
可以使用以下代码重现示例数据表:
> dput(Cost)
structure(list(Row.Labels = structure(c(12L, 7L, 1L, 13L, 5L,
2L, 4L, 9L, 11L, 10L, 3L, 6L, 8L), .Label = c("Arable reversion to grassland",
"Below ground storage tank", "Farmer innovation", "Hard but permeable base for livestock trough",
"Inspection pit / chamber", "Livestock and machinery hardcore track",
"Livestock trough and associated pipework", "Outdoor concrete yard renewal",
"Post and wire", "Rainwater goods (e.g. downpipes and gutters)",
"Sheep netting", "Take small areas out of management", "Underground drainage pipework"
), class = "factor"), Sum.of.FS_P_Reduction_Kg = c(12.64101786,
0.176600362, 55.72340816, 3.703855174, 0.720092872, 2.706349044,
0.147201968, 2.384, 4.172, 0.902116348, 0.484584083, 2.804297297,
7.318418487), Average.of.FS_._Change = c(7.512500968, 0.171350352,
15.57475304, 1.134677581, 0.427948007, 1.373840031, 0.087481478,
1.416800649, 2.009493261, 1.09895273, 0.213553091, 1.86688413,
0.855936243), Average.of.Cost_Per_Kg_P_Removal.undiscounted. = c(2.803262345,
39.60180868, 62.83192667, 89.73898667, 117.1877639, 140.2541905,
142.5326142, 148.4987396, 204.7318937, 205.9310466, 482.1676716,
642.6339007, 1645.812466), Average.of.STW_Treatment_Cost_BASIC = c(155.1394461,
155.1394461, 155.1394461, 155.1394461, 155.1394461, 155.1394461,
155.1394461, 155.1394461, 155.1394461, 155.1394461, 155.1394461,
155.1394461, 155.1394461), Average.of.STW_Treatment_Cost_HIGH = c(286.0340149,
286.0340149, 286.0340149, 286.0340149, 286.0340149, 286.0340149,
286.0340149, 286.0340149, 286.0340149, 286.0340149, 286.0340149,
286.0340149, 286.0340149), Average.of.STW_Treatment_Cost_INTENSIVE = c(1109.93418,
1109.93418, 1109.93418, 1109.93418, 1109.93418, 1109.93418, 1109.93418,
1109.93418, 1109.93418, 1109.93418, 1109.93418, 1109.93418, 1109.93418
)), class = "data.frame", row.names = c(NA, -13L))
我正在尝试在 ggplot 中创建以下接受的副本:
最佳答案
这看起来像一个具有可变 bin 宽度的直方图。如果您有计算总和和平均值的原始数据,则可以将 geom_histogram 与“binwidth”参数一起使用。不幸的是,仅使用摘要数据很难复制该图,但这是一次尝试。我们可以计算每个矩形的边界,然后使用geom_rect
来绘制它们。
library(tidyverse)
Cost.new <- Cost %>%
mutate(
xmin = cumsum(lag(Sum.of.FS_P_Reduction_Kg, default = 0)),
xmax = cumsum(Sum.of.FS_P_Reduction_Kg),
ymin = 0,
ymax = Average.of.Cost_Per_Kg_P_Removal.undiscounted.,
label.pos = (xmin + xmax) / 2
)
ggplot(data = Cost.new) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), fill = 'white', color = 'black') +
scale_x_continuous(breaks = Cost.new$label.pos, labels = Cost.new$Row.Labels) +
theme(axis.text.x=element_text (size=12, angle=45, vjust=1, hjust=1),
axis.text.y=element_text (size=12),
axis.title=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
关于r - 在 ggplot 中创建条形图,其中条形的宽度与变量相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071879/